fs: add O_BENEATH flag to openat(2)
Subsystems (8)
v3 cover fs: add O_BENEATH flag to openat(2)
This change adds a new O_BENEATH flag for openat(2) which restricts the provided path, rejecting (with -EPERM) paths that are not beneath the provided dfd.
This change was originally included as part of a larger patchset
(https://lkml.org/lkml/2014/7/25/426) for Capsicum support; however, it is potentially useful as an independent change so I've pulled it out separately here.
In particular, various folks from Chrome[OS] have indicated an interest in having this functionality -- when combined with a seccomp filter it allows a directory to be accessed by a sandboxed process.
Changes since v2:
- Move tests into xfstests [Dave Chinner, with thanks for feedback on initial version]
- Merge up to v4.0-rc3 & latest man-pages
Changes since v1:
- Don't needlessly duplicate flags [Al Viro]
- Use EPERM rather than EACCES as error code [Paolo Bonzini]
- Disallow nd_jump_link for O_BENEATH [Al Viro/Andy Lutomirski]
- Add test of a jumped symlink (/proc/self/root)
Changes since the version included in the Capsicum v2 patchset:
- Add tests of normal symlinks
- Fix man-page typo
- Update patch to 3.17
Changes from v1 to v2 of Capsicum patchset:
- renamed O_BENEATH_ONLY to O_BENEATH [Christoph Hellwig]
David Drysdale (1):
fs: add O_BENEATH flag to openat(2)
arch/alpha/include/uapi/asm/fcntl.h | 1 + arch/parisc/include/uapi/asm/fcntl.h | 1 + arch/sparc/include/uapi/asm/fcntl.h | 1 + fs/fcntl.c | 4 ++-- fs/namei.c | 21 ++++++++++++++++++--- fs/open.c | 4 +++- fs/proc/base.c | 4 +++- fs/proc/namespaces.c | 8 ++++++-- include/linux/namei.h | 3 ++- include/uapi/asm-generic/fcntl.h | 4 ++++ 10 files changed, 41 insertions(+), 10 deletions(-)
--
2.2.0.rc0.207.ga3a616c
PATCH v3 1/3 fs: add O_BENEATH flag to openat(2)
Add a new O_BENEATH flag for openat(2) which restricts the provided path, rejecting (with -EPERM) paths that are not beneath the provided dfd. In particular, reject:
- paths that contain .. components
- paths that begin with /
- symlinks that have paths as above.
Also disallow use of nd_jump_link() for following symlinks without path expansion, when O_BENEATH is set.
--- arch/alpha/include/uapi/asm/fcntl.h | 1 + arch/parisc/include/uapi/asm/fcntl.h | 1 + arch/sparc/include/uapi/asm/fcntl.h | 1 + fs/fcntl.c | 4 ++-- fs/namei.c | 21 ++++++++++++++++++--- fs/open.c | 4 +++- fs/proc/base.c | 4 +++- fs/proc/namespaces.c | 8 ++++++-- include/linux/namei.h | 3 ++- include/uapi/asm-generic/fcntl.h | 4 ++++ 10 files changed, 41 insertions(+), 10 deletions(-)
arch/alpha/include/uapi/asm/fcntl.h +1 -0
@@ -33,6 +33,7 @@#define O_PATH 040000000#define __O_TMPFILE 0100000000 +#define O_BENEATH 0200000000/* no / or .. in openat path */ #define F_GETLK 7#define F_SETLK 8
arch/parisc/include/uapi/asm/fcntl.h +1 -0
@@ -21,6 +21,7 @@#define O_PATH 020000000#define __O_TMPFILE 040000000 +#define O_BENEATH 080000000/* no / or .. in openat path */ #define F_GETLK64 8#define F_SETLK64 9
arch/sparc/include/uapi/asm/fcntl.h +1 -0
@@ -36,6 +36,7 @@#define O_PATH 0x1000000#define __O_TMPFILE 0x2000000 +#define O_BENEATH 0x4000000/* no / or .. in openat path */ #define F_GETOWN 5/* for sockets. */ #define F_SETOWN 6/* for sockets. */
fs/fcntl.c +2 -2
@@ -740,7 +740,7 @@ static int __init fcntl_init(void)* Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY * is defined as O_NONBLOCK on some platforms and not on others. */ - BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ != HWEIGHT32( + BUILD_BUG_ON(22 - 1 /* for O_RDONLY being 0 */ != HWEIGHT32( O_RDONLY | O_WRONLY | O_RDWR | O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC | O_APPEND | /* O_NONBLOCK | */
@@ -748,7 +748,7 @@ static int __init fcntl_init(void)O_DIRECT |O_LARGEFILE |O_DIRECTORY |O_NOFOLLOW |O_NOATIME |O_CLOEXEC | __FMODE_EXEC |O_PATH | __O_TMPFILE | - __FMODE_NONOTIFY + __FMODE_NONOTIFY|O_BENEATH )); fasync_cache = kmem_cache_create("fasync_cache",
fs/namei.c +18 -3
@@ -705,13 +705,16 @@ static inline void path_to_nameidata(const struct path *path,* Helper to directly jump to a known parsed path from ->follow_link, * caller must have taken a reference to path beforehand. */ -void nd_jump_link (struct nameidata *nd,struct path *path) +int nd_jump_link (struct nameidata *nd,struct path *path) { +if (nd->flags &LOOKUP_BENEATH ) +return -EPERM ;path_put (&nd->path); nd->path = *path; nd->inode = nd->path.dentry->d_inode; nd->flags |=LOOKUP_JUMPED ; +return 0 ; }void nd_set_link (struct nameidata *nd,char *path)
@@ -1775,9 +1778,14 @@ static int link_path_walk(const char *name, struct nameidata *nd) {struct path next;int err; - -while (*name=='/' ) + +while (*name =='/' ) { +if (nd->flags &LOOKUP_BENEATH ) { + err = -EPERM ; + goto exit; + } name++; + }if (!*name)return 0 ;
@@ -1796,6 +1804,10 @@ static int link_path_walk(const char *name, struct nameidata *nd)if (name[0 ] =='.' )switch (hashlen_len (hash_len)) {case 2 :if (name[1 ] =='.' ) { +if (nd->flags &LOOKUP_BENEATH ) { + err = -EPERM ; + goto exit; + } type =LAST_DOTDOT ; nd->flags |=LOOKUP_JUMPED ; }
@@ -1847,6 +1859,7 @@ static int link_path_walk(const char *name, struct nameidata *nd)break ; } } +exit:terminate_walk (nd);return err; }
@@ -1886,6 +1899,8 @@ static int path_init(int dfd, const char *name, unsigned int flags, nd->m_seq =read_seqbegin (&mount_lock);if (*name=='/' ) { +if (flags &LOOKUP_BENEATH ) +return -EPERM ;if (flags &LOOKUP_RCU ) {rcu_read_lock (); nd->seq =set_root_rcu (nd);
fs/open.c +3 -1
@@ -902,7 +902,7 @@ static inline int build_open_flags(int flags, umode_t mode, struct open_flags *o* If we have O_PATH in the open flag. Then we * cannot have anything other than the below set of flags */ - flags &=O_DIRECTORY |O_NOFOLLOW |O_PATH ; + flags &=O_DIRECTORY |O_NOFOLLOW |O_PATH |O_BENEATH ; acc_mode =0 ; }else { acc_mode =MAY_OPEN |ACC_MODE (flags);
@@ -933,6 +933,8 @@ static inline int build_open_flags(int flags, umode_t mode, struct open_flags *o lookup_flags |=LOOKUP_DIRECTORY ;if (!(flags &O_NOFOLLOW )) lookup_flags |=LOOKUP_FOLLOW ; +if (flags &O_BENEATH ) + lookup_flags |=LOOKUP_BENEATH ; op->lookup_flags = lookup_flags;return 0 ; }
fs/proc/base.c +3 -1
@@ -1385,7 +1385,9 @@ static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)if (error) goto out; -nd_jump_link (nd, &path); + error =nd_jump_link (nd, &path); +if (error) + goto out;return NULL ; out:return ERR_PTR (error);
fs/proc/namespaces.c +6 -2
@@ -36,6 +36,7 @@ static void *proc_ns_follow_link(struct dentry *dentry, struct nameidata *nd)const struct proc_ns_operations *ns_ops =PROC_I (inode)->ns_ops;struct task_struct *task;struct path ns_path; +int err;void *error =ERR_PTR (-EACCES ); task =get_proc_task (inode);
@@ -44,8 +45,11 @@ static void *proc_ns_follow_link(struct dentry *dentry, struct nameidata *nd)if (ptrace_may_access (task,PTRACE_MODE_READ )) { error =ns_get_path (&ns_path, task, ns_ops); -if (!error) -nd_jump_link (nd, &ns_path); +if (!error) { + err =nd_jump_link (nd, &ns_path); +if (err) + error =ERR_PTR (err); + } }put_task_struct (task);return error;
include/linux/namei.h +2 -1
@@ -28,6 +28,7 @@ enum {LAST_NORM, LAST_ROOT, LAST_DOT, LAST_DOTDOT, LAST_BIND};#define LOOKUP_FOLLOW 0x0001#define LOOKUP_DIRECTORY 0x0002#define LOOKUP_AUTOMOUNT 0x0004 +#define LOOKUP_BENEATH 0x0008#define LOOKUP_PARENT 0x0010#define LOOKUP_REVAL 0x0020
@@ -70,7 +71,7 @@ extern int follow_up(struct path *);extern struct dentry *lock_rename (struct dentry *,struct dentry *);extern void unlock_rename (struct dentry *,struct dentry *); -extern void nd_jump_link (struct nameidata *nd,struct path *path); +extern int nd_jump_link (struct nameidata *nd,struct path *path);extern void nd_set_link (struct nameidata *nd,char *path);extern char *nd_get_link (struct nameidata *nd);
include/uapi/asm-generic/fcntl.h +4 -0
@@ -92,6 +92,10 @@#define O_TMPFILE (__O_TMPFILE | O_DIRECTORY)#define O_TMPFILE_MASK (__O_TMPFILE | O_DIRECTORY | O_CREAT) +#ifndef O_BENEATH +#define O_BENEATH 040000000/* no / or .. in openat path */ +#endif +#ifndef O_NDELAY #define O_NDELAY O_NONBLOCK#endif
--
2.2.0.rc0.207.ga3a616c
PATCH v3 2/3 generic: test openat and new O_BENEATH flag
In reply to David DrysdaleTest basic openat(2) behaviour.
Test that if O_BENEATH flag is set, openat() will only open paths that have no .. component and do not start with /. Symlinks are also checked for the same restrictions.
--- .gitignore | 1 + common/openat | 61 ++++++++++++++++++++++++++++++ src/Makefile | 3 +- src/openat.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/generic/151 | 89 ++++++++++++++++++++++++++++++++++++++++++++ tests/generic/151.out | 9 +++++ tests/generic/152 | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/generic/152.out | 23 ++++++++++++ tests/generic/group | 2 + 9 files changed, 388 insertions(+), 1 deletion(-) create mode 100644 common/openat create mode 100644 src/openat.c create mode 100755 tests/generic/151 create mode 100644 tests/generic/151.out create mode 100755 tests/generic/152 create mode 100644 tests/generic/152.out
.gitignore +1 -0
@@ -109,6 +109,7 @@ /src/cloner /src/renameat2 /src/t_rename_overwrite +/src/openat # dmapi/ binaries /dmapi/src/common/cmd/read_invis
common/openat new file +61 -0
61 lines not on this page · raw message
src/Makefile +2 -1
@@ -11,7 +11,8 @@ TARGETS = dirstress fill fill2 getpagesize holes lstat64 \ devzero feature alloc fault fstest t_access_root \ godown resvtest writemod makeextents itrash rename \ multi_open_unlink dmiperf unwritten_sync genhashnames t_holes \ - t_mmap_writev t_truncate_cmtime dirhash_collide t_rename_overwrite + t_mmap_writev t_truncate_cmtime dirhash_collide t_rename_overwrite \ + openat LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \ preallo_rw_pattern_writer ftrunc trunc fs_perms testx looptest \
src/openat.c new file +100 -0
100 lines not on this page · raw message
tests/generic/151 new file +89 -0
89 lines not on this page · raw message
tests/generic/151.out new file +9 -0
@@ -0,0 +1,9 @@ +QA output created by 151 +normal behavior, AT_FDCWD +normal behavior, dfd to main dir +normal behavior, dfd to subdir +normal behavior, absolute path +normal behavior, non-existent file (ENOENT) +No such file or directory +No such file or directory +No such file or directory
tests/generic/152 new file +101 -0
101 lines not on this page · raw message
tests/generic/152.out new file +23 -0
@@ -0,0 +1,23 @@ +QA output created by 152 +Test O_BENEATH: + Symlinks without .. or leading / are OK + ...unless of course we specify O_NOFOLLOW (ELOOP) +Too many levels of symbolic links +Too many levels of symbolic links +Too many levels of symbolic links + Can't open paths with .. in them (EPERM) +Operation not permitted +Operation not permitted +Operation not permitted +Operation not permitted + Can't open paths starting with / (EPERM) +Operation not permitted +Operation not permitted +Operation not permitted + Can't sneak around constraints with symlinks (EPERM) +Operation not permitted +Operation not permitted +Operation not permitted +Operation not permitted + Can't open files below pre-resolved symlinks in /proc +Operation not permitted
tests/generic/group +2 -0
@@ -96,6 +96,8 @@ 133 rw auto 135 metadata auto quick 141 rw auto quick +151 metadata auto quick +152 metadata auto quick 169 rw metadata auto quick 184 metadata auto quick 192 atime auto
--
1.9.1
PATCH v3 3/3 open.2: describe O_BENEATH flag
In reply to David Drysdale--- man2/open.2 | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)
man2/open.2 +32 -0
@@ -716,6 +716,31 @@ XFS support was added .\" commit ab29743117f9f4c22ac44c13c1647fb24fb2bafe in Linux 3.15. .TP +.B O_BENEATH " (since Linux 3.??)" +Ensure that the +.I pathname +is beneath the current working directory (for +.BR open (2)) +or the +.I dirfd +(for +.BR openat (2)). +If the +.I pathname +is absolute or contains a path component of "..", the +.BR open () +fails with the error +.BR EPERM. +This occurs even if ".." path component would not actually +escape the original directory; for example, a +.I pathname +of "subdir/../filename" would be rejected. +Path components that are symbolic links to absolute paths, or that are +relative paths containing a ".." component, will also cause the +.BR open () +operation to fail with the error +.BR EPERM. +.TP .B O_TRUNC If the file already exists and is a regular file and the access mode allows writing (i.e., is
@@ -984,6 +1009,13 @@ did not match the owner of the file and the caller was not privileged The operation was prevented by a file seal; see .BR fcntl (2). .TP +.B EPERM +The +.B O_BENEATH +flag was specified and the +.I pathname +was not beneath the relevant directory. +.TP .B EROFS .I pathname refers to a file on a read-only filesystem and write access was
--
2.2.0.rc0.207.ga3a616c
Re: [PATCHv3 man-pages 3/3] open.2: describe O_BENEATH flag
Quoted 1 line · David Drysdale
Signed-off-by David Drysdale <[email protected]>
Hi David,
The text looks good insofar as it goes. But, it would be helpful to have sentence or to that explains why this flag exists. Could you add that, please?
Thanks,
Michael
Quoted 57 lines
--- man2/open.2 | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)man2/open.2 +32
-1@@ -716,6 +716,31 @@ XFS support was added .\" commit ab29743117f9f4c22ac44c13c1647fb24fb2bafe in Linux 3.15. .TP +.B O_BENEATH " (since Linux 3.??)" +Ensure that the +.I pathname +is beneath the current working directory (for +.BR open (2)) +or the +.I dirfd +(for +.BR openat (2)). +If the +.I pathname +is absolute or contains a path component of "..", the +.BR open () +fails with the error +.BR EPERM. +This occurs even if ".." path component would not actually +escape the original directory; for example, a +.I pathname +of "subdir/../filename" would be rejected. +Path components that are symbolic links to absolute paths, or that are +relative paths containing a ".." component, will also cause the +.BR open () +operation to fail with the error +.BR EPERM. +.TP .B O_TRUNC If the file already exists and is a regular file and the access mode allows writing (i.e., is@@ -984,6 +1009,13 @@ did not match the owner of the file and the caller was not privileged The operation was prevented by a file seal; see .BR fcntl (2). .TP +.B EPERM +The +.B O_BENEATH +flag was specified and the +.I pathname +was not beneath the relevant directory. +.TP .B EROFS .I pathname refers to a file on a read-only filesystem and write access was --2.2.0.rc0.207.ga3a616c
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCHv3 man-pages 3/3] open.2: describe O_BENEATH flag
Quoted 5 lines · Michael Kerrisk (man-pages)
Quoted 1 line · David Drysdale
Signed-off-by David Drysdale <[email protected]>Hi David,
Quoted 7 lines
The text looks good insofar as it goes. But, it would be helpful to have sentence or to that explains why this flag exists. Could you add that, please?
Thanks,
Michael
How about something like:
This feature allows applications to be sure that the opened file is within the specified directory, regardless of the original source of the pathname argument. Some security-conscious pro‐ grams may further ensure this by imposing a system call filter (with seccomp(2)) that requires this flag for all open() opera‐ tions, so that the program cannot open files outside of speci‐ fied directories even if subverted.
(Also, I realize that I somehow failed to notice that the flags are listed in alphabetical order, so I'll move the text up, as in the updated diff below).
Thanks,
David
--- man2/open.2 | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+)
man2/open.2 +37 -0
@@ -201,6 +201,43 @@ See for further details. See also BUGS, below. .TP +.B O_BENEATH " (since Linux 4.??)" +Ensure that the +.I pathname +is beneath the current working directory (for +.BR open (2)) +or the +.I dirfd +(for +.BR openat (2)). +If the +.I pathname +is absolute or contains a path component of "..", the +.BR open () +fails with the error +.BR EPERM. +This occurs even if ".." path component would not actually +escape the original directory; for example, a +.I pathname +of "subdir/../filename" would be rejected. +Path components that are symbolic links to absolute paths, or that are +relative paths containing a ".." component, will also cause the +.BR open () +operation to fail with the error +.BR EPERM. + +This feature allows applications to be sure that the opened file is +within the specified directory, regardless of the original source of the +.I pathname +argument. +Some security-conscious programs may further ensure +this by imposing a system call filter (with +.BR seccomp (2)) +that requires this flag for all +.BR open () +operations, so that the program cannot open files outside of +specified directories even if subverted. +.TP .BR O_CLOEXEC " (since Linux 2.6.23)" .\" NOTE! several other man pages refer to this text Enable the close-on-exec flag for the new file descriptor.
@@ -984,6 +1021,13 @@ did not match the owner of the file and the
caller was not privileged
The operation was prevented by a file seal; see
.BR fcntl (2).
.TP
+.B EPERM
+The
+.B O_BENEATH
+flag was specified and the
+.I pathname
+was not beneath the relevant directory.
+.TP
.B EROFS
.I pathname
refers to a file on a read-only filesystem and write access was
Signature · 5 lines
2.2.0.rc0.207.ga3a616c -- To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCHv3 man-pages 3/3] open.2: describe O_BENEATH flag
Quoted 25 lines · David Drysdale
Quoted 5 lines · Michael Kerrisk (man-pages)
Quoted 1 line · David Drysdale
Signed-off-by David Drysdale <[email protected]>Hi David,
Quoted 7 lines
The text looks good insofar as it goes. But, it would be helpful to have sentence or to that explains why this flag exists. Could you add that, please?
Thanks,
Michael
How about something like:
This feature allows applications to be sure that the opened file is within the specified directory, regardless of the original source of the pathname argument. Some security-conscious pro‐ grams may further ensure this by imposing a system call filter (with seccomp(2)) that requires this flag for all
open()opera‐ tions, so that the program cannot open files outside of speci‐ fied directories even if subverted.
Quoted 3 lines
(Also, I realize that I somehow failed to notice that the flags are listed in alphabetical order, so I'll move the text up, as in the updated diff below).
That looks good to me. Thanks!
Cheers,
Michael
Quoted 69 lines
--- man2/open.2 | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+)man2/open.2 +37
-0@@ -201,6 +201,43 @@ See for further details. See also BUGS, below. .TP +.B O_BENEATH " (since Linux 4.??)" +Ensure that the +.I pathname +is beneath the current working directory (for +.BR open (2)) +or the +.I dirfd +(for +.BR openat (2)). +If the +.I pathname +is absolute or contains a path component of "..", the +.BR open () +fails with the error +.BR EPERM. +This occurs even if ".." path component would not actually +escape the original directory; for example, a +.I pathname +of "subdir/../filename" would be rejected. +Path components that are symbolic links to absolute paths, or that are +relative paths containing a ".." component, will also cause the +.BR open () +operation to fail with the error +.BR EPERM. + +This feature allows applications to be sure that the opened file is +within the specified directory, regardless of the original source of the +.I pathname +argument. +Some security-conscious programs may further ensure +this by imposing a system call filter (with +.BR seccomp (2)) +that requires this flag for all +.BR open () +operations, so that the program cannot open files outside of +specified directories even if subverted. +.TP .BR O_CLOEXEC " (since Linux 2.6.23)" .\" NOTE! several other man pages refer to this text Enable the close-on-exec flag for the new file descriptor.@@ -984,6 +1021,13 @@ did not match the owner of the file and thecaller was not privileged
The operation was prevented by a file seal; see
.BR fcntl (2).
.TP
+.B EPERMman2/open.2 +6
-1+The +.B O_BENEATH +flag was specified and the +.I pathname +was not beneath the relevant directory. +.TP .B EROFS .I pathname refers to a file on a read-only filesystem and write access was --2.2.0.rc0.207.ga3a616c
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCHv3 xfstests 2/3] generic: test openat and new O_BENEATH flag
In reply to David DrysdaleQuoted 6 lines · David Drysdale
Test basic openat(2) behaviour.
Test that if
O_BENEATHflag is set,openat()will only open paths that have no .. component and do not start with /. Symlinks are also checked for the same restrictions.
Quoted 6 lines
Signed-off-by David Drysdale <[email protected]>--- .gitignore | 1 + common/openat | 61 ++++++++++++++++++++++++++++++ src/Makefile | 3 +- src/openat.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++
This strikes me as something that shoul dbe added to xfs_io for testing, as it already supports a heap of other open flags and xfstests is already dependent on it.
Quoted 5 lines
tests/generic/151 | 89 ++++++++++++++++++++++++++++++++++++++++++++ tests/generic/151.out | 9 +++++ tests/generic/152 | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/generic/152.out | 23 ++++++++++++ tests/generic/group | 2 +
I'd also prefer one patch per new test - it's easier to review...
Quoted 12 lines
common/openat new file +12
-0+_openat_setup() +{ + local dir=$1 + + mkdir -p $dir/subdir + echo 0123456789 > $dir/topfile + echo 0123456789 > $dir/subdir/bottomfile + + ln -s subdir/bottomfile $dir/symlinkdown + ln -s ../topfile $dir/subdir/symlinkup + ln -s $dir/topfile $dir/subdir/symlinkout + ln -s bottomfile $dir/subdir/symlinkin
common/openat line 47
Quoted 10 lines
common/openat new file +10
-0+} + +# +# Check whether the openat wrapper program is available +# +_requires_openat() +{ + OPENAT_PROG=$here/src/openat + _require_command $OPENAT_PROG +}
if this is part of xfs_io, then _requires_xfs_io_command "open -b" could be used to test if the command is supported, and no need for this function at all.
common/openat line 61
Quoted 13 lines
common/openat new file +13
-0+# +# This checks whether the O_BENEATH flag is supported by the openat syscall +# +_requires_o_beneath() +{ + # Kernels that don't support O_BENEATH will silently accept it, so + # check for O_BENEATH behavior: attempting to open an absolute + # path should fail with EPERM. + $OPENAT_PROG -t -b $TEST_DIR + if [ $? -ne 0 ]; then + _notrun "kernel doesn't support O_BENEATH flag in openat syscall" + fi +}
as running the command would tell us if the kernel supports it, too.
src/openat.c line 38
Quoted 13 lines
src/openat.c new file +13
-0+#endif +#endif + +void usage (const char *progname) +{ +fprintf (stderr,"Usage: %s [-f dirname] [-b] [-n] [-t] <file>\n" , + progname); +fprintf (stderr," -f dirname : use this dir for dfd\n" ); +fprintf (stderr," -b : open with O_BENEATH\n" ); +fprintf (stderr," -n : open with O_NOFOLLOW\n" ); +fprintf (stderr," -t : test for expected EPERM failure\n" ); +fprintf (stderr," -h : show this usage message\n" ); +exit (1 );
Hmm - you're also testing O_NOFOLLOW behaviour too? Perhaps that should be mentioned/added to xfs_io, too?
The reason I suggest this, even though it's a little more work, is tht we can then re-use the new flags in other tests easily without needing to write new helper functions...
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Re: [PATCHv3 xfstests 2/3] generic: test openat and new O_BENEATH flag
Quoted 14 lines · Dave Chinner
Quoted 6 lines · David Drysdale
Test basic openat(2) behaviour.
Test that if
O_BENEATHflag is set,openat()will only open paths that have no .. component and do not start with /. Symlinks are also checked for the same restrictions.Quoted 6 lines
Signed-off-by David Drysdale <[email protected]>--- .gitignore | 1 + common/openat | 61 ++++++++++++++++++++++++++++++ src/Makefile | 3 +- src/openat.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++
Quoted 3 lines
This strikes me as something that shoul dbe added to
xfs_iofor testing, as it already supports a heap of other open flags and xfstests is already dependent on it.
While I don't see a problem adding this to xfs_io, I'd still like to see this test live in the kernel tree too. Having it in the same source means more testing, IMO.
-Kees
Signature · 2 lines
Kees Cook Chrome OS Security
Re: [PATCHv3 xfstests 2/3] generic: test openat and new O_BENEATH flag
Quoted 15 lines · Kees Cook
Quoted 13 lines · Dave Chinner
Quoted 11 lines · David Drysdale
Test basic openat(2) behaviour.
Test that if
O_BENEATHflag is set,openat()will only open paths that have no .. component and do not start with /. Symlinks are also checked for the same restrictions.Signed-off-by David Drysdale <[email protected]> ---.gitignore | 1 + common/openat | 61
++++++++++++++++++++++++++++++ src/Makefile | 3 +-src/openat.c| 100
+++++++++++++++++++++++++++++++++++++++++++++++++
Quoted 6 lines
Quoted 3 lines
This strikes me as something that shoul dbe added to
xfs_iofor testing, as it already supports a heap of other open flags and xfstests is already dependent on it.While I don't see a problem adding this to
xfs_io, I'd still like to see this test live in the kernel tree too.
You can do whatever you want with the kernel tree - it doesn't concern me at all.
Quoted 2 lines
Having it in the
same source means more testing, IMO.
That's complete bunk, though. This helper binary is a limited use, one shot test that can't be combined with anything else. "sharing" the source code across multiple different test suites doesn't change that. However, putting support into xfs_io means it will get more testing because we can *easily* use the functionality in many, many different ways.
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Re: [PATCHv3 xfstests 2/3] generic: test openat and new O_BENEATH flag
[resend, remembering to tick the Plaintext button this time -- sorry]
Quoted 29 lines · Dave Chinner
Quoted 15 lines · Kees Cook
Quoted 13 lines · Dave Chinner
Quoted 11 lines · David Drysdale
Test basic openat(2) behaviour.
Test that if
O_BENEATHflag is set,openat()will only open paths that have no .. component and do not start with /. Symlinks are also checked for the same restrictions.Signed-off-by David Drysdale <[email protected]> ---.gitignore | 1 + common/openat | 61
++++++++++++++++++++++++++++++ src/Makefile | 3 +-src/openat.c| 100
+++++++++++++++++++++++++++++++++++++++++++++++++Quoted 6 lines
Quoted 3 lines
This strikes me as something that shoul dbe added to
xfs_iofor testing, as it already supports a heap of other open flags and xfstests is already dependent on it.While I don't see a problem adding this to
xfs_io, I'd still like to see this test live in the kernel tree too.You can do whatever you want with the kernel tree - it doesn't concern me at all.
Quoted 2 lines
Having it in the
same source means more testing, IMO.
Quoted 6 lines
That's complete bunk, though. This helper binary is a limited use, one shot test that can't be combined with anything else. "sharing" the source code across multiple different test suites doesn't change that. However, putting support into
xfs_iomeans it will get more testing because we can *easily* use the functionality in many, many different ways.
It's useful to have a simple self-test in the kernel tree, so that kernel developers (and their buildbots) can catch any obvious regressions that might arise when working in related areas, without requiring any other dependencies.
(Personally, I also think it's good that new kernel APIs come with demonstrations of their use in userspace in a nearby commit.)
OTOH, testing openat() behaviour should clearly be a part of the xfstests suite (as a general/comprehensive filesystem test repository), and Dave makes a good point that putting the openat() functionality into xfs_io allows for more flexible test combinations.
So I guess ideally we should end up with:
a) Reinstate the kernel-tree selftests from the earlier iteration.
b) Add openat test functionality into xfs_io
c) Migrate the new xfstests cases to use xfs_io rather than a local openat utility program
However, given that I've already moved the test code once, and that a)-c) involve three different source trees, I'd like to de-couple things:
- Concentrate on kernel changes and a) on the LKML.
- If & when the kernel changes make progress, return to
b) and c).
Sound sensible?
Thanks,
David.
Quoted 6 lines
Cheers,
Dave.
--
Dave Chinner
[email protected]