fs: Consider capabilities relative to namespace for linkat permission check
cover fs: Consider capabilities relative to namespace for linkat permission check
This is a one line change that makes linkat aware of namespaces when checking for capabilities.
As far as I can tell, the call to capable in this code dates back to before the ns_capable function existed, so I don't think the author specifically intended to prefer regular capable over ns_capable, and no one has noticed or cared to change it yet... until now!
It is already hard enough to use linkat to link temporarily files into the filesystem without the /proc workaround, and when moving a program that was working fine on bare metal into a container, I got hung up on this additional snag due to the lack of namespace awareness in linkat.
Charles Mirabile (1):
fs: Consider capabilities relative to namespace for linkat permission
check
fs/namei.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
base-commit: 89cdf9d556016a54ff6ddd62324aa5ec790c05cc
Signature · 1 line
2.38.1
PATCH 1/1 fs: Consider capabilities relative to namespace for linkat permission check
Previously, the check for CAP_DAC_READ_SEARCH when trying to use AT_EMPTY_PATH happened relative to the init process's namespace rather than the namespace of the current process. This seems like an oversight because it meant that processes in new namespaces could not ever use AT_EMPTY_PATH with linkat even if they have CAP_DAC_READ_SEARCH within their namespace.
--- fs/namei.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/namei.c +1 -1
@@ -4628,7 +4628,7 @@ int do_linkat(int olddfd, struct filename *old, int newdfd,* This ensures that not everyone will be able to create * handlink using the passed filedescriptor. */ -if (flags &AT_EMPTY_PATH && !capable (CAP_DAC_READ_SEARCH )) { +if (flags &AT_EMPTY_PATH && !ns_capable (current_user_ns (),CAP_DAC_READ_SEARCH )) { error = -ENOENT ; goto out_putnames; }
Signature · 1 line
2.38.1
Christian Brauner
maintainerQuoted 8 lines · Charles Mirabile
This is a one line change that makes
linkataware of namespaces when checking for capabilities.As far as I can tell, the call to
capablein this code dates back to before thens_capablefunction existed, so I don't think the author specifically intended to prefer regularcapableoverns_capable, and no one has noticed or cared to change it yet... until now!
Quoted 5 lines
It is already hard enough to use
linkatto link temporarily files into the filesystem without the/procworkaround, and when moving a program that was working fine on bare metal into a container, I got hung up on this additional snag due to the lack of namespace awareness inlinkat.
I agree that it would be nice to relax this a bit to make this play nicer with containers.
The current checks want to restrict scenarios where an application is able to create a hardlink for an arbitrary file descriptor it has received via e.g., SCM_RIGHTS or that it has inherited.
So we want to somehow get a good enough approximation to the question whether the caller would have been able to open the source file.
When we check for CAP_DAC_READ_SEARCH in the caller's namespace we presuppose that the file is accessible in the current namespace and that CAP_DAC_READ_SEARCH would have been enough to open it. Both aren't necessarily true. Neither need the file be accessible, e.g., due to a chroot or pivot_root nor need CAP_DAC_READ_SEARCH be enough. For example, the file could be accessible in the caller's namespace but due to uid/gid mapping the {g,u}id of the file doesn't have a mapping in the caller's namespace. So that doesn't really cut it imho.
However, if we check for CAP_DAC_READ_SEARCH in the namespace the file was opened in that could work. We know that the file must've been accessible in the namespace the file was opened in and we know that the {g,u}id of the file must have been mapped in the namespace the file was opened in. So if we check that the caller does have CAP_DAC_READ_SEARCH in the opener's namespace we can approximate that the caller could've opened the file.
So that should allow us to enabled this for containers.
Quoted 16 lines · Christian Brauner
Quoted 8 lines · Charles Mirabile
This is a one line change that makes
linkataware of namespaces when checking for capabilities.As far as I can tell, the call to
capablein this code dates back to before thens_capablefunction existed, so I don't think the author specifically intended to prefer regularcapableoverns_capable, and no one has noticed or cared to change it yet... until now!Quoted 5 lines
It is already hard enough to use
linkatto link temporarily files into the filesystem without the/procworkaround, and when moving a program that was working fine on bare metal into a container, I got hung up on this additional snag due to the lack of namespace awareness inlinkat.
Quoted 6 lines
I agree that it would be nice to relax this a bit to make this play nicer with containers.
The current checks want to restrict scenarios where an application is able to create a hardlink for an arbitrary file descriptor it has received via e.g.,
SCM_RIGHTSor that it has inherited.
Makes sense.
Quoted 4 lines
So we want to somehow get a good enough approximation to the question whether the caller would have been able to open the source file.
Quoted 8 lines
When we check for
CAP_DAC_READ_SEARCHin the caller's namespace we presuppose that the file is accessible in the current namespace and thatCAP_DAC_READ_SEARCHwould have been enough to open it. Both aren't necessarily true. Neither need the file be accessible, e.g., due to a chroot orpivot_rootnor needCAP_DAC_READ_SEARCHbe enough. For example, the file could be accessible in the caller's namespace but due to uid/gid mapping the {g,u}id of the file doesn't have a mapping in the caller's namespace. So that doesn't really cut it imho.
Good point.
Quoted 8 lines
However, if we check for
CAP_DAC_READ_SEARCHin the namespace the file was opened in that could work. We know that the file must've been accessible in the namespace the file was opened in and we know that the {g,u}id of the file must have been mapped in the namespace the file was opened in. So if we check that the caller does haveCAP_DAC_READ_SEARCHin the opener's namespace we can approximate that the caller could've opened the file.
Would that be the namespace pointed to by ->f_cred->user_ns on the struct file corresponding to the fd?
If so is there a better way to surface that struct file for checking than this? error=-ENOENT;
if(flags & AT_EMPTY_PATH && !old->name[0]) {
struct file *file = fget(oldfd);
bool capable = ns_capable(file->f_cred->user_ns, CAP_DAC_READ_SEARCH);
fput(file);
if(!capable)
goto out_putnames;
}Quoted 3 lines
So that should allow us to enabled this for containers.
Best - Charlie
Christian Brauner
maintainerQuoted 45 lines · Charles Mirabile
Quoted 16 lines · Christian Brauner
Quoted 8 lines · Charles Mirabile
This is a one line change that makes
linkataware of namespaces when checking for capabilities.As far as I can tell, the call to
capablein this code dates back to before thens_capablefunction existed, so I don't think the author specifically intended to prefer regularcapableoverns_capable, and no one has noticed or cared to change it yet... until now!Quoted 5 lines
It is already hard enough to use
linkatto link temporarily files into the filesystem without the/procworkaround, and when moving a program that was working fine on bare metal into a container, I got hung up on this additional snag due to the lack of namespace awareness inlinkat.Quoted 6 lines
I agree that it would be nice to relax this a bit to make this play nicer with containers.
The current checks want to restrict scenarios where an application is able to create a hardlink for an arbitrary file descriptor it has received via e.g.,
SCM_RIGHTSor that it has inherited.Makes sense.
Quoted 4 lines
So we want to somehow get a good enough approximation to the question whether the caller would have been able to open the source file.
Quoted 8 lines
When we check for
CAP_DAC_READ_SEARCHin the caller's namespace we presuppose that the file is accessible in the current namespace and thatCAP_DAC_READ_SEARCHwould have been enough to open it. Both aren't necessarily true. Neither need the file be accessible, e.g., due to a chroot orpivot_rootnor needCAP_DAC_READ_SEARCHbe enough. For example, the file could be accessible in the caller's namespace but due to uid/gid mapping the {g,u}id of the file doesn't have a mapping in the caller's namespace. So that doesn't really cut it imho.Good point.
Quoted 8 lines
However, if we check for
CAP_DAC_READ_SEARCHin the namespace the file was opened in that could work. We know that the file must've been accessible in the namespace the file was opened in and we know that the {g,u}id of the file must have been mapped in the namespace the file was opened in. So if we check that the caller does haveCAP_DAC_READ_SEARCHin the opener's namespace we can approximate that the caller could've opened the file.
Quoted 2 lines
Would that be the namespace pointed to by
->f_cred->user_nson the struct file corresponding to the fd?
Yes.
Quoted 9 lines
If so is there a better way to surface that struct file for checking than this? error=-ENOENT;
if(flags & AT_EMPTY_PATH && !old->name[0]) { struct file *file = fget(oldfd); bool capable = ns_capable(file->f_cred->user_ns, CAP_DAC_READ_SEARCH); fput(file); if(!capable) goto out_putnames;
Two observations:
(1) The current do_linkat() has a bug when the caller passes AT_EMPTY_PATH with an absolute path. In that case getname_uflags() will handle this fine and ignore AT_EMPTY_PATH but do_linkat() will perform the capable() check even though the path isn't empty.
(2) io_uring uses do_linkat() but doesn't actually support AT_EMPTY_PATH because it calls getname(), not getname_flags(). Maybe that's intentional. In any case, it would need a separate patch for io_uring to enable that functionality.
I think for your case you need something like the below where you bubble up whether the path is empty from getname_flags() and pass that to do_linkat() which can then perform the privilege check only for the case where the source path is actually empty. That will also fix (1).
(not even compile tested)
fs/internal.h +1 -1
@@ -61,7 +61,7 @@ int do_renameat2(int olddfd, struct filename *oldname, int newdfd,int do_mkdirat (int dfd,struct filename *name,umode_t mode);int do_symlinkat (struct filename *from,int newdfd,struct filename *to);int do_linkat (int olddfd,struct filename *old,int newdfd, -struct filename *new,int flags); +struct filename *new,int flags,bool is_empty);/* * namespace.c
fs/namei.c +37 -17
83 lines not on this page · raw message
io_uring/fs.c +1 -1
@@ -277,7 +277,7 @@ int io_linkat(struct io_kiocb *req, unsigned int issue_flags)WARN_ON_ONCE (issue_flags &IO_URING_F_NONBLOCK ); ret =do_linkat (lnk->old_dfd, lnk->oldpath, lnk->new_dfd, - lnk->newpath, lnk->flags); + lnk->newpath, lnk->flags, false); req->flags &= ~REQ_F_NEED_CLEANUP ;io_req_set_res (req, ret,0 );
Signature · 1 line
2.34.1