Bugfix for Linux open(O_CREAT|O_EXCL)
PATCH 1/1 Bugfix for Linux open(O_CREAT|O_EXCL)
From: Simon Lindholm <[email protected]>
In Linux, when calling open(path, O_CREAT|O_EXCL) it implies O_NOFOLLOW. If the path exists and is a symlink, the link shouldn't be resolved but rather just fail directly with EEXIST. Resolving the symlink causes problems with e.g. extracting files with GNU tar where strange errors ensue when it tries to overwrite an existing symlink.
Fix the bug by not resolving symlinks for open (and the related functions) if O_EXCL is set.
For original patch from Simon Lindholm see:
See: https://bugzilla.yoctoproject.org/show_bug.cgi?id=15559
Original submission was modified to specifically check for BOTH O_CREAT|O_EXCL, as this matches the behavior in the Linux kernel:
fs/open.c: build_open_flags(...)
if (flags & O_CREAT) {
op->intent |= LOOKUP_CREATE;
if (flags & O_EXCL) {
op->intent |= LOOKUP_EXCL;
flags |= O_NOFOLLOW;
}
}If the kernel sees O_CREAT and O_EXCL, it assumes O_NOFOLLOW, we need to duplicate the same behavior. Note, this behavior is likely Linux specific thus implemented in the ports/linu/wrapfuncs.in.
--- makewrappers | 5 +- ports/linux/wrapfuncs.in | 12 ++--- test/test-open-symlinks.c | 99 ++++++++++++++++++++++++++++++++++++++ test/test-open-symlinks.sh | 18 +++++++ 4 files changed, 126 insertions(+), 8 deletions(-) create mode 100644 test/test-open-symlinks.c create mode 100755 test/test-open-symlinks.sh
makewrappers +3 -2
@@ -291,15 +291,16 @@ class Function: # handle special comments, such as flags=AT_SYMLINK_NOFOLLOW if self.comments: + comments = self.comments.replace('==','<equals>') # Build a dictionary of key=value, key=value pairs - modifiers = dict(mod.split("=") for mod in self.comments.split(',')) + modifiers = dict(mod.split("=") for mod in comments.split(',')) # Strip all leading/trailing whitespace modifiers = {k.strip():v.strip() for k, v in modifiers.items()} arch = "-" + platform.machine() # Sorted so that versions-foo appear after versions, so overrides are easy for key in sorted(modifiers): - value = modifiers[key] + value = modifiers[key].replace('<equals>', '==') # If the key is version-arm64 and we're on arm64 then rename this to version if key.endswith(arch): key = key.replace(arch, "")
ports/linux/wrapfuncs.in +6 -6
@@ -1,4 +1,4 @@ -int open(const char *path, int flags, ...{mode_t mode}); /* flags=flags&O_NOFOLLOW, noignore_path=1 */ +int open(const char *path, int flags, ...{mode_t mode}); /* flags=((flags&O_NOFOLLOW)||((flags&(O_CREAT|O_EXCL))==(O_CREAT|O_EXCL))), noignore_path=1 */ char *get_current_dir_name(void); int __xstat(int ver, const char *path, struct stat *buf); int __lxstat(int ver, const char *path, struct stat *buf); /* flags=AT_SYMLINK_NOFOLLOW */
@@ -6,8 +6,8 @@ int __fxstat(int ver, int fd, struct stat *buf); int lchmod(const char *path, mode_t mode); /* flags=AT_SYMLINK_NOFOLLOW */ int lchown(const char *path, uid_t owner, gid_t group); /* flags=AT_SYMLINK_NOFOLLOW */ int __fxstatat(int ver, int dirfd, const char *path, struct stat *buf, int flags); -int openat(int dirfd, const char *path, int flags, ...{mode_t mode}); /* flags=flags&O_NOFOLLOW, noignore_path=1 */ -int __openat_2(int dirfd, const char *path, int flags); /* flags=flags&O_NOFOLLOW, noignore_path=1 */ +int openat(int dirfd, const char *path, int flags, ...{mode_t mode}); /* flags=((flags&O_NOFOLLOW)||((flags&(O_CREAT|O_EXCL))==(O_CREAT|O_EXCL))), noignore_path=1 */ +int __openat_2(int dirfd, const char *path, int flags); /* flags=((flags&O_NOFOLLOW)||((flags&(O_CREAT|O_EXCL))==(O_CREAT|O_EXCL))), noignore_path=1 */ int mknod(const char *path, mode_t mode, dev_t dev); /* real_func=pseudo_mknod */ int mknodat(int dirfd, const char *path, mode_t mode, dev_t dev); /* real_func=pseudo_mknodat */ int __xmknod(int ver, const char *path, mode_t mode, dev_t *dev); /* flags=AT_SYMLINK_NOFOLLOW */
@@ -17,9 +17,9 @@ int fcntl64(int fd, int cmd, ...{struct flock *lock}); /* noignore_path=1 */ # just so we know the inums of symlinks char *canonicalize_file_name(const char *filename); int eaccess(const char *path, int mode); -int open64(const char *path, int flags, ...{mode_t mode}); /* flags=flags&O_NOFOLLOW, noignore_path=1 */ -int openat64(int dirfd, const char *path, int flags, ...{mode_t mode}); /* flags=flags&O_NOFOLLOW, noignore_path=1 */ -int __openat64_2(int dirfd, const char *path, int flags); /* flags=flags&O_NOFOLLOW, noignore_path=1 */ +int open64(const char *path, int flags, ...{mode_t mode}); /* flags=((flags&O_NOFOLLOW)||((flags&(O_CREAT|O_EXCL))==(O_CREAT|O_EXCL))), noignore_path=1 */ +int openat64(int dirfd, const char *path, int flags, ...{mode_t mode}); /* flags=((flags&O_NOFOLLOW)||((flags&(O_CREAT|O_EXCL))==(O_CREAT|O_EXCL))), noignore_path=1 */ +int __openat64_2(int dirfd, const char *path, int flags); /* flags=((flags&O_NOFOLLOW)||((flags&(O_CREAT|O_EXCL))==(O_CREAT|O_EXCL))), noignore_path=1 */ int creat64(const char *path, mode_t mode); int stat(const char *path, struct stat *buf); /* real_func=pseudo_stat */ int lstat(const char *path, struct stat *buf); /* real_func=pseudo_lstat, flags=AT_SYMLINK_NOFOLLOW */
test/test-open-symlinks.c new file +99 -0
99 lines not on this page · raw message
test/test-open-symlinks.sh new file +18 -0
@@ -0,0 +1,18 @@ +#! /bin/sh + +# * Create a temporary directory +# * Create some symlinks in the directory +# * Run test/test-open-symlinks, which will call open(O_CREAT|O_EXCL) on the +# symlinks in the directory and check that errno is EEXIST. + +tmpdir="$(mktemp -d -p "$PWD")" +trap "rm -rf '$tmpdir'" EXIT TERM INT + +# 'a' is a symlink to a file that doesn't exist +ln -s noexist.txt "$tmpdir/a" + +# 'b' is a symlink to a file that doesn't exist and where we also don't have +# write permissions to the parent directory (of the target). +ln -s /root/noexist.txt "$tmpdir/b" + +./test/test-open-symlinks "$tmpdir"
Signature · 1 line
2.34.1