file: add fd_except cleanup class
Subsystems (2)
PATCH 1/3 file: add fd_except cleanup class
CLASS(fd_except, f)(fd, mask) allows to specify whether to accept or reject O_PATH file descriptors by passing a mode value of 0 or FMODE_PATH rather than using a specific cleanup class for each case (CLASS(fd, f)(fd) or CLASS(fd_raw, f)(fd)).
--- fs/file.c | 5 +++++ include/linux/file.h | 2 ++ 2 files changed, 7 insertions(+)
fs/file.c +5 -0
@@ -1211,6 +1211,11 @@ struct fd fdget(unsigned int fd) }EXPORT_SYMBOL (fdget); +struct fd fdget_except (unsigned int fd,fmode_t mask) +{ +return __fget_light (fd, mask); +} +struct fd fdget_raw (unsigned int fd) {return __fget_light (fd,0 );
include/linux/file.h +2 -0
@@ -70,6 +70,7 @@ extern struct file *fget_task_next(struct task_struct *task, unsigned int *fd);extern void __f_unlock_pos (struct file *);struct fd fdget (unsigned int fd); +struct fd fdget_except (unsigned int fd,fmode_t mask);struct fd fdget_raw (unsigned int fd);struct fd fdget_pos (unsigned int fd);
@@ -81,6 +82,7 @@ static inline void fdput_pos(struct fd f) } DEFINE_CLASS(fd, struct fd, fdput(_T), fdget(fd), int fd) +DEFINE_CLASS(fd_except, struct fd, fdput(_T), fdget_except(fd, mask), int fd, fmode_t mask) DEFINE_CLASS(fd_raw, struct fd, fdput(_T), fdget_raw(fd), int fd) DEFINE_CLASS(fd_pos, struct fd, fdput_pos(_T), fdget_pos(fd), int fd)
Signature · 1 line
2.54.0
PATCH 2/3 xattrat: accept empty O_PATH file descriptors
Right now, the setxattrat(), getxattrat(), listxattrat(), and removexattrat() system calls fail with -EBADF when dfd is an O_PATH file descriptor, pathname is an empty string or NULL, and the AT_EMPTY_PATH flag in at_flags is set. (Regular non-O_PATH file descriptors are accepted.) This is inconsistent with the behavior of system calls like fstatat() and fchmodat() which do accept those file descriptors.
The same operations can also be carried out indirectly by using "/proc/self/fd/<dfd>" as the pathname, but that is only more cumbersome, and for no good reason. Fix that by changing the xattrat() system calls to accept these file descriptors directly.
We stick with the existing practice of leaving the behavior of the fsetxattr(), fgetxattr(), flistxattr(), and fremovexattr() system calls unchanged: those will still reject O_PATH file descriptors.
--- fs/xattr.c | 56 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 24 deletions(-)
fs/xattr.c +32 -24
@@ -692,7 +692,8 @@ int filename_setxattr(int dfd, struct filename *filename,static int path_setxattrat (int dfd,const char __user *pathname,unsigned int at_flags,const char __user *name, -const void __user *value,size_t size,int flags) +const void __user *value,size_t size,int flags, +fmode_t mask) {struct xattr_name kname;struct kernel_xattr_ctx ctx = {
@@ -717,7 +718,7 @@ static int path_setxattrat(int dfd, const char __user *pathname,CLASS (filename_maybe_null, filename)(pathname, at_flags);if (!filename && dfd >=0 ) { -CLASS (fd, f)(dfd); +CLASS (fd_except, f)(dfd, mask);if (fd_empty (f)) error = -EBADF ;else
@@ -750,14 +751,15 @@ SYSCALL_DEFINE6(setxattrat, int, dfd, const char __user *, pathname, unsigned inreturn path_setxattrat (dfd, pathname, at_flags, name,u64_to_user_ptr (args.value), args.size, - args.flags); + args.flags,0 ); } SYSCALL_DEFINE5(setxattr, const char __user *, pathname, const char __user *, name, const void __user *, value, size_t, size, int, flags) { -return path_setxattrat (AT_FDCWD , pathname,0 , name, value, size, flags); +return path_setxattrat (AT_FDCWD , pathname,0 , name, value, size, flags, +FMODE_PATH ); } SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
@@ -765,14 +767,14 @@ SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname, size_t, size, int, flags) { return path_setxattrat(AT_FDCWD, pathname, AT_SYMLINK_NOFOLLOW, name, -value ,size ,flags ); +value ,size ,flags ,FMODE_PATH ); } SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name, const void __user *,value, size_t, size, int, flags) {return path_setxattrat (fd,NULL ,AT_EMPTY_PATH , name, - value, size, flags); + value, size, flags,FMODE_PATH ); }/*
@@ -837,7 +839,7 @@ ssize_t filename_getxattr(int dfd, struct filename *filename,static ssize_t path_getxattrat (int dfd,const char __user *pathname,unsigned int at_flags,const char __user *name, -void __user *value,size_t size) +void __user *value,size_t size,fmode_t mask) {struct xattr_name kname;struct kernel_xattr_ctx ctx = {
@@ -857,7 +859,7 @@ static ssize_t path_getxattrat(int dfd, const char __user *pathname,CLASS (filename_maybe_null, filename)(pathname, at_flags);if (!filename && dfd >=0 ) { -CLASS (fd, f)(dfd); +CLASS (fd_except, f)(dfd, mask);if (fd_empty (f))return -EBADF ;return file_getxattr (fd_file (f), &ctx);
@@ -891,26 +893,28 @@ SYSCALL_DEFINE6(getxattrat, int, dfd, const char __user *, pathname, unsigned inreturn -EINVAL ;return path_getxattrat (dfd, pathname, at_flags, name, -u64_to_user_ptr (args.value), args.size); +u64_to_user_ptr (args.value), args.size,0 ); } SYSCALL_DEFINE4(getxattr, const char __user *, pathname, const char __user *, name, void __user *, value, size_t, size) { -return path_getxattrat (AT_FDCWD , pathname,0 , name, value, size); +return path_getxattrat (AT_FDCWD , pathname,0 , name, value, size, +FMODE_PATH ); } SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname, const char __user *, name, void __user *, value, size_t, size) {return path_getxattrat (AT_FDCWD , pathname,AT_SYMLINK_NOFOLLOW , name, - value, size); + value, size,FMODE_PATH ); } SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name, void __user *, value, size_t, size) { -return path_getxattrat (fd,NULL ,AT_EMPTY_PATH , name, value, size); +return path_getxattrat (fd,NULL ,AT_EMPTY_PATH , name, value, size, +FMODE_PATH ); }/*
@@ -974,7 +978,7 @@ ssize_t filename_listxattr(int dfd, struct filename *filename,static ssize_t path_listxattrat (int dfd,const char __user *pathname,unsigned int at_flags,char __user *list, -size_t size) +size_t size,fmode_t mask) {int lookup_flags;
@@ -983,7 +987,7 @@ static ssize_t path_listxattrat(int dfd, const char __user *pathname,CLASS (filename_maybe_null, filename)(pathname, at_flags);if (!filename) { -CLASS (fd, f)(dfd); +CLASS (fd_except, f)(dfd, mask);if (fd_empty (f))return -EBADF ;return file_listxattr (fd_file (f), list, size);
@@ -997,24 +1001,26 @@ SYSCALL_DEFINE5(listxattrat, int, dfd, const char __user *, pathname, unsigned int, at_flags, char __user *, list, size_t, size) { - return path_listxattrat(dfd, pathname, at_flags, list, size); + return path_listxattrat(dfd, pathname, at_flags, list, size, 0); } SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list, size_t, size) { -return path_listxattrat (AT_FDCWD , pathname,0 , list, size); +return path_listxattrat (AT_FDCWD , pathname,0 , list, size,FMODE_PATH ); } SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list, size_t, size) { -return path_listxattrat (AT_FDCWD , pathname,AT_SYMLINK_NOFOLLOW , list, size); +return path_listxattrat (AT_FDCWD , pathname,AT_SYMLINK_NOFOLLOW , list, + size,FMODE_PATH ); } SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size) { -return path_listxattrat (fd,NULL ,AT_EMPTY_PATH , list, size); +return path_listxattrat (fd,NULL ,AT_EMPTY_PATH , list, size, +FMODE_PATH ); }/*
@@ -1065,7 +1071,8 @@ static int filename_removexattr(int dfd, struct filename *filename, }static int path_removexattrat (int dfd,const char __user *pathname, -unsigned int at_flags,const char __user *name) +unsigned int at_flags,const char __user *name, +fmode_t mask) {struct xattr_name kname;unsigned int lookup_flags;
@@ -1080,7 +1087,7 @@ static int path_removexattrat(int dfd, const char __user *pathname,CLASS (filename_maybe_null, filename)(pathname, at_flags);if (!filename) { -CLASS (fd, f)(dfd); +CLASS (fd_except, f)(dfd, mask);if (fd_empty (f))return -EBADF ;return file_removexattr (fd_file (f), &kname);
@@ -1092,24 +1099,25 @@ static int path_removexattrat(int dfd, const char __user *pathname, SYSCALL_DEFINE4(removexattrat, int, dfd, const char __user *, pathname, unsigned int, at_flags, const char __user *, name) { -return path_removexattrat (dfd, pathname, at_flags, name); +return path_removexattrat (dfd, pathname, at_flags, name,0 ); } SYSCALL_DEFINE2(removexattr, const char __user *, pathname, const char __user *, name) { -return path_removexattrat (AT_FDCWD , pathname,0 , name); +return path_removexattrat (AT_FDCWD , pathname,0 , name,FMODE_PATH ); } SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname, const char __user *, name) { -return path_removexattrat (AT_FDCWD , pathname,AT_SYMLINK_NOFOLLOW , name); +return path_removexattrat (AT_FDCWD , pathname,AT_SYMLINK_NOFOLLOW , name, +FMODE_PATH ); } SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name) { -return path_removexattrat (fd,NULL ,AT_EMPTY_PATH , name); +return path_removexattrat (fd,NULL ,AT_EMPTY_PATH , name,FMODE_PATH ); }int xattr_list_one (char **buffer,ssize_t *remaining_size,const char *name)
Signature · 1 line
2.54.0
Christian Brauner
maintainerRe: [PATCH 2/3] xattrat: accept empty O_PATH file descriptors
Quoted 33 lines
Right now, the
setxattrat(),getxattrat(),listxattrat(), andremovexattrat()system calls fail with-EBADFwhen dfd is anO_PATHfile descriptor, pathname is an empty string or NULL, and theAT_EMPTY_PATHflag inat_flagsis set. (Regular non-O_PATH file descriptors are accepted.) This is inconsistent with the behavior of system calls likefstatat()andfchmodat()which do accept those file descriptors.The same operations can also be carried out indirectly by using "/proc/self/fd/<dfd>" as the pathname, but that is only more cumbersome, and for no good reason. Fix that by changing the
xattrat()system calls to accept these file descriptors directly.We stick with the existing practice of leaving the behavior of the
fsetxattr(),fgetxattr(),flistxattr(), andfremovexattr()system calls unchanged: those will still rejectO_PATHfile descriptors.Signed-off-by Andreas Gruenbacher <[email protected]>fs/xattr.c +2
-1@@ -700,7 +700,8 @@ int filename_setxattr(int dfd, struct filename *filename,static int path_setxattrat (int dfd,const char __user *pathname,unsigned int at_flags,const char __user *name, -const void __user *value,size_t size,int flags) +const void __user *value,size_t size,int flags, +fmode_t mask) {struct xattr_name kname;struct kernel_xattr_ctx ctx = {
fs/xattr.c static int path_setxattrat(int dfd, const char __user *pathname, · line 729
Quoted 5 lines
fs/xattr.c +1
-1@@ -725,7 +726,7 @@ static int path_setxattrat(int dfd, const char __user *pathname,CLASS (filename_maybe_null, filename)(pathname, at_flags);if (!filename && dfd >=0 ) { -CLASS (fd, f)(dfd); +CLASS (fd_except, f)(dfd, mask);
So I can live with the O_PATH change, I guess...
but I really dislike fd_except. The code should never bother to pass FMODE_PATH directly which is just rather ugly and we've taken pain to make it work without passing it.
I also don't yet understand why this would be necessary at all.
Signature · 1 line
Christian Brauner <[email protected]>
Re: [PATCH 2/3] xattrat: accept empty O_PATH file descriptors
Quoted 44 lines · Christian Brauner
Quoted 33 lines
Right now, the
setxattrat(),getxattrat(),listxattrat(), andremovexattrat()system calls fail with-EBADFwhen dfd is anO_PATHfile descriptor, pathname is an empty string or NULL, and theAT_EMPTY_PATHflag inat_flagsis set. (Regular non-O_PATH file descriptors are accepted.) This is inconsistent with the behavior of system calls likefstatat()andfchmodat()which do accept those file descriptors.The same operations can also be carried out indirectly by using "/proc/self/fd/<dfd>" as the pathname, but that is only more cumbersome, and for no good reason. Fix that by changing the
xattrat()system calls to accept these file descriptors directly.We stick with the existing practice of leaving the behavior of the
fsetxattr(),fgetxattr(),flistxattr(), andfremovexattr()system calls unchanged: those will still rejectO_PATHfile descriptors.Signed-off-by Andreas Gruenbacher <[email protected]>fs/xattr.c +2
-1@@ -700,7 +700,8 @@ int filename_setxattr(int dfd, struct filename *filename,static int path_setxattrat (int dfd,const char __user *pathname,unsigned int at_flags,const char __user *name, -const void __user *value,size_t size,int flags) +const void __user *value,size_t size,int flags, +fmode_t mask) {struct xattr_name kname;struct kernel_xattr_ctx ctx = {fs/xattr.c static int path_setxattrat(int dfd, const char __user *pathname, · line 729
Quoted 5 lines
fs/xattr.c +1
-1@@ -725,7 +726,7 @@ static int path_setxattrat(int dfd, const char __user *pathname,CLASS (filename_maybe_null, filename)(pathname, at_flags);if (!filename && dfd >=0 ) { -CLASS (fd, f)(dfd); +CLASS (fd_except, f)(dfd, mask);So I can live with the
O_PATHchange, I guess...
but I really dislikefd_except. The code should never bother to passFMODE_PATHdirectly which is just rather ugly and we've taken pain to make it work without passing it.
Quoted 1 line
I also don't yet understand why this would be necessary at all.
So we've got some system calls that should accept O_PATH file descriptors ({get,set,list,remove}xattrat), and others that should not (f{get,set,list,remove}xattr).
We could implement that by using either CLASS(fd) or CLASS(fd_raw) in path_{get,set,list,remove}xattrat depending on which case we want, but that would duplicate unrelated code. To avoid that, this patch took the approach of extending the class with a parameter that defines which file descriptors to accept. But we could also accept all file descriptors and filter out the bad ones later like in the untested patch below. Either approach will get us there; just pick one ...
Thanks,
Andreas
fs/xattr.c +12 -7
@@ -845,7 +845,8 @@ ssize_t filename_getxattr(int dfd, struct filename *filename,static ssize_t path_getxattrat (int dfd,const char __user *pathname,unsigned int at_flags,const char __user *name, -void __user *value,size_t size) +void __user *value,size_t size, +unsigned int reject_flags) {struct xattr_name kname;struct kernel_xattr_ctx ctx = {
@@ -865,10 +866,13 @@ static ssize_t path_getxattrat(int dfd, const char __user *pathname,CLASS (filename_maybe_null, filename)(pathname, at_flags);if (!filename && dfd >=0 ) { -CLASS (fd, f)(dfd); +CLASS (fd_raw, f)(dfd);if (fd_empty (f))return -EBADF ; -return file_getxattr (fd_file (f), &ctx); +struct file *file =fd_file (f); +if (file->f_flags & reject_flags) +return -EBADF ; +return file_getxattr (file, &ctx); }else {int lookup_flags =0 ;if (!(at_flags &AT_SYMLINK_NOFOLLOW ))
@@ -899,26 +903,27 @@ SYSCALL_DEFINE6(getxattrat, int, dfd, const char __user *, pathname, unsigned inreturn -EINVAL ;return path_getxattrat (dfd, pathname, at_flags, name, -u64_to_user_ptr (args.value), args.size); +u64_to_user_ptr (args.value), args.size,0 ); } SYSCALL_DEFINE4(getxattr, const char __user *, pathname, const char __user *, name, void __user *, value, size_t, size) { -return path_getxattrat (AT_FDCWD , pathname,0 , name, value, size); +return path_getxattrat (AT_FDCWD , pathname,0 , name, value, size,0 ); } SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname, const char __user *, name, void __user *, value, size_t, size) {return path_getxattrat (AT_FDCWD , pathname,AT_SYMLINK_NOFOLLOW , name, - value, size); + value, size,0 ); } SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name, void __user *, value, size_t, size) { -return path_getxattrat (fd,NULL ,AT_EMPTY_PATH , name, value, size); +return path_getxattrat (fd,NULL ,AT_EMPTY_PATH , name, value, size, +O_PATH ); }/*
Christian Brauner
maintainerRe: [PATCH 2/3] xattrat: accept empty O_PATH file descriptors
Quoted 45 lines · Andreas Gruenbacher
Quoted 44 lines · Christian Brauner
Quoted 33 lines
Right now, the
setxattrat(),getxattrat(),listxattrat(), andremovexattrat()system calls fail with-EBADFwhen dfd is anO_PATHfile descriptor, pathname is an empty string or NULL, and theAT_EMPTY_PATHflag inat_flagsis set. (Regular non-O_PATH file descriptors are accepted.) This is inconsistent with the behavior of system calls likefstatat()andfchmodat()which do accept those file descriptors.The same operations can also be carried out indirectly by using "/proc/self/fd/<dfd>" as the pathname, but that is only more cumbersome, and for no good reason. Fix that by changing the
xattrat()system calls to accept these file descriptors directly.We stick with the existing practice of leaving the behavior of the
fsetxattr(),fgetxattr(),flistxattr(), andfremovexattr()system calls unchanged: those will still rejectO_PATHfile descriptors.Signed-off-by Andreas Gruenbacher <[email protected]>fs/xattr.c +2
-1@@ -700,7 +700,8 @@ int filename_setxattr(int dfd, struct filename *filename,static int path_setxattrat (int dfd,const char __user *pathname,unsigned int at_flags,const char __user *name, -const void __user *value,size_t size,int flags) +const void __user *value,size_t size,int flags, +fmode_t mask) {struct xattr_name kname;struct kernel_xattr_ctx ctx = {fs/xattr.c static int path_setxattrat(int dfd, const char __user *pathname, · line 729
Quoted 5 lines
fs/xattr.c +1
-1@@ -725,7 +726,7 @@ static int path_setxattrat(int dfd, const char __user *pathname,CLASS (filename_maybe_null, filename)(pathname, at_flags);if (!filename && dfd >=0 ) { -CLASS (fd, f)(dfd); +CLASS (fd_except, f)(dfd, mask);So I can live with the
O_PATHchange, I guess...
but I really dislikefd_except. The code should never bother to passFMODE_PATHdirectly which is just rather ugly and we've taken pain to make it work without passing it.
Quoted 5 lines
Quoted 1 line
I also don't yet understand why this would be necessary at all.
So we've got some system calls that should accept
O_PATHfile descriptors ({get,set,list,remove}xattrat), and others that should not (f{get,set,list,remove}xattr).
I'm very confused why the f*at() variants are supposed to accept O_PATH but the f*() aren't? What's the point? glibc has in multiple cases used the f*at() variant to implement the f*() variant.
Re: [PATCH 2/3] xattrat: accept empty O_PATH file descriptors
Quoted 46 lines · Christian Brauner
Quoted 45 lines · Andreas Gruenbacher
Quoted 44 lines · Christian Brauner
Quoted 33 lines
Right now, the
setxattrat(),getxattrat(),listxattrat(), andremovexattrat()system calls fail with-EBADFwhen dfd is anO_PATHfile descriptor, pathname is an empty string or NULL, and theAT_EMPTY_PATHflag inat_flagsis set. (Regular non-O_PATH file descriptors are accepted.) This is inconsistent with the behavior of system calls likefstatat()andfchmodat()which do accept those file descriptors.The same operations can also be carried out indirectly by using "/proc/self/fd/<dfd>" as the pathname, but that is only more cumbersome, and for no good reason. Fix that by changing the
xattrat()system calls to accept these file descriptors directly.We stick with the existing practice of leaving the behavior of the
fsetxattr(),fgetxattr(),flistxattr(), andfremovexattr()system calls unchanged: those will still rejectO_PATHfile descriptors.Signed-off-by Andreas Gruenbacher <[email protected]>fs/xattr.c +2
-1@@ -700,7 +700,8 @@ int filename_setxattr(int dfd, struct filename *filename,static int path_setxattrat (int dfd,const char __user *pathname,unsigned int at_flags,const char __user *name, -const void __user *value,size_t size,int flags) +const void __user *value,size_t size,int flags, +fmode_t mask) {struct xattr_name kname;struct kernel_xattr_ctx ctx = {fs/xattr.c static int path_setxattrat(int dfd, const char __user *pathname, · line 729
Quoted 5 lines
fs/xattr.c +1
-1@@ -725,7 +726,7 @@ static int path_setxattrat(int dfd, const char __user *pathname,CLASS (filename_maybe_null, filename)(pathname, at_flags);if (!filename && dfd >=0 ) { -CLASS (fd, f)(dfd); +CLASS (fd_except, f)(dfd, mask);So I can live with the
O_PATHchange, I guess...
but I really dislikefd_except. The code should never bother to passFMODE_PATHdirectly which is just rather ugly and we've taken pain to make it work without passing it.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
I also don't yet understand why this would be necessary at all.
So we've got some system calls that should accept
O_PATHfile descriptors ({get,set,list,remove}xattrat), and others that should not (f{get,set,list,remove}xattr).I'm very confused why the f*at() variants are supposed to accept
O_PATHbut the f*() aren't? What's the point?
Oh, I see. To keep things consistent with fchmod() and fchown() vs. fchmodat() and fchownat(): the former two also don't accept O_PATH file descriptors, while the latter two do.
Quoted 1 line
glibc has in multiple cases used the f*at() variant to implement the f*() variant.
Yes, but not the other way around, and keeping the behaviour of the existing f*() calls unchanged seems to be the key idea here. The *at() variants require the AT_EMPTY_PATH flag to be set when operating directly on a file descriptor, so it can't happen by accident.
Thanks,
Andreas
Christian Brauner
maintainerRe: [PATCH 2/3] xattrat: accept empty O_PATH file descriptors
Quoted 10 lines · Andreas Gruenbacher
Quoted 4 lines · Christian Brauner
On 2026-06-30 14:06 +0200, Andreas Gruenbacher wrote:
I'm very confused why the f*at() variants are supposed to accept
O_PATHbut the f*() aren't? What's the point?Oh, I see. To keep things consistent with
fchmod()andfchown()vs.fchmodat()andfchownat(): the former two also don't acceptO_PATHfile descriptors, while the latter two do.
Right, I saw that and wondered about that there too. I guess it makes some sense in that nothing about O_PATH at this point is consistent anymore. Will be fun to document what exactly works and doesn't work on the manpage...
So then I think we should refactor fs/xattr.c similar to how we did it for fch*() and fch*at(). So that f*xattr() call CLASS(fd, ...) and f*xattrat() call CLASS(fd_raw, ...).
This should also make it a lot easier to follow, hopefully.
Re: [PATCH 2/3] xattrat: accept empty O_PATH file descriptors
Quoted 17 lines · Christian Brauner
Quoted 10 lines · Andreas Gruenbacher
Quoted 4 lines · Christian Brauner
On 2026-06-30 14:06 +0200, Andreas Gruenbacher wrote:
I'm very confused why the f*at() variants are supposed to accept
O_PATHbut the f*() aren't? What's the point?Oh, I see. To keep things consistent with
fchmod()andfchown()vs.fchmodat()andfchownat(): the former two also don't acceptO_PATHfile descriptors, while the latter two do.Right, I saw that and wondered about that there too. I guess it makes some sense in that nothing about
O_PATHat this point is consistent anymore. Will be fun to document what exactly works and doesn't work on the manpage...
Quoted 5 lines
So then I think we should refactor
fs/xattr.csimilar to how we did it for fch*() and fch*at(). So that f*xattr() callCLASS(fd, ...)and f*xattrat() callCLASS(fd_raw, ...).This should also make it a lot easier to follow, hopefully.
That works for flistxattr(), but it creates a mess for fgetxattr() and fsetxattr() due to the argument passing; see below.
That argument passing happenss at that level because the lower-level functions filename_{get,set}xattr() and file_{get,set}xattr() are also used by io_uring, which has slightly different argument passing semantics.
The attribute name copying in path_removexattr() could be pushed down into removexattr() because io_uring doesn't use that, but that would make things less orthogonal.
So that's why I've proposed to change path_{get,set,list,remove}xattr() to either handle only non-O_PATH or all file descriptors depending on the caller.
Thanks,
Andreas
fs/xattr.c +63 -9
@@ -725,7 +725,7 @@ static int path_setxattrat(int dfd, const char __user *pathname,CLASS (filename_maybe_null, filename)(pathname, at_flags);if (!filename && dfd >=0 ) { -CLASS (fd, f)(dfd); +CLASS (fd_raw, f)(dfd);if (fd_empty (f)) error = -EBADF ;else
@@ -779,8 +779,30 @@ SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname, SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name, const void __user *,value, size_t, size, int, flags) { -return path_setxattrat (fd,NULL ,AT_EMPTY_PATH , name, - value, size, flags); +struct xattr_name kname; +struct kernel_xattr_ctx ctx = { + .cvalue = value, + .kvalue =NULL , + .size = size, + .kname = &kname, + .flags = flags, + }; +int error; + + error =setxattr_copy (name, &ctx); +if (error) +return error; + +CLASS (fd, f)(fd); + error = -EBADF ; +if (fd_empty (f)) + goto out; + + error =file_setxattr (fd_file (f), &ctx); + +out: +kvfree (ctx.kvalue); +return error; }/*
@@ -865,7 +887,7 @@ static ssize_t path_getxattrat(int dfd, const char __user *pathname,CLASS (filename_maybe_null, filename)(pathname, at_flags);if (!filename && dfd >=0 ) { -CLASS (fd, f)(dfd); +CLASS (fd_raw, f)(dfd);if (fd_empty (f))return -EBADF ;return file_getxattr (fd_file (f), &ctx);
@@ -918,7 +940,24 @@ SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname, SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name, void __user *, value, size_t, size) { -return path_getxattrat (fd,NULL ,AT_EMPTY_PATH , name, value, size); +struct xattr_name kname; +struct kernel_xattr_ctx ctx = { + .value = value, + .size = size, + .kname = &kname, + .flags =0 , + }; +int error; + + error =import_xattr_name (&kname, name); +if (error) +return error; + +CLASS (fd, f)(fd); +if (fd_empty (f)) +return -EBADF ; + +return file_getxattr (fd_file (f), &ctx); }/*
@@ -991,7 +1030,7 @@ static ssize_t path_listxattrat(int dfd, const char __user *pathname,CLASS (filename_maybe_null, filename)(pathname, at_flags);if (!filename) { -CLASS (fd, f)(dfd); +CLASS (fd_raw, f)(dfd);if (fd_empty (f))return -EBADF ;return file_listxattr (fd_file (f), list, size);
@@ -1022,7 +1061,11 @@ SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list, SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size) { -return path_listxattrat (fd,NULL ,AT_EMPTY_PATH , list, size); +CLASS (fd, f)(fd); +if (fd_empty (f)) +return -EBADF ; + +return file_listxattr (fd_file (f), list, size); }/*
@@ -1088,7 +1131,7 @@ static int path_removexattrat(int dfd, const char __user *pathname,CLASS (filename_maybe_null, filename)(pathname, at_flags);if (!filename) { -CLASS (fd, f)(dfd); +CLASS (fd_raw, f)(dfd);if (fd_empty (f))return -EBADF ;return file_removexattr (fd_file (f), &kname);
@@ -1117,7 +1160,18 @@ SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname, SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name) { -return path_removexattrat (fd,NULL ,AT_EMPTY_PATH , name); +struct xattr_name kname; +int error; + + error =import_xattr_name (&kname, name); +if (error) +return error; + +CLASS (fd, f)(fd); +if (fd_empty (f)) +return -EBADF ; + +return file_removexattr (fd_file (f), &kname); }int xattr_list_one (char **buffer,ssize_t *remaining_size,const char *name)
PATCH 3/3 file: consolidate fd cleanup classes using fdget_except
In reply to Andreas GruenbacherCurrently, the fd, fd_except, and fd_raw cleanup classes all use separate init functions, but fdget() and fdget_raw() can be replaced with fdget_except() with negligible overhead.
The only complication is that we now need access to the definition of FMODE_PATH in <linux/file.h>. To make that definition available without including <linux/fs.h>, split the fmode definitions off into a separate <linux/fmode.h> header and include that in both places.
--- drivers/android/binder.c | 2 +- fs/file.c | 18 +----- include/linux/file.h | 11 ++-- include/linux/fmode.h | 120 +++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 116 +------------------------------------ 5 files changed, 131 insertions(+), 136 deletions(-) create mode 100644 include/linux/fmode.h
drivers/android/binder.c +1 -1
@@ -1983,7 +1983,7 @@ struct binder_task_work_cb {* It is not safe to call ksys_close() during the binder_ioctl() * function if there is a chance that binder's own file descriptor * might be closed. This is to meet the requirements for using -* fdget() (see comments for __fget_light()). Therefore use +* fdget_except() (see the comments there). Therefore use * task_work_add() to schedule the close operation once we have * returned from binder_ioctl(). This function is a callback * for that mechanism and does the actual ksys_close() on the
fs/file.c +2 -16
@@ -1179,7 +1179,7 @@ EXPORT_SYMBOL(fget_task_next);* * See also the documentation in rust/kernel/file.rs. */ -static inline struct fd __fget_light (unsigned int fd,fmode_t mask) +struct fd fdget_except (unsigned int fd,fmode_t mask) {struct files_struct *files = current->files;struct file *file;
@@ -1205,21 +1205,7 @@ static inline struct fd __fget_light(unsigned int fd, fmode_t mask)return CLONED_FD (file); } } -struct fd fdget (unsigned int fd) -{ -return __fget_light (fd,FMODE_PATH ); -} -EXPORT_SYMBOL (fdget); - -struct fd fdget_except (unsigned int fd,fmode_t mask) -{ -return __fget_light (fd, mask); -} - -struct fd fdget_raw (unsigned int fd) -{ -return __fget_light (fd,0 ); -} +EXPORT_SYMBOL (fdget_except);/* * Try to avoid f_pos locking. We only need it if the
include/linux/file.h +7 -4
@@ -11,6 +11,7 @@#include <linux/posix_types.h> #include <linux/errno.h> #include <linux/cleanup.h> +#include <linux/fmode.h> #include <linux/err.h> struct file ;
@@ -69,9 +70,11 @@ extern struct file *fget_task(struct task_struct *task, unsigned int fd);extern struct file *fget_task_next (struct task_struct *task,unsigned int *fd);extern void __f_unlock_pos (struct file *); -struct fd fdget (unsigned int fd); -struct fd fdget_except (unsigned int fd,fmode_t mask); -struct fd fdget_raw (unsigned int fd); +struct fd fdget_except (unsigned int fd,fmode_t mode); +static inline struct fd fdget (unsigned int fd) +{ +return fdget_except (fd,FMODE_PATH ); +}struct fd fdget_pos (unsigned int fd);static inline void fdput_pos (struct fd f)
@@ -83,7 +86,7 @@ static inline void fdput_pos(struct fd f) DEFINE_CLASS(fd, struct fd, fdput(_T), fdget(fd), int fd) DEFINE_CLASS(fd_except, struct fd, fdput(_T), fdget_except(fd, mask), int fd, fmode_t mask) -DEFINE_CLASS(fd_raw, struct fd, fdput(_T), fdget_raw(fd), int fd) +DEFINE_CLASS(fd_raw, struct fd, fdput(_T), fdget_except(fd, 0), int fd) DEFINE_CLASS(fd_pos, struct fd, fdput_pos(_T), fdget_pos(fd), int fd)extern int f_dupfd (unsigned int from,struct file *file,unsigned flags);
include/linux/fmode.h new file +120 -0
120 lines not on this page · raw message
include/linux/fs.h +1 -115
128 lines not on this page · raw message
Signature · 1 line
2.54.0
Christian Brauner
maintainerQuoted 4 lines · Andreas Gruenbacher
CLASS(fd_except, f)(fd, mask) allows to specify whether to accept or rejectO_PATHfile descriptors by passing a mode value of 0 orFMODE_PATHrather than using a specific cleanup class for each case (CLASS(fd, f)(fd) orCLASS(fd_raw, f)(fd)).
My main objection to such patches continues to be that it keeps watering down the meaning of O_PATH if it continues being usable for performing ever more actions including write-like operations like setting extended attributes.
Christian,
Quoted 10 lines · Christian Brauner
Quoted 4 lines · Andreas Gruenbacher
CLASS(fd_except, f)(fd, mask) allows to specify whether to accept or rejectO_PATHfile descriptors by passing a mode value of 0 orFMODE_PATHrather than using a specific cleanup class for each case (CLASS(fd, f)(fd) orCLASS(fd_raw, f)(fd)).My main objection to such patches continues to be that it keeps watering down the meaning of
O_PATHif it continues being usable for performing ever more actions including write-like operations like setting extended attributes.
I see where you're going, but let me argue that extended attributes are different from file data. For example, the "system.posix_acl_access" and "system.posix_acl_default" attributes are about file permissions. They are an extension of the classical file mode permission bits which are accessed via stat*() and chmod*(), operations which accept O_PATH file descriptors today.
The "security.selinux" attribute is in a similar category as "system.posix_acl_access" and "system.posix_acl_default".
The "trusted.*" namespace is proctected by CAP_SYS_ADMIN, so it's obviously also not about file data like information.
The "user.*" namespace may appear different because access is guarded by the file permissions. In that sense, you could say that setting "user.*" attributes is a bit more like writing to the file. It's still only arbitrary information attached to the inode itself, though. For example, operations on "user.*" attributes affect the ctime (when changing), but never the atime or mtime.
Also, you're clearly aware that the same operations are possible via "/proc/self/fd/<num>" today, just in a messy way. The exception are environments that don't make /proc available. It seems fairly bizarre to me to make O_PATH file descriptors available to environments restricted in that way in the first place. And that's the only case in which processes would effectively gain new abilities.
By the way, I forgot to mention previously that this patch set is related to the following change:
5daeb41a6fc9 ("fchmodat2: add support for AT_EMPTY_PATH")
Thanks,
Andreas
Christian Brauner
maintainerQuoted 34 lines · Andreas Gruenbacher
Christian,
Quoted 10 lines · Christian Brauner
Quoted 4 lines · Andreas Gruenbacher
CLASS(fd_except, f)(fd, mask) allows to specify whether to accept or rejectO_PATHfile descriptors by passing a mode value of 0 orFMODE_PATHrather than using a specific cleanup class for each case (CLASS(fd, f)(fd) orCLASS(fd_raw, f)(fd)).My main objection to such patches continues to be that it keeps watering down the meaning of
O_PATHif it continues being usable for performing ever more actions including write-like operations like setting extended attributes.I see where you're going, but let me argue that extended attributes are different from file data. For example, the "system.posix_acl_access" and "system.posix_acl_default" attributes are about file permissions. They are an extension of the classical file mode permission bits which are accessed via stat*() and chmod*(), operations which accept
O_PATHfile descriptors today.The "security.selinux" attribute is in a similar category as "system.posix_acl_access" and "system.posix_acl_default".
The "trusted.*" namespace is proctected by
CAP_SYS_ADMIN, so it's obviously also not about file data like information.The "user.*" namespace may appear different because access is guarded by the file permissions. In that sense, you could say that setting "user.*" attributes is a bit more like writing to the file. It's still only arbitrary information attached to the inode itself, though. For example, operations on "user.*" attributes affect the ctime (when changing), but never the atime or mtime.
Quoted 2 lines
Also, you're clearly aware that the same operations are possible via "/proc/self/fd/<num>" today, just in a messy way. The exception are
Yes, very aware and I hate it. I wish we had upgrade masks but that's is very very hairy work. :)
Quoted 9 lines
environments that don't make /proc available. It seems fairly bizarre to me to make
O_PATHfile descriptors available to environments restricted in that way in the first place. And that's the only case in which processes would effectively gain new abilities.By the way, I forgot to mention previously that this patch set is related to the following change:
5daeb41a6fc9("fchmodat2: add support forAT_EMPTY_PATH")
Yeah, I did merge that because of fchown*() parity. Let me sit on it for a few days to mull it over.