Skip to content

udev-node: use flock() for symlink stack directory - #23043

Merged
yuwata merged 10 commits into
systemd:mainfrom
yuwata:udev-node-use-flock
Sep 12, 2022
Merged

yuwata merged 10 commits into
systemd:mainfrom
yuwata:udev-node-use-flock

Conversation

@yuwata

@yuwata yuwata commented Apr 11, 2022

Copy link
Copy Markdown
Member

No description provided.

Comment thread src/udev/udev-node.c Resolved
Comment thread src/udev/udev-node.c Resolved
Comment thread src/udev/udev-node.c Resolved
Comment thread src/udev/udev-node.c Resolved
Comment thread src/udev/udev-node.c Resolved
@poettering poettering added the reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks label Apr 11, 2022
@poettering poettering changed the title udev-node: use flock() udev-node: use flock() for symlink stack directory Apr 11, 2022
Comment thread src/udev/udev-node.c Resolved
@yuwata
yuwata force-pushed the udev-node-use-flock branch from 2c48e52 to 2267c38 Compare April 11, 2022 12:55
@yuwata yuwata added please-review and removed reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks labels Apr 11, 2022
@yuwata

yuwata commented Apr 11, 2022

Copy link
Copy Markdown
Member Author

@poettering Thank you for the review. Most of comments are addressed. PTAL.

Comment thread src/udev/udev-node.c Resolved
Comment thread src/udev/udev-node.c Resolved
Comment thread src/udev/udev-node.c Resolved
Comment thread src/udev/udev-node.c Resolved
@poettering poettering added reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks and removed please-review labels Apr 12, 2022
@yuwata
yuwata force-pushed the udev-node-use-flock branch from 2267c38 to 1a340d6 Compare April 12, 2022 15:11
@yuwata

yuwata commented Apr 12, 2022

Copy link
Copy Markdown
Member Author

@poettering Thank you for the review. The commit message and the comment in the code is updated. Also, the cleanup logic is updated. PTAL.

@yuwata yuwata added please-review and removed reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks labels Apr 12, 2022
Comment thread src/udev/udev-node.c Resolved
@yuwata
yuwata force-pushed the udev-node-use-flock branch from 1a340d6 to 02952ad Compare April 12, 2022 15:51
Comment thread src/basic/path-util.c Resolved
Comment thread src/udev/udev-node.c Resolved
Comment thread src/udev/udev-node.c Resolved
Comment thread src/udev/udev-node.c Resolved
Comment thread src/udev/udev-node.c Resolved
Comment thread src/udev/udev-node.c Resolved
@poettering

Copy link
Copy Markdown
Member

Hmm, shouldn't we just use a stack directory with a different name to avoid the locking issues.

I kinda like the idea, but how to handle upgrades then?

@poettering

Copy link
Copy Markdown
Member

I kinda like the idea, but how to handle upgrades then?

Maybe we should just tell people to retrigger everything on update. Which they really should do anyway. If so, then the old dir just doesn't matter (though we wouldn't clean it up either, but I'd be OK with that)

@poettering

Copy link
Copy Markdown
Member

think this should be handled in tempfn_xxxxxxx instead. If we assume tempfn_xxxxxxx can fail, we'll have to have the same fallback in every caller, or at least in those where the filename is controlled externally or unpredictable.
Let's make tempfn_xxxxxxx smarter: only use up to NAME_MAX - strlen(extra) - strlen(".#" "XXXXXX") of the original file name. This way the function can only fail on oom, and callers become simpler.

So I thought about this approach, and I think we should implement this, but this doesn't solve the problem fully, because if you want to create a temporary path for something that has NAME_MAX length already, and the last compononent (i.e. the filename) is just one character, then you cannot possibly stick six XXXXXX characters in there anymore. So while it pushes the problem out, it doesn't solve it...

@keszybz

keszybz commented Apr 13, 2022

Copy link
Copy Markdown
Member

Can you give an example? I don't understand the comment.

Then, we can always assume the directory exists, and the code become
slightly simpler.

Note, unused directories are removed by the main udevd process in a
later commit.
And try to read it only when the file is symlink.
Also shorten code a bit.

Just for consistency with other part and readability of the code.
By locking the stack directory, we can safely determine the device node
with the highest priority for a symlink. So, the multiple try-and-wait
loops can be dropped, and the code becomes quite simple.
By the previous commit, the stack directories are not removed even if
it is empty. To reduce the inode usage of /run, let's cleanup the
directories.
@yuwata
yuwata force-pushed the udev-node-use-flock branch from 4cdeb8b to 1055172 Compare September 2, 2022 20:02
@yuwata

yuwata commented Sep 2, 2022

Copy link
Copy Markdown
Member Author

@poettering Thank you for the review. All comments are addressed. PTAL.

@fbuihuu Sorry for late response again.

If the lock file is readable by an unprivileged user, then the user can lock the file, and udev workers are blocked to access the dir. If the directory is not workd-accessible, then there should be no problem.

Again the lock file is created with permissions 600, hence let me ask again how an unprivileged process could open the lock file.

Hm, yeah, the file is created with 0600. There is no problem.

If the lock file is removed by a worker on unlock, then another worker needs to recreate it before lock the file. Hence, simple open() + flock() does not work...

make_lock_file_for() seems to be prepared to handle this case, no ?

Not sure, maybe. If so, we can rewrite with it. But let's do that later.

@yuwata yuwata added please-review and removed reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks needs-reporter-feedback ❓ There's an unanswered question, the reporter needs to answer labels Sep 2, 2022
@poettering poettering added good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed and removed please-review labels Sep 9, 2022
@yuwata
yuwata merged commit 75a5a14 into systemd:main Sep 12, 2022
@yuwata
yuwata deleted the udev-node-use-flock branch September 12, 2022 14:40
yuwata added a commit to yuwata/systemd that referenced this pull request Sep 12, 2022
If the filename of a device symlink is too long, then the temporary
filename may become invalid, and we fail to create symlink.

The function `tempfn_random()` used in symlink_atomic_full() generates
a safe temporary filename.

Note that, thanks to the PR systemd#23043, now only one worker can handle
the same symlink simultaneously. Hence, the device ID based temporary
filename is not necessary.
yuwata added a commit to yuwata/systemd that referenced this pull request Sep 12, 2022
If the filename of a device symlink is too long, then the temporary
filename may become invalid, and we fail to create symlink.

The function `tempfn_random()` used in symlink_atomic_full() generates
a safe temporary filename.

Note that, thanks to the PR systemd#23043, now only one worker can handle
the same symlink simultaneously. Hence, the device ID based temporary
filename is not necessary.
yuwata added a commit to yuwata/systemd that referenced this pull request Sep 13, 2022
If the filename of a device symlink is too long, then the temporary
filename may become invalid, and we fail to create symlink.

The function `tempfn_random()` used in symlink_atomic_full() generates
a safe temporary filename.

Note that, thanks to the PR systemd#23043, now only one worker can handle
the same symlink simultaneously. Hence, the device ID based temporary
filename is not necessary.
yuwata added a commit to yuwata/systemd that referenced this pull request Sep 14, 2022
If the filename of a device symlink is too long, then the temporary
filename may become invalid, and we fail to create symlink.

The function `tempfn_random()` used in symlink_atomic_full() generates
a safe temporary filename.

Note that, thanks to the PR systemd#23043, now only one worker can handle
the same symlink simultaneously. Hence, the device ID based temporary
filename is not necessary.
yuwata added a commit to yuwata/systemd that referenced this pull request Sep 16, 2022
If the filename of a device symlink is too long, then the temporary
filename may become invalid, and we fail to create symlink.

The function `tempfn_random()` used in symlink_atomic_full() generates
a safe temporary filename.

Note that, thanks to the PR systemd#23043, now only one worker can handle
the same symlink simultaneously. Hence, the device ID based temporary
filename is not necessary.
yuwata added a commit to yuwata/systemd that referenced this pull request Sep 16, 2022
If the filename of a device symlink is too long, then the temporary
filename may become invalid, and we fail to create symlink.

The function `tempfn_random()` used in symlink_atomic_full() generates
a safe temporary filename.

Note that, thanks to the PR systemd#23043, now only one worker can handle
the same symlink simultaneously. Hence, the device ID based temporary
filename is not necessary.
yuwata added a commit to yuwata/systemd that referenced this pull request Sep 18, 2022
If the filename of a device symlink is too long, then the temporary
filename may become invalid, and we fail to create symlink.

The function `tempfn_random()` used in symlink_atomic_full() generates
a safe temporary filename.

Note that, thanks to the PR systemd#23043, now only one worker can handle
the same symlink simultaneously. Hence, the device ID based temporary
filename is not necessary.
yuwata added a commit to yuwata/systemd that referenced this pull request Sep 18, 2022
If the filename of a device symlink is too long, then the temporary
filename may become invalid, and we fail to create symlink.

The function `tempfn_random()` used in symlink_atomic_full() generates
a safe temporary filename.

Note that, thanks to the PR systemd#23043, now only one worker can handle
the same symlink simultaneously. Hence, the device ID based temporary
filename is not necessary.
yuwata added a commit to yuwata/systemd that referenced this pull request Sep 18, 2022
If the filename of a device symlink is too long, then the temporary
filename may become invalid, and we fail to create symlink.

The function `tempfn_random()` used in symlink_atomic_full() generates
a safe temporary filename.

Note that, thanks to the PR systemd#23043, now only one worker can handle
the same symlink simultaneously. Hence, the device ID based temporary
filename is not necessary.
@keszybz keszybz removed the good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed label Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

5 participants