ns: support file handles
Subsystems (12)
Christian Brauner
maintainercover ns: support file handles
For a while now we have supported file handles for pidfds. This has proven to be very useful.
Extend the concept to cover namespaces as well. After this patchset it is possible to encode and decode namespace file handles using the commong name_to_handle_at() and open_by_handle_at() apis.
Namespaces file descriptors can already be derived from pidfds which means they aren't subject to overmount protection bugs. IOW, it's irrelevant if the caller would not have access to an appropriate /proc/<pid>/ns/ directory as they could always just derive the namespace based on a pidfd already.
It has the same advantage as pidfds. It's possible to reliably and for the lifetime of the system refer to a namespace without pinning any resources and to compare them.
Permission checking is kept simple. If the caller is located in the namespace the file handle refers to they are able to open it otherwise they must hold privilege over the owning namespace of the relevant namespace.
Both the network namespace and the mount namespace already have an associated cookie that isn't recycled and is fully exposed to userspace. Move this into ns_common and use the same id space for all namespaces so they can trivially and reliably be compared.
There's more coming based on the iterator infrastructure but the series is large enough and focuses on file handles.
Extensive selftests included. I still have various other test-suites to run but it holds up so far.
---
Christian Brauner (32):
pidfs: validate extensible ioctls
nsfs: validate extensible ioctls
block: use extensible_ioctl_valid()
ns: move to_ns_common() to ns_common.h
nsfs: add nsfs.h header
ns: uniformly initialize ns_common
mnt: use ns_common_init()
ipc: use ns_common_init()
cgroup: use ns_common_init()
pid: use ns_common_init()
time: use ns_common_init()
uts: use ns_common_init()
user: use ns_common_init()
net: use ns_common_init()
ns: remove ns_alloc_inum()
nstree: make iterator generic
mnt: support iterator
cgroup: support iterator
ipc: support iterator
net: support iterator
pid: support iterator
time: support iterator
userns: support iterator
uts: support iterator
ns: add to_<type>_ns() to respective headers
nsfs: add current_in_namespace()
nsfs: support file handles
nsfs: support exhaustive file handles
nsfs: add missing id retrieval support
tools: update nsfs.h uapi header selftests/namespaces: add identifier selftests selftests/namespaces: add file handle selftests
block/blk-integrity.c | 8 +- fs/fhandle.c | 6 + fs/internal.h | 1 + fs/mount.h | 10 +- fs/namespace.c | 156 +-- fs/nsfs.c | 266 +++- fs/pidfs.c | 2 +- include/linux/cgroup.h | 5 + include/linux/exportfs.h | 6 + include/linux/fs.h | 14 + include/linux/ipc_namespace.h | 5 + include/linux/ns_common.h | 29 + include/linux/nsfs.h | 40 + include/linux/nsproxy.h | 11 - include/linux/nstree.h | 89 ++ include/linux/pid_namespace.h | 5 + include/linux/proc_ns.h | 32 +- include/linux/time_namespace.h | 9 + include/linux/user_namespace.h | 5 + include/linux/utsname.h | 5 + include/net/net_namespace.h | 6 + include/uapi/linux/fcntl.h | 1 + include/uapi/linux/nsfs.h | 12 +- init/main.c | 2 + ipc/msgutil.c | 1 + ipc/namespace.c | 12 +- ipc/shm.c | 2 + kernel/Makefile | 2 +- kernel/cgroup/cgroup.c | 2 + kernel/cgroup/namespace.c | 24 +- kernel/nstree.c | 233 ++++ kernel/pid_namespace.c | 13 +- kernel/time/namespace.c | 23 +- kernel/user_namespace.c | 17 +- kernel/utsname.c | 28 +- net/core/net_namespace.c | 59 +- tools/include/uapi/linux/nsfs.h | 23 +- tools/testing/selftests/namespaces/.gitignore | 2 + tools/testing/selftests/namespaces/Makefile | 7 + tools/testing/selftests/namespaces/config | 7 + .../selftests/namespaces/file_handle_test.c | 1410 ++++++++++++++++++++ tools/testing/selftests/namespaces/nsid_test.c | 986 ++++++++++++++ 42 files changed, 3306 insertions(+), 270 deletions(-)
---
base-commit: 8f5ae30d69d7543eee0d70083daf4de8fe15d585
change-id: 20250905-work-namespace-c68826dda0d4
Christian Brauner
maintainerPATCH 1/32 pidfs: validate extensible ioctls
Validate extensible ioctls stricter than we do now.
--- fs/pidfs.c | 2 +- include/linux/fs.h | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-)
fs/pidfs.c +1 -1
@@ -440,7 +440,7 @@ static bool pidfs_ioctl_valid(unsigned int cmd)* erronously mistook the file descriptor for a pidfd. * This is not perfect but will catch most cases. */ -return (_IOC_TYPE (cmd) ==_IOC_TYPE (PIDFD_GET_INFO )); +return extensible_ioctl_valid (cmd,PIDFD_GET_INFO ,PIDFD_INFO_SIZE_VER0 ); }return false;
include/linux/fs.h +14 -0
@@ -4023,4 +4023,18 @@ static inline bool vfs_empty_path(int dfd, const char __user *path)int generic_atomic_write_valid (struct kiocb *iocb,struct iov_iter *iter); +static inline bool extensible_ioctl_valid (unsigned int cmd_a, +unsigned int cmd_b,size_t min_size) +{ +if (_IOC_DIR (cmd_a) !=_IOC_DIR (cmd_b)) +return false; +if (_IOC_TYPE (cmd_a) !=_IOC_TYPE (cmd_b)) +return false; +if (_IOC_NR (cmd_a) !=_IOC_NR (cmd_b)) +return false; +if (_IOC_SIZE (cmd_a) < min_size) +return false; +return true; +} +#endif /* _LINUX_FS_H */
Signature · 1 line
2.47.3
Re: [PATCH 01/32] pidfs: validate extensible ioctls
Quoted 3 lines · Christian Brauner
Validate extensible ioctls stricter than we do now.
Signed-off-by Christian Brauner <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 45 lines
--- fs/pidfs.c | 2 +- include/linux/fs.h | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-)fs/pidfs.c +1
-1@@ -440,7 +440,7 @@ static bool pidfs_ioctl_valid(unsigned int cmd)* erronously mistook the file descriptor for a pidfd. * This is not perfect but will catch most cases. */ -return (_IOC_TYPE (cmd) ==_IOC_TYPE (PIDFD_GET_INFO )); +return extensible_ioctl_valid (cmd,PIDFD_GET_INFO ,PIDFD_INFO_SIZE_VER0 ); }return false;include/linux/fs.h +14
-0@@ -4023,4 +4023,18 @@ static inline bool vfs_empty_path(int dfd, const char __user *path)int generic_atomic_write_valid (struct kiocb *iocb,struct iov_iter *iter); +static inline bool extensible_ioctl_valid (unsigned int cmd_a, +unsigned int cmd_b,size_t min_size) +{ +if (_IOC_DIR (cmd_a) !=_IOC_DIR (cmd_b)) +return false; +if (_IOC_TYPE (cmd_a) !=_IOC_TYPE (cmd_b)) +return false; +if (_IOC_NR (cmd_a) !=_IOC_NR (cmd_b)) +return false; +if (_IOC_SIZE (cmd_a) < min_size) +return false; +return true; +} +#endif /* _LINUX_FS_H */ Signature · 2 lines
-- 2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH 01/32] pidfs: validate extensible ioctls
In reply to Christian BraunerQuoted 33 lines · Christian Brauner
Validate extensible ioctls stricter than we do now.
Signed-off-by Christian Brauner <[email protected]>--- fs/pidfs.c | 2 +- include/linux/fs.h | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-)fs/pidfs.c +1
-1@@ -440,7 +440,7 @@ static bool pidfs_ioctl_valid(unsigned int cmd)* erronously mistook the file descriptor for a pidfd. * This is not perfect but will catch most cases. */ -return (_IOC_TYPE (cmd) ==_IOC_TYPE (PIDFD_GET_INFO )); +return extensible_ioctl_valid (cmd,PIDFD_GET_INFO ,PIDFD_INFO_SIZE_VER0 ); }return false;include/linux/fs.h +4
-0@@ -4023,4 +4023,18 @@ static inline bool vfs_empty_path(int dfd, const char __user *path)int generic_atomic_write_valid (struct kiocb *iocb,struct iov_iter *iter); +static inline bool extensible_ioctl_valid (unsigned int cmd_a, +unsigned int cmd_b,size_t min_size) +{ +if (_IOC_DIR (cmd_a) !=_IOC_DIR (cmd_b))
include/linux/fs.h static inline bool vfs_empty_path(int dfd, const char __user *path) · line 4039
Quoted 10 lines
include/linux/fs.h +10
-0+return false; +if (_IOC_TYPE (cmd_a) !=_IOC_TYPE (cmd_b)) +return false; +if (_IOC_NR (cmd_a) !=_IOC_NR (cmd_b)) +return false; +if (_IOC_SIZE (cmd_a) < min_size) +return false; +return true; +} +
nit: I know only we use them for now, but does this maybe belong in ioctl.h (or even uaccess.h with the other extensible struct stuff)?
Otherwise,
Quoted 5 lines
include/linux/fs.h +0
-0#endif /* _LINUX_FS_H */ Signature · 2 lines
-- 2.47.3
Signature · 4 lines
Aleksa Sarai Senior Software Engineer (Containers) SUSE Linux GmbH https://www.cyphar.com/
Re: [PATCH 01/32] pidfs: validate extensible ioctls
In reply to Christian BraunerQuoted 13 lines · Christian Brauner
Validate extensible ioctls stricter than we do now.
Signed-off-by Christian Brauner <[email protected]>--- fs/pidfs.c | 2 +- include/linux/fs.h | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-)fs/pidfs.c +0
-0
fs/pidfs.c static bool pidfs_ioctl_valid(unsigned int cmd) · line 443
Quoted 5 lines
fs/pidfs.c +1
-1@@ -440,7 +440,7 @@ static bool pidfs_ioctl_valid(unsigned int cmd)* erronously mistook the file descriptor for a pidfd. * This is not perfect but will catch most cases. */ -return (_IOC_TYPE (cmd) ==_IOC_TYPE (PIDFD_GET_INFO )); +return extensible_ioctl_valid (cmd,PIDFD_GET_INFO ,PIDFD_INFO_SIZE_VER0 );
Hi,
this turned EINVAL (from pidfd_info()) into ENOTTY (from pidfd_ioctl()) for at least LTP's:
struct pidfd_info_invalid {
uint32_t dummy;
};#define PIDFD_GET_INFO_SHORT _IOWR(PIDFS_IOCTL_MAGIC, 11, struct pidfd_info_invalid)
ioctl(pidfd, PIDFD_GET_INFO_SHORT, info_invalid) == EINVAL
Is this expected?
Quoted 12 lines
include/linux/fs.h +5
-0@@ -4023,4 +4023,18 @@ static inline bool vfs_empty_path(int dfd, const char __user *path)int generic_atomic_write_valid (struct kiocb *iocb,struct iov_iter *iter); +static inline bool extensible_ioctl_valid (unsigned int cmd_a, +unsigned int cmd_b,size_t min_size) +{ +if (_IOC_DIR (cmd_a) !=_IOC_DIR (cmd_b)) +return false;
include/linux/fs.h static inline bool vfs_empty_path(int dfd, const char __user *path) · line 4040
Quoted 10 lines
include/linux/fs.h +9
-0+if (_IOC_TYPE (cmd_a) !=_IOC_TYPE (cmd_b)) +return false; +if (_IOC_NR (cmd_a) !=_IOC_NR (cmd_b)) +return false; +if (_IOC_SIZE (cmd_a) < min_size) +return false; + return true; +} +#endif /* _LINUX_FS_H */
thanks,
Signature · 2 lines
js suse labs
Jan Kara
reviewerRe: [PATCH 01/32] pidfs: validate extensible ioctls
Quoted 31 lines · Jiri Slaby
Quoted 13 lines · Christian Brauner
Validate extensible ioctls stricter than we do now.
Signed-off-by Christian Brauner <[email protected]>--- fs/pidfs.c | 2 +- include/linux/fs.h | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-)fs/pidfs.c +0
-0fs/pidfs.c static bool pidfs_ioctl_valid(unsigned int cmd) · line 443
Quoted 5 lines
fs/pidfs.c +1
-1@@ -440,7 +440,7 @@ static bool pidfs_ioctl_valid(unsigned int cmd)* erronously mistook the file descriptor for a pidfd. * This is not perfect but will catch most cases. */ -return (_IOC_TYPE (cmd) ==_IOC_TYPE (PIDFD_GET_INFO )); +return extensible_ioctl_valid (cmd,PIDFD_GET_INFO ,PIDFD_INFO_SIZE_VER0 );Hi,
this turned EINVAL (from
pidfd_info()) into ENOTTY (frompidfd_ioctl()) for at least LTP's:struct pidfd_info_invalid { uint32_t dummy; };#define
PIDFD_GET_INFO_SHORT_IOWR(PIDFS_IOCTL_MAGIC, 11, struct pidfd_info_invalid)
Quoted 6 lines
ioctl(pidfd, PIDFD_GET_INFO_SHORT, info_invalid)== EINVALIs this expected?
We already discussed this internally but for others the problem was discussed here [1] and we decided the new errno value is OK and LTP test is being fixed up.
Honza
[1] https://lore.kernel.org/all/[email protected]/
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Stability of ioctl constants in the UAPI (Re: [PATCH 01/32] pidfs: validate extensible ioctls)
In reply to Christian Brauner* Christian Brauner:
Quoted 34 lines
Validate extensible ioctls stricter than we do now.
Signed-off-by Christian Brauner <[email protected]>--- fs/pidfs.c | 2 +- include/linux/fs.h | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-)fs/pidfs.c +1
-1@@ -440,7 +440,7 @@ static bool pidfs_ioctl_valid(unsigned int cmd)* erronously mistook the file descriptor for a pidfd. * This is not perfect but will catch most cases. */ -return (_IOC_TYPE (cmd) ==_IOC_TYPE (PIDFD_GET_INFO )); +return extensible_ioctl_valid (cmd,PIDFD_GET_INFO ,PIDFD_INFO_SIZE_VER0 ); }return false;include/linux/fs.h +5
-0@@ -4023,4 +4023,18 @@ static inline bool vfs_empty_path(int dfd, const char __user *path)int generic_atomic_write_valid (struct kiocb *iocb,struct iov_iter *iter); +static inline bool extensible_ioctl_valid (unsigned int cmd_a, +unsigned int cmd_b,size_t min_size) +{ +if (_IOC_DIR (cmd_a) !=_IOC_DIR (cmd_b)) +return false;
include/linux/fs.h static inline bool vfs_empty_path(int dfd, const char __user *path) · line 4040
Quoted 10 lines
include/linux/fs.h +9
-0+if (_IOC_TYPE (cmd_a) !=_IOC_TYPE (cmd_b)) +return false; +if (_IOC_NR (cmd_a) !=_IOC_NR (cmd_b)) +return false; +if (_IOC_SIZE (cmd_a) < min_size) +return false; + return true; +} +#endif /* _LINUX_FS_H */
Is this really the right direction? This implies that the ioctl constants change as the structs get extended. At present, this impacts struct pidfd_info and PIDFD_GET_INFO.
I think this is a deparature from the previous design, where (low-level) userspace did not have not worry about the internal structure of ioctl commands and could treat them as opaque bit patterns. With the new approach, we have to dissect some of the commands in the same way extensible_ioctl_valid does it above.
So far, this impacts glibc ABI tests. Looking at the strace sources, it doesn't look to me as if the ioctl handler is prepared to deal with this situation, either, because it uses the full ioctl command for lookups.
The sanitizers could implement generic ioctl checking with the embedded size information in the ioctl command, but the current code structure is not set up to handle this because it's indexed by the full ioctl command, not the type. I think in some cases, the size is required to disambiguate ioctl commands because the type field is not unique across devices. In some cases, the sanitizers would have to know the exact command (not just the size), to validate points embedded in the struct passed to the ioctl. So I don't think changing ioctl constants when extensible structs change is obviously beneficial to the sanitizers, either.
I would prefer if the ioctl commands could be frozen and decoupled from the structs. As far as I understand it, there is no requirement that the embedded size matches what the kernel deals with.
Thanks,
Florian
Re: Stability of ioctl constants in the UAPI (Re: [PATCH 01/32] pidfs: validate extensible ioctls)
Quoted 43 lines · Florian Weimer
* Christian Brauner:
Quoted 40 lines
Validate extensible ioctls stricter than we do now.
Signed-off-by Christian Brauner <[email protected]>--- fs/pidfs.c | 2 +- include/linux/fs.h | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-)fs/pidfs.c +1
-1@@ -440,7 +440,7 @@ static bool pidfs_ioctl_valid(unsigned int cmd)* erronously mistook the file descriptor for a pidfd. * This is not perfect but will catch most cases. */ -return (_IOC_TYPE (cmd) ==_IOC_TYPE (PIDFD_GET_INFO )); +return extensible_ioctl_valid (cmd,PIDFD_GET_INFO ,PIDFD_INFO_SIZE_VER0 ); }return false;include/linux/fs.h +11
-0@@ -4023,4 +4023,18 @@ static inline bool vfs_empty_path(int dfd, const char __user *path)int generic_atomic_write_valid (struct kiocb *iocb,struct iov_iter *iter); +static inline bool extensible_ioctl_valid (unsigned int cmd_a, +unsigned int cmd_b,size_t min_size) +{ +if (_IOC_DIR (cmd_a) !=_IOC_DIR (cmd_b)) +return false; +if (_IOC_TYPE (cmd_a) !=_IOC_TYPE (cmd_b)) +return false; +if (_IOC_NR (cmd_a) !=_IOC_NR (cmd_b)) +return false; +if (_IOC_SIZE (cmd_a) < min_size) +return false;
Quoted 8 lines
include/linux/fs.h static inline bool vfs_empty_path(int dfd, const char __user *path) · line 4040
Quoted 4 lines
include/linux/fs.h +3
-0+ return true; +} +#endif /* _LINUX_FS_H */ Is this really the right direction? This implies that the ioctl constants change as the structs get extended. At present, this impacts struct
pidfd_infoandPIDFD_GET_INFO.
Well, some classes of ioctls are indeed designed like that (drm comes to mind), but I agree that it is something that preferably should be decided at the initial interface design stage (including the constants, for example, evdev is probably a good example of handling them), as otherwise there will be userspace code that assumes that the size doesn't change.
Quoted 9 lines
I think this is a deparature from the previous design, where (low-level) userspace did not have not worry about the internal structure of ioctl commands and could treat them as opaque bit patterns. With the new approach, we have to dissect some of the commands in the same way
extensible_ioctl_validdoes it above.So far, this impacts glibc ABI tests. Looking at the strace sources, it doesn't look to me as if the ioctl handler is prepared to deal with this situation, either, because it uses the full ioctl command for lookups.
strace usually handles this opportunistically, but indeed ioctls which don't have fixed size across kernel version require additional care in terms of handling, decoding, and printing.
Quoted 6 lines
The sanitizers could implement generic ioctl checking with the embedded size information in the ioctl command, but the current code structure is not set up to handle this because it's indexed by the full ioctl command, not the type. I think in some cases, the size is required to disambiguate ioctl commands because the type field is not unique across devices.
In terms of disambiguation, one needs to look at the fd in question, and even that is oftentimes not enough (again, in drm, one needs to figure out what driver backs a specific fd in order to determine the ioctl semantics properly).
Quoted 17 lines
In some cases, the sanitizers would have to know the exact command (not just the size), to validate points embedded in the struct passed to the ioctl. So I don't think changing ioctl constants when extensible structs change is obviously beneficial to the sanitizers, either.
I would prefer if the ioctl commands could be frozen and decoupled from the structs. As far as I understand it, there is no requirement that the embedded size matches what the kernel deals with.
Thanks,
Florian--
Strace-devel mailing list
[email protected]
https://lists.strace.io/mailman/listinfo/strace-devel
Signature · 3 lines
Eugene Syromyatnikov mailto:[email protected] xmpp:esyr@jabber.{ru|org}
Re: Stability of ioctl constants in the UAPI (Re: [PATCH 01/32] pidfs: validate extensible ioctls)
In reply to Florian WeimerHi,
Quoted 14 lines · Florian Weimer
Is this really the right direction? This implies that the ioctl constants change as the structs get extended. At present, this impacts struct
pidfd_infoandPIDFD_GET_INFO.I think this is a deparature from the previous design, where (low-level) userspace did not have not worry about the internal structure of ioctl commands and could treat them as opaque bit patterns. With the new approach, we have to dissect some of the commands in the same way
extensible_ioctl_validdoes it above.So far, this impacts glibc ABI tests. Looking at the strace sources, it doesn't look to me as if the ioctl handler is prepared to deal with this situation, either, because it uses the full ioctl command for lookups.
Quoted 10 lines
The sanitizers could implement generic ioctl checking with the embedded size information in the ioctl command, but the current code structure is not set up to handle this because it's indexed by the full ioctl command, not the type. I think in some cases, the size is required to disambiguate ioctl commands because the type field is not unique across devices. In some cases, the sanitizers would have to know the exact command (not just the size), to validate points embedded in the struct passed to the ioctl. So I don't think changing ioctl constants when extensible structs change is obviously beneficial to the sanitizers, either.
Same for valgrind memcheck handling of ioctls.
Quoted 3 lines
I would prefer if the ioctl commands could be frozen and decoupled from the structs. As far as I understand it, there is no requirement that the embedded size matches what the kernel deals with.
Yes please.
Thanks,
Mark
Christian Brauner
maintainerPATCH 2/32 nsfs: validate extensible ioctls
In reply to Christian BraunerValidate extensible ioctls stricter than we do now.
--- fs/nsfs.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
fs/nsfs.c +3 -1
@@ -169,9 +169,11 @@ static bool nsfs_ioctl_valid(unsigned int cmd)/* Extensible ioctls require some extra handling. */ switch (_IOC_NR (cmd)) {case _IOC_NR (NS_MNT_GET_INFO ): +return extensible_ioctl_valid (cmd,NS_MNT_GET_INFO ,MNT_NS_INFO_SIZE_VER0 );case _IOC_NR (NS_MNT_GET_NEXT ): +return extensible_ioctl_valid (cmd,NS_MNT_GET_NEXT ,MNT_NS_INFO_SIZE_VER0 );case _IOC_NR (NS_MNT_GET_PREV ): -return (_IOC_TYPE (cmd) ==_IOC_TYPE (cmd)); +return extensible_ioctl_valid (cmd,NS_MNT_GET_PREV ,MNT_NS_INFO_SIZE_VER0 ); }return false;
Signature · 1 line
2.47.3
Re: [PATCH 02/32] nsfs: validate extensible ioctls
Quoted 3 lines · Christian Brauner
Validate extensible ioctls stricter than we do now.
Signed-off-by Christian Brauner <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 25 lines
--- fs/nsfs.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)fs/nsfs.c +3
-1@@ -169,9 +169,11 @@ static bool nsfs_ioctl_valid(unsigned int cmd)/* Extensible ioctls require some extra handling. */ switch (_IOC_NR (cmd)) {case _IOC_NR (NS_MNT_GET_INFO ): +return extensible_ioctl_valid (cmd,NS_MNT_GET_INFO ,MNT_NS_INFO_SIZE_VER0 );case _IOC_NR (NS_MNT_GET_NEXT ): +return extensible_ioctl_valid (cmd,NS_MNT_GET_NEXT ,MNT_NS_INFO_SIZE_VER0 );case _IOC_NR (NS_MNT_GET_PREV ): -return (_IOC_TYPE (cmd) ==_IOC_TYPE (cmd)); +return extensible_ioctl_valid (cmd,NS_MNT_GET_PREV ,MNT_NS_INFO_SIZE_VER0 ); }return false;Signature · 2 lines
-- 2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerPATCH 3/32 block: use extensible_ioctl_valid()
In reply to Christian BraunerUse the new extensible_ioctl_valid() helper which is equivalent to what is done here.
--- block/blk-integrity.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
block/blk-integrity.c +3 -5
@@ -58,16 +58,14 @@ int blk_rq_count_integrity_sg(struct request_queue *q, struct bio *bio)int blk_get_meta_cap (struct block_device *bdev,unsigned int cmd,struct logical_block_metadata_cap __user *argp) { -struct blk_integrity *bi =blk_get_integrity (bdev->bd_disk); +struct blk_integrity *bi;struct logical_block_metadata_cap meta_cap = {};size_t usize =_IOC_SIZE (cmd); -if (_IOC_DIR (cmd) !=_IOC_DIR (FS_IOC_GETLBMD_CAP ) || -_IOC_TYPE (cmd) !=_IOC_TYPE (FS_IOC_GETLBMD_CAP ) || -_IOC_NR (cmd) !=_IOC_NR (FS_IOC_GETLBMD_CAP ) || -_IOC_SIZE (cmd) <LBMD_SIZE_VER0 ) +if (extensible_ioctl_valid (cmd,FS_IOC_GETLBMD_CAP ,LBMD_SIZE_VER0 ))return -ENOIOCTLCMD ; + bi =blk_get_integrity (bdev->bd_disk);if (!bi) goto out;
Signature · 1 line
2.47.3
Re: [PATCH 03/32] block: use extensible_ioctl_valid()
Quoted 4 lines · Christian Brauner
Use the new
extensible_ioctl_valid()helper which is equivalent to what is done here.Signed-off-by Christian Brauner <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 32 lines
--- block/blk-integrity.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)block/blk-integrity.c +3
-5@@ -58,16 +58,14 @@ int blk_rq_count_integrity_sg(struct request_queue *q, struct bio *bio)int blk_get_meta_cap (struct block_device *bdev,unsigned int cmd,struct logical_block_metadata_cap __user *argp) { -struct blk_integrity *bi =blk_get_integrity (bdev->bd_disk); +struct blk_integrity *bi;struct logical_block_metadata_cap meta_cap = {};size_t usize =_IOC_SIZE (cmd); -if (_IOC_DIR (cmd) !=_IOC_DIR (FS_IOC_GETLBMD_CAP ) || -_IOC_TYPE (cmd) !=_IOC_TYPE (FS_IOC_GETLBMD_CAP ) || -_IOC_NR (cmd) !=_IOC_NR (FS_IOC_GETLBMD_CAP ) || -_IOC_SIZE (cmd) <LBMD_SIZE_VER0 ) +if (extensible_ioctl_valid (cmd,FS_IOC_GETLBMD_CAP ,LBMD_SIZE_VER0 ))return -ENOIOCTLCMD ; + bi =blk_get_integrity (bdev->bd_disk);if (!bi) goto out;Signature · 2 lines
-- 2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH 03/32] block: use extensible_ioctl_valid()
In reply to Christian BraunerSignature · 1 line
Jens Axboe
Christian Brauner
maintainerPATCH 4/32 ns: move to_ns_common() to ns_common.h
In reply to Christian BraunerMove the helper to ns_common.h where it belongs.
--- include/linux/ns_common.h | 20 ++++++++++++++++++++ include/linux/nsproxy.h | 11 ----------- 2 files changed, 20 insertions(+), 11 deletions(-)
include/linux/ns_common.h +20 -0
@@ -6,6 +6,15 @@struct proc_ns_operations ; +struct cgroup_namespace ; +struct ipc_namespace ; +struct mnt_namespace ; +struct net ; +struct pid_namespace ; +struct time_namespace ; +struct user_namespace ; +struct uts_namespace ; +struct ns_common {struct dentry *stashed;const struct proc_ns_operations *ops;
@@ -13,4 +22,15 @@ struct ns_common { refcount_t count; }; +#define to_ns_common (__ns) \ + _Generic((__ns), \ + struct cgroup_namespace *: &(__ns)->ns, \ + struct ipc_namespace *: &(__ns)->ns, \ + struct mnt_namespace *: &(__ns)->ns, \ + struct net *: &(__ns)->ns, \ + struct pid_namespace *: &(__ns)->ns, \ + struct time_namespace *: &(__ns)->ns, \ + struct user_namespace *: &(__ns)->ns, \ + struct uts_namespace *: &(__ns)->ns) +#endif
include/linux/nsproxy.h +0 -11
@@ -42,17 +42,6 @@ struct nsproxy { };extern struct nsproxy init_nsproxy; -#define to_ns_common (__ns) \ - _Generic((__ns), \ - struct cgroup_namespace *: &(__ns->ns), \ - struct ipc_namespace *: &(__ns->ns), \ - struct net *: &(__ns->ns), \ - struct pid_namespace *: &(__ns->ns), \ - struct mnt_namespace *: &(__ns->ns), \ - struct time_namespace *: &(__ns->ns), \ - struct user_namespace *: &(__ns->ns), \ - struct uts_namespace *: &(__ns->ns)) -/* * A structure to encompass all bits needed to install * a partial or complete new set of namespaces.
Signature · 1 line
2.47.3
Re: [PATCH 04/32] ns: move to_ns_common() to ns_common.h
Quoted 3 lines · Christian Brauner
Move the helper to
ns_common.hwhere it belongs.Signed-off-by Christian Brauner <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 67 lines
--- include/linux/ns_common.h | 20 ++++++++++++++++++++ include/linux/nsproxy.h | 11 ----------- 2 files changed, 20 insertions(+), 11 deletions(-)include/linux/ns_common.h +20
-0@@ -6,6 +6,15 @@struct proc_ns_operations ; +struct cgroup_namespace ; +struct ipc_namespace ; +struct mnt_namespace ; +struct net ; +struct pid_namespace ; +struct time_namespace ; +struct user_namespace ; +struct uts_namespace ; +struct ns_common {struct dentry *stashed;const struct proc_ns_operations *ops;@@ -13,4 +22,15 @@ struct ns_common { refcount_t count; }; +#define to_ns_common (__ns) \ + _Generic((__ns), \ + struct cgroup_namespace *: &(__ns)->ns, \ + struct ipc_namespace *: &(__ns)->ns, \ + struct mnt_namespace *: &(__ns)->ns, \ + struct net *: &(__ns)->ns, \ + struct pid_namespace *: &(__ns)->ns, \ + struct time_namespace *: &(__ns)->ns, \ + struct user_namespace *: &(__ns)->ns, \ + struct uts_namespace *: &(__ns)->ns) +#endif include/linux/nsproxy.h +0
-11@@ -42,17 +42,6 @@ struct nsproxy { };extern struct nsproxy init_nsproxy; -#define to_ns_common (__ns) \ - _Generic((__ns), \ - struct cgroup_namespace *: &(__ns->ns), \ - struct ipc_namespace *: &(__ns->ns), \ - struct net *: &(__ns->ns), \ - struct pid_namespace *: &(__ns->ns), \ - struct mnt_namespace *: &(__ns->ns), \ - struct time_namespace *: &(__ns->ns), \ - struct user_namespace *: &(__ns->ns), \ - struct uts_namespace *: &(__ns->ns)) -/* * A structure to encompass all bits needed to install * a partial or complete new set of namespaces. Signature · 2 lines
-- 2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerPATCH 5/32 nsfs: add nsfs.h header
In reply to Christian BraunerAnd move the stuff out from proc_ns.h where it really doesn't belong.
--- include/linux/nsfs.h | 26 ++++++++++++++++++++++++++ include/linux/proc_ns.h | 13 +------------ 2 files changed, 27 insertions(+), 12 deletions(-)
include/linux/nsfs.h new file +26 -0
@@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2025 Christian Brauner <[email protected]> */ + +#ifndef _LINUX_NSFS_H +#define _LINUX_NSFS_H + +#include <linux/ns_common.h> + +struct path ; +struct task_struct ; +struct proc_ns_operations ; + +int ns_get_path (struct path *path,struct task_struct *task, +const struct proc_ns_operations *ns_ops); +typedef struct ns_common *ns_get_path_helper_t (void *); +int ns_get_path_cb (struct path *path,ns_get_path_helper_t ns_get_cb, +void *private_data); + +bool ns_match (const struct ns_common *ns,dev_t dev,ino_t ino); + +int ns_get_name (char *buf,size_t size,struct task_struct *task, +const struct proc_ns_operations *ns_ops); +void nsfs_init (void ); + +#endif /* _LINUX_NSFS_H */ +
include/linux/proc_ns.h +1 -12
@@ -5,7 +5,7 @@#ifndef _LINUX_PROC_NS_H#define _LINUX_PROC_NS_H -#include <linux/ns_common.h> +#include <linux/nsfs.h> #include <uapi/linux/nsfs.h> struct pid_namespace ;
@@ -75,16 +75,5 @@ static inline int ns_alloc_inum(struct ns_common *ns)#define ns_free_inum (ns) proc_free_inum((ns)->inum)#define get_proc_ns (inode) ((struct ns_common *)(inode)->i_private) -extern int ns_get_path (struct path *path,struct task_struct *task, -const struct proc_ns_operations *ns_ops); -typedef struct ns_common *ns_get_path_helper_t (void *); -extern int ns_get_path_cb (struct path *path,ns_get_path_helper_t ns_get_cb, -void *private_data); - -extern bool ns_match (const struct ns_common *ns,dev_t dev,ino_t ino); - -extern int ns_get_name (char *buf,size_t size,struct task_struct *task, -const struct proc_ns_operations *ns_ops); -extern void nsfs_init (void );#endif /* _LINUX_PROC_NS_H */
Signature · 1 line
2.47.3
Re: [PATCH 05/32] nsfs: add nsfs.h header
Quoted 3 lines · Christian Brauner
And move the stuff out from
proc_ns.hwhere it really doesn't belong.Signed-off-by Christian Brauner <[email protected]>
Looks sensible. Feel free to add:
Honza
Quoted 71 lines
--- include/linux/nsfs.h | 26 ++++++++++++++++++++++++++ include/linux/proc_ns.h | 13 +------------ 2 files changed, 27 insertions(+), 12 deletions(-)include/linux/nsfs.h new file +26
-0@@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2025 Christian Brauner <[email protected]> */ + +#ifndef _LINUX_NSFS_H +#define _LINUX_NSFS_H + +#include <linux/ns_common.h> + +struct path ; +struct task_struct ; +struct proc_ns_operations ; + +int ns_get_path (struct path *path,struct task_struct *task, +const struct proc_ns_operations *ns_ops); +typedef struct ns_common *ns_get_path_helper_t (void *); +int ns_get_path_cb (struct path *path,ns_get_path_helper_t ns_get_cb, +void *private_data); + +bool ns_match (const struct ns_common *ns,dev_t dev,ino_t ino); + +int ns_get_name (char *buf,size_t size,struct task_struct *task, +const struct proc_ns_operations *ns_ops); +void nsfs_init (void ); + +#endif /* _LINUX_NSFS_H */ +include/linux/proc_ns.h +1
-12@@ -5,7 +5,7 @@#ifndef _LINUX_PROC_NS_H#define _LINUX_PROC_NS_H -#include <linux/ns_common.h> +#include <linux/nsfs.h> #include <uapi/linux/nsfs.h> struct pid_namespace ;@@ -75,16 +75,5 @@ static inline int ns_alloc_inum(struct ns_common *ns)#define ns_free_inum (ns) proc_free_inum((ns)->inum)#define get_proc_ns (inode) ((struct ns_common *)(inode)->i_private) -extern int ns_get_path (struct path *path,struct task_struct *task, -const struct proc_ns_operations *ns_ops); -typedef struct ns_common *ns_get_path_helper_t (void *); -extern int ns_get_path_cb (struct path *path,ns_get_path_helper_t ns_get_cb, -void *private_data); - -extern bool ns_match (const struct ns_common *ns,dev_t dev,ino_t ino); - -extern int ns_get_name (char *buf,size_t size,struct task_struct *task, -const struct proc_ns_operations *ns_ops); -extern void nsfs_init (void );#endif /* _LINUX_PROC_NS_H */ Signature · 2 lines
-- 2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerPATCH 6/32 ns: uniformly initialize ns_common
In reply to Christian BraunerNo point in cargo-culting the same code across all the different types. Use one common initializer.
--- include/linux/proc_ns.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
include/linux/proc_ns.h +16 -0
@@ -72,6 +72,22 @@ static inline int ns_alloc_inum(struct ns_common *ns)return proc_alloc_inum (&ns->inum); } +static inline int ns_common_init (struct ns_common *ns, +const struct proc_ns_operations *ops, +bool alloc_inum) +{ +if (alloc_inum) { +int ret; + ret =proc_alloc_inum (&ns->inum); +if (ret) +return ret; + } +refcount_set (&ns->count,1 ); + ns->stashed =NULL ; + ns->ops = ops; +return 0 ; +} +#define ns_free_inum (ns) proc_free_inum((ns)->inum)#define get_proc_ns (inode) ((struct ns_common *)(inode)->i_private)
Signature · 1 line
2.47.3
Re: [PATCH 06/32] ns: uniformly initialize ns_common
Quoted 4 lines · Christian Brauner
No point in cargo-culting the same code across all the different types. Use one common initializer.
Signed-off-by Christian Brauner <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 35 lines
--- include/linux/proc_ns.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)include/linux/proc_ns.h +16
-0@@ -72,6 +72,22 @@ static inline int ns_alloc_inum(struct ns_common *ns)return proc_alloc_inum (&ns->inum); } +static inline int ns_common_init (struct ns_common *ns, +const struct proc_ns_operations *ops, +bool alloc_inum) +{ +if (alloc_inum) { +int ret; + ret =proc_alloc_inum (&ns->inum); +if (ret) +return ret; + } +refcount_set (&ns->count,1 ); + ns->stashed =NULL ; + ns->ops = ops; +return 0 ; +} +#define ns_free_inum (ns) proc_free_inum((ns)->inum)#define get_proc_ns (inode) ((struct ns_common *)(inode)->i_private)Signature · 2 lines
-- 2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerPATCH 7/32 mnt: use ns_common_init()
In reply to Christian BraunerDon't cargo-cult the same thing over and over.
--- fs/namespace.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)
fs/namespace.c +6 -9
@@ -4177,18 +4177,15 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool adec_mnt_namespaces (ucounts);return ERR_PTR (-ENOMEM ); } -if (!anon) { - ret =ns_alloc_inum (&new_ns->ns); -if (ret) { -kfree (new_ns); -dec_mnt_namespaces (ucounts); -return ERR_PTR (ret); - } + + ret =ns_common_init (&new_ns->ns, &mntns_operations, !anon); +if (ret) { +kfree (new_ns); +dec_mnt_namespaces (ucounts); +return ERR_PTR (ret); } - new_ns->ns.ops = &mntns_operations;if (!anon) new_ns->seq =atomic64_inc_return (&mnt_ns_seq); -refcount_set (&new_ns->ns.count,1 );refcount_set (&new_ns->passive,1 ); new_ns->mounts =RB_ROOT ;INIT_LIST_HEAD (&new_ns->mnt_ns_list);
Signature · 1 line
2.47.3
Re: [PATCH 07/32] mnt: use ns_common_init()
Quoted 3 lines · Christian Brauner
Don't cargo-cult the same thing over and over.
Signed-off-by Christian Brauner <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 37 lines
--- fs/namespace.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)fs/namespace.c +6
-9@@ -4177,18 +4177,15 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool adec_mnt_namespaces (ucounts);return ERR_PTR (-ENOMEM ); } -if (!anon) { - ret =ns_alloc_inum (&new_ns->ns); -if (ret) { -kfree (new_ns); -dec_mnt_namespaces (ucounts); -return ERR_PTR (ret); - } + + ret =ns_common_init (&new_ns->ns, &mntns_operations, !anon); +if (ret) { +kfree (new_ns); +dec_mnt_namespaces (ucounts); +return ERR_PTR (ret); } - new_ns->ns.ops = &mntns_operations;if (!anon) new_ns->seq =atomic64_inc_return (&mnt_ns_seq); -refcount_set (&new_ns->ns.count,1 );refcount_set (&new_ns->passive,1 ); new_ns->mounts =RB_ROOT ;INIT_LIST_HEAD (&new_ns->mnt_ns_list);Signature · 2 lines
-- 2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerPATCH 8/32 ipc: use ns_common_init()
In reply to Christian BraunerDon't cargo-cult the same thing over and over.
--- ipc/namespace.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
ipc/namespace.c +1 -3
@@ -61,12 +61,10 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,if (ns ==NULL ) goto fail_dec; - err =ns_alloc_inum (&ns->ns); + err =ns_common_init (&ns->ns, &ipcns_operations, true);if (err) goto fail_free; - ns->ns.ops = &ipcns_operations; -refcount_set (&ns->ns.count,1 ); ns->user_ns =get_user_ns (user_ns); ns->ucounts = ucounts;
Signature · 1 line
2.47.3
Re: [PATCH 08/32] ipc: use ns_common_init()
Quoted 3 lines · Christian Brauner
Don't cargo-cult the same thing over and over.
Signed-off-by Christian Brauner <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 26 lines
--- ipc/namespace.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)ipc/namespace.c +1
-3@@ -61,12 +61,10 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,if (ns ==NULL ) goto fail_dec; - err =ns_alloc_inum (&ns->ns); + err =ns_common_init (&ns->ns, &ipcns_operations, true);if (err) goto fail_free; - ns->ns.ops = &ipcns_operations; -refcount_set (&ns->ns.count,1 ); ns->user_ns =get_user_ns (user_ns); ns->ucounts = ucounts;Signature · 2 lines
-- 2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerPATCH 9/32 cgroup: use ns_common_init()
In reply to Christian BraunerDon't cargo-cult the same thing over and over.
--- kernel/cgroup/namespace.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
kernel/cgroup/namespace.c +4 -8
@@ -21,20 +21,16 @@ static void dec_cgroup_namespaces(struct ucounts *ucounts)static struct cgroup_namespace *alloc_cgroup_ns (void ) { -struct cgroup_namespace *new_ns; + struct cgroup_namespace *new_ns __free(kfree) = NULL;int ret; new_ns =kzalloc (sizeof (struct cgroup_namespace ),GFP_KERNEL_ACCOUNT );if (!new_ns)return ERR_PTR (-ENOMEM ); - ret =ns_alloc_inum (&new_ns->ns); -if (ret) { -kfree (new_ns); + ret =ns_common_init (&new_ns->ns, &cgroupns_operations, true); +if (ret)return ERR_PTR (ret); - } -refcount_set (&new_ns->ns.count,1 ); - new_ns->ns.ops = &cgroupns_operations; -return new_ns; +return no_free_ptr (new_ns); }void free_cgroup_ns (struct cgroup_namespace *ns)
Signature · 1 line
2.47.3
Re: [PATCH 09/32] cgroup: use ns_common_init()
Quoted 3 lines · Christian Brauner
Don't cargo-cult the same thing over and over.
Signed-off-by Christian Brauner <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 37 lines
--- kernel/cgroup/namespace.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)kernel/cgroup/namespace.c +4
-8@@ -21,20 +21,16 @@ static void dec_cgroup_namespaces(struct ucounts *ucounts)static struct cgroup_namespace *alloc_cgroup_ns (void ) { -struct cgroup_namespace *new_ns; + struct cgroup_namespace *new_ns __free(kfree) = NULL;int ret; new_ns =kzalloc (sizeof (struct cgroup_namespace ),GFP_KERNEL_ACCOUNT );if (!new_ns)return ERR_PTR (-ENOMEM ); - ret =ns_alloc_inum (&new_ns->ns); -if (ret) { -kfree (new_ns); + ret =ns_common_init (&new_ns->ns, &cgroupns_operations, true); +if (ret)return ERR_PTR (ret); - } -refcount_set (&new_ns->ns.count,1 ); - new_ns->ns.ops = &cgroupns_operations; -return new_ns; +return no_free_ptr (new_ns); }void free_cgroup_ns (struct cgroup_namespace *ns)Signature · 2 lines
-- 2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerPATCH 10/32 pid: use ns_common_init()
In reply to Christian BraunerDon't cargo-cult the same thing over and over.
--- kernel/pid_namespace.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
kernel/pid_namespace.c +1 -3
@@ -102,17 +102,15 @@ static struct pid_namespace *create_pid_namespace(struct user_namespace *user_nsif (ns->pid_cachep ==NULL ) goto out_free_idr; - err =ns_alloc_inum (&ns->ns); + err =ns_common_init (&ns->ns, &pidns_operations, true);if (err) goto out_free_idr; - ns->ns.ops = &pidns_operations; ns->pid_max =PID_MAX_LIMIT ; err =register_pidns_sysctls (ns);if (err) goto out_free_inum; -refcount_set (&ns->ns.count,1 ); ns->level = level; ns->parent =get_pid_ns (parent_pid_ns); ns->user_ns =get_user_ns (user_ns);
Signature · 1 line
2.47.3
Re: [PATCH 10/32] pid: use ns_common_init()
Quoted 3 lines · Christian Brauner
Don't cargo-cult the same thing over and over.
Signed-off-by Christian Brauner <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 31 lines
--- kernel/pid_namespace.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)kernel/pid_namespace.c +1
-3@@ -102,17 +102,15 @@ static struct pid_namespace *create_pid_namespace(struct user_namespace *user_nsif (ns->pid_cachep ==NULL ) goto out_free_idr; - err =ns_alloc_inum (&ns->ns); + err =ns_common_init (&ns->ns, &pidns_operations, true);if (err) goto out_free_idr; - ns->ns.ops = &pidns_operations; ns->pid_max =PID_MAX_LIMIT ; err =register_pidns_sysctls (ns);if (err) goto out_free_inum; -refcount_set (&ns->ns.count,1 ); ns->level = level; ns->parent =get_pid_ns (parent_pid_ns); ns->user_ns =get_user_ns (user_ns);Signature · 2 lines
-- 2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerPATCH 11/32 time: use ns_common_init()
In reply to Christian BraunerDon't cargo-cult the same thing over and over.
--- kernel/time/namespace.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
kernel/time/namespace.c +1 -4
@@ -92,18 +92,15 @@ static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,if (!ns) goto fail_dec; -refcount_set (&ns->ns.count,1 ); - ns->vvar_page =alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO);if (!ns->vvar_page) goto fail_free; - err =ns_alloc_inum (&ns->ns); + err =ns_common_init (&ns->ns, &timens_operations, true);if (err) goto fail_free_page; ns->ucounts = ucounts; - ns->ns.ops = &timens_operations; ns->user_ns =get_user_ns (user_ns); ns->offsets = old_ns->offsets; ns->frozen_offsets = false;
Signature · 1 line
2.47.3
Re: [PATCH 11/32] time: use ns_common_init()
Quoted 3 lines · Christian Brauner
Don't cargo-cult the same thing over and over.
Signed-off-by Christian Brauner <[email protected]>
Re: [PATCH 11/32] time: use ns_common_init()
In reply to Christian BraunerQuoted 3 lines · Christian Brauner
Don't cargo-cult the same thing over and over.
Signed-off-by Christian Brauner <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 32 lines
--- kernel/time/namespace.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)kernel/time/namespace.c +1
-4@@ -92,18 +92,15 @@ static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,if (!ns) goto fail_dec; -refcount_set (&ns->ns.count,1 ); - ns->vvar_page =alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO);if (!ns->vvar_page) goto fail_free; - err =ns_alloc_inum (&ns->ns); + err =ns_common_init (&ns->ns, &timens_operations, true);if (err) goto fail_free_page; ns->ucounts = ucounts; - ns->ns.ops = &timens_operations; ns->user_ns =get_user_ns (user_ns); ns->offsets = old_ns->offsets; ns->frozen_offsets = false;Signature · 2 lines
-- 2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerPATCH 12/32 uts: use ns_common_init()
In reply to Christian BraunerDon't cargo-cult the same thing over and over.
--- kernel/utsname.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-)
kernel/utsname.c +2 -14
@@ -27,16 +27,6 @@ static void dec_uts_namespaces(struct ucounts *ucounts)dec_ucount (ucounts,UCOUNT_UTS_NAMESPACES ); } -static struct uts_namespace *create_uts_ns (void ) -{ -struct uts_namespace *uts_ns; - - uts_ns =kmem_cache_alloc (uts_ns_cache,GFP_KERNEL ); -if (uts_ns) -refcount_set (&uts_ns->ns.count,1 ); -return uts_ns; -} -/* * Clone a new ns copying an original utsname, setting refcount to 1 * @old_ns: namespace to clone
@@ -55,17 +45,15 @@ static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns, goto fail; err = -ENOMEM ; - ns =create_uts_ns (); + ns =kmem_cache_zalloc (uts_ns_cache,GFP_KERNEL );if (!ns) goto fail_dec; - err =ns_alloc_inum (&ns->ns); + err =ns_common_init (&ns->ns, &utsns_operations, true);if (err) goto fail_free; ns->ucounts = ucounts; - ns->ns.ops = &utsns_operations; -down_read (&uts_sem);memcpy (&ns->name, &old_ns->name,sizeof (ns->name)); ns->user_ns =get_user_ns (user_ns);
Signature · 1 line
2.47.3
Re: [PATCH 12/32] uts: use ns_common_init()
Quoted 3 lines · Christian Brauner
Don't cargo-cult the same thing over and over.
Signed-off-by Christian Brauner <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 49 lines
--- kernel/utsname.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-)kernel/utsname.c +2
-14@@ -27,16 +27,6 @@ static void dec_uts_namespaces(struct ucounts *ucounts)dec_ucount (ucounts,UCOUNT_UTS_NAMESPACES ); } -static struct uts_namespace *create_uts_ns (void ) -{ -struct uts_namespace *uts_ns; - - uts_ns =kmem_cache_alloc (uts_ns_cache,GFP_KERNEL ); -if (uts_ns) -refcount_set (&uts_ns->ns.count,1 ); -return uts_ns; -} -/* * Clone a new ns copying an original utsname, setting refcount to 1 * @old_ns: namespace to clone @@ -55,17 +45,15 @@ static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns, goto fail; err = -ENOMEM ; - ns =create_uts_ns (); + ns =kmem_cache_zalloc (uts_ns_cache,GFP_KERNEL );if (!ns) goto fail_dec; - err =ns_alloc_inum (&ns->ns); + err =ns_common_init (&ns->ns, &utsns_operations, true);if (err) goto fail_free; ns->ucounts = ucounts; - ns->ns.ops = &utsns_operations; -down_read (&uts_sem);memcpy (&ns->name, &old_ns->name,sizeof (ns->name)); ns->user_ns =get_user_ns (user_ns);Signature · 2 lines
-- 2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerPATCH 13/32 user: use ns_common_init()
In reply to Christian BraunerDon't cargo-cult the same thing over and over.
--- kernel/user_namespace.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
kernel/user_namespace.c +2 -3
@@ -124,12 +124,11 @@ int create_user_ns(struct cred *new) goto fail_dec; ns->parent_could_setfcap =cap_raised (new->cap_effective,CAP_SETFCAP ); - ret =ns_alloc_inum (&ns->ns); + + ret =ns_common_init (&ns->ns, &userns_operations, true);if (ret) goto fail_free; - ns->ns.ops = &userns_operations; -refcount_set (&ns->ns.count,1 );/* Leave the new->user_ns reference with the new user namespace. */ ns->parent = parent_ns; ns->level = parent_ns->level +1 ;
Signature · 1 line
2.47.3
Re: [PATCH 13/32] user: use ns_common_init()
Quoted 3 lines · Christian Brauner
Don't cargo-cult the same thing over and over.
Signed-off-by Christian Brauner <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 27 lines
--- kernel/user_namespace.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)kernel/user_namespace.c +2
-3@@ -124,12 +124,11 @@ int create_user_ns(struct cred *new) goto fail_dec; ns->parent_could_setfcap =cap_raised (new->cap_effective,CAP_SETFCAP ); - ret =ns_alloc_inum (&ns->ns); + + ret =ns_common_init (&ns->ns, &userns_operations, true);if (ret) goto fail_free; - ns->ns.ops = &userns_operations; -refcount_set (&ns->ns.count,1 );/* Leave the new->user_ns reference with the new user namespace. */ ns->parent = parent_ns; ns->level = parent_ns->level +1 ;Signature · 2 lines
-- 2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerPATCH 14/32 net: use ns_common_init()
In reply to Christian BraunerDon't cargo-cult the same thing over and over.
--- net/core/net_namespace.c | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-)
net/core/net_namespace.c +32 -14
@@ -397,10 +397,22 @@ static __net_init void preinit_net_sysctl(struct net *net) }/* init code that must occur even if setup_net() is not called. */ -static __net_init void preinit_net (struct net *net,struct user_namespace *user_ns) +static __net_init int preinit_net (struct net *net,struct user_namespace *user_ns) { +const struct proc_ns_operations *ns_ops; +int ret; + +#ifdef CONFIG_NET_NS + ns_ops = &netns_operations; +#else + ns_ops =NULL ; +#endif + + ret =ns_common_init (&net->ns, ns_ops, false); +if (ret) +return ret; +refcount_set (&net->passive,1 ); -refcount_set (&net->ns.count,1 );ref_tracker_dir_init (&net->refcnt_tracker,128 ,"net_refcnt" );ref_tracker_dir_init (&net->notrefcnt_tracker,128 ,"net_notrefcnt" );
@@ -420,6 +432,7 @@ static __net_init void preinit_net(struct net *net, struct user_namespace *user_INIT_LIST_HEAD (&net->ptype_all);INIT_LIST_HEAD (&net->ptype_specific);preinit_net_sysctl (net); +return 0 ; }/*
@@ -559,7 +572,9 @@ struct net *copy_net_ns(unsigned long flags, goto dec_ucounts; } -preinit_net (net, user_ns); + rv =preinit_net (net, user_ns); +if (rv <0 ) + goto dec_ucounts; net->ucounts = ucounts;get_user_ns (user_ns);
@@ -573,6 +588,7 @@ struct net *copy_net_ns(unsigned long flags,if (rv <0 ) { put_userns: +ns_free_inum (&net->ns);#ifdef CONFIG_KEYS key_remove_domain (net->key_domain);#endif
@@ -812,17 +828,14 @@ static void net_ns_net_debugfs(struct net *net)static __net_init int net_ns_net_init (struct net *net) { -#ifdef CONFIG_NET_NS - net->ns.ops = &netns_operations; -#endif - net->ns.inum =PROC_NET_INIT_INO ; -if (net != &init_net) { -int ret =ns_alloc_inum (&net->ns); -if (ret) -return ret; - } +int ret =0 ; + +if (net == &init_net) + net->ns.inum =PROC_NET_INIT_INO ; +else + ret =proc_alloc_inum (&to_ns_common (net)->inum);net_ns_net_debugfs (net); -return 0 ; +return ret; } static __net_exit void net_ns_net_exit(struct net *net)
@@ -1282,7 +1295,12 @@ void __init net_ns_init(void)#ifdef CONFIG_KEYS init_net.key_domain = &init_net_key_domain;#endif -preinit_net (&init_net, &init_user_ns); +/* +* This currently cannot fail as the initial network namespace +* has a static inode number. +*/ +if (preinit_net (&init_net, &init_user_ns)) +panic ("Could not preinitialize the initial network namespace" );down_write (&pernet_ops_rwsem);if (setup_net (&init_net))
Signature · 1 line
2.47.3
Jan Kara
reviewerRe: [PATCH 14/32] net: use ns_common_init()
Quoted 3 lines · Christian Brauner
Don't cargo-cult the same thing over and over.
Signed-off-by Christian Brauner <[email protected]>
One comment below.
Quoted 10 lines
net/core/net_namespace.c +0
-6@@ -812,17 +828,14 @@ static void net_ns_net_debugfs(struct net *net)static __net_init int net_ns_net_init (struct net *net) { -#ifdef CONFIG_NET_NS - net->ns.ops = &netns_operations; -#endif - net->ns.inum =PROC_NET_INIT_INO ; -if (net != &init_net) { -int ret =ns_alloc_inum (&net->ns);
net/core/net_namespace.c static void net_ns_net_debugfs(struct net *net) · line 837
Quoted 10 lines
net/core/net_namespace.c +6
-3-if (ret) -return ret; - } +int ret =0 ; + +if (net == &init_net) + net->ns.inum =PROC_NET_INIT_INO ; +else + ret =proc_alloc_inum (&to_ns_common (net)->inum);net_ns_net_debugfs (net);
Here you're calling net_ns_net_debugfs() even if proc_alloc_inum() failed which looks like a bug to me...
Honza
Quoted 23 lines
net/core/net_namespace.c +7
-2-return 0 ; +return ret; } static __net_exit void net_ns_net_exit(struct net *net)@@ -1282,7 +1295,12 @@ void __init net_ns_init(void)#ifdef CONFIG_KEYS init_net.key_domain = &init_net_key_domain;#endif -preinit_net (&init_net, &init_user_ns); +/* +* This currently cannot fail as the initial network namespace +* has a static inode number. +*/ +if (preinit_net (&init_net, &init_user_ns)) +panic ("Could not preinitialize the initial network namespace" );down_write (&pernet_ops_rwsem);if (setup_net (&init_net))Signature · 2 lines
-- 2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerRe: [PATCH 14/32] net: use ns_common_init()
Quoted 22 lines · Jan Kara
Quoted 3 lines · Christian Brauner
Don't cargo-cult the same thing over and over.
Signed-off-by Christian Brauner <[email protected]>One comment below.
Quoted 15 lines
net/core/net_namespace.c +2
-9@@ -812,17 +828,14 @@ static void net_ns_net_debugfs(struct net *net)static __net_init int net_ns_net_init (struct net *net) { -#ifdef CONFIG_NET_NS - net->ns.ops = &netns_operations; -#endif - net->ns.inum =PROC_NET_INIT_INO ; -if (net != &init_net) { -int ret =ns_alloc_inum (&net->ns); -if (ret) -return ret; - } +int ret =0 ; +
Quoted 8 lines
net/core/net_namespace.c static void net_ns_net_debugfs(struct net *net) · line 837
Quoted 5 lines
net/core/net_namespace.c +4
-0+if (net == &init_net) + net->ns.inum =PROC_NET_INIT_INO ; +else + ret =proc_alloc_inum (&to_ns_common (net)->inum);net_ns_net_debugfs (net);Here you're calling
net_ns_net_debugfs()even ifproc_alloc_inum()failed which looks like a bug to me...
Yes, good catch!
Fyi, I have been out properly sick this week and that's why I haven't been very active on-list. I hope to be back in a more functional state tomorrow and will process the backlog.
Jan Kara
reviewerRe: [PATCH 14/32] net: use ns_common_init()
Quoted 32 lines · Christian Brauner
Quoted 22 lines · Jan Kara
Quoted 3 lines · Christian Brauner
Don't cargo-cult the same thing over and over.
Signed-off-by Christian Brauner <[email protected]>One comment below.
Quoted 15 lines
net/core/net_namespace.c +2
-9@@ -812,17 +828,14 @@ static void net_ns_net_debugfs(struct net *net)static __net_init int net_ns_net_init (struct net *net) { -#ifdef CONFIG_NET_NS - net->ns.ops = &netns_operations; -#endif - net->ns.inum =PROC_NET_INIT_INO ; -if (net != &init_net) { -int ret =ns_alloc_inum (&net->ns); -if (ret) -return ret; - } +int ret =0 ; +Quoted 8 lines
net/core/net_namespace.c static void net_ns_net_debugfs(struct net *net) · line 837
Quoted 5 lines
net/core/net_namespace.c +4
-0+if (net == &init_net) + net->ns.inum =PROC_NET_INIT_INO ; +else + ret =proc_alloc_inum (&to_ns_common (net)->inum);net_ns_net_debugfs (net);Here you're calling
net_ns_net_debugfs()even ifproc_alloc_inum()failed which looks like a bug to me...
Quoted 5 lines
Yes, good catch!
Fyi, I have been out properly sick this week and that's why I haven't been very active on-list. I hope to be back in a more functional state tomorrow and will process the backlog.
There's no rush. Get well soon!
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH 14/32] net: use ns_common_init()
In reply to Christian Braunernet/core/net_namespace.c struct net *copy_net_ns(unsigned long flags, · line 591
Quoted 5 lines · Christian Brauner
net/core/net_namespace.c +0
-0@@ -573,6 +588,7 @@ struct net *copy_net_ns(unsigned long flags,if (rv <0 ) {
put_userns:net/core/net_namespace.c +1
-0+ns_free_inum (&net->ns);
I've ended up looking at this patch because of Jan's earlier comment about a different issue in this patch.
Aren't we double-freeing net->ns here if setup_net() failed?
setup_net() can call ops_undo_list() on failure, which will ns_free_inum(&net->ns) once, and then we do it again in the put_userns error handling label.
Signature · 2 lines
Thanks, Sasha
Christian Brauner
maintainerPATCH 15/32 ns: remove ns_alloc_inum()
In reply to Christian BraunerIt's now unused.
--- include/linux/proc_ns.h | 6 ------ 1 file changed, 6 deletions(-)
include/linux/proc_ns.h +0 -6
@@ -66,12 +66,6 @@ static inline void proc_free_inum(unsigned int inum) {}#endif /* CONFIG_PROC_FS */ -static inline int ns_alloc_inum (struct ns_common *ns) -{ -WRITE_ONCE (ns->stashed,NULL ); -return proc_alloc_inum (&ns->inum); -} -static inline int ns_common_init (struct ns_common *ns,const struct proc_ns_operations *ops,bool alloc_inum)
Signature · 1 line
2.47.3
Re: [PATCH 15/32] ns: remove ns_alloc_inum()
Quoted 3 lines · Christian Brauner
It's now unused.
Signed-off-by Christian Brauner <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 25 lines
--- include/linux/proc_ns.h | 6 ------ 1 file changed, 6 deletions(-)include/linux/proc_ns.h +0
-6@@ -66,12 +66,6 @@ static inline void proc_free_inum(unsigned int inum) {}#endif /* CONFIG_PROC_FS */ -static inline int ns_alloc_inum (struct ns_common *ns) -{ -WRITE_ONCE (ns->stashed,NULL ); -return proc_alloc_inum (&ns->inum); -} -static inline int ns_common_init (struct ns_common *ns,const struct proc_ns_operations *ops,bool alloc_inum)Signature · 2 lines
-- 2.47.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerPATCH 16/32 nstree: make iterator generic
In reply to Christian BraunerMove the namespace iteration infrastructure originally introduced for mount namespaces into a generic library usable by all namespace types.
--- include/linux/ns_common.h | 9 ++ include/linux/nstree.h | 89 ++++++++++++++++++ include/linux/proc_ns.h | 3 + kernel/Makefile | 2 +- kernel/nstree.c | 233 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 335 insertions(+), 1 deletion(-)
include/linux/ns_common.h +9 -0
@@ -3,6 +3,7 @@#define _LINUX_NS_COMMON_H#include <linux/refcount.h> +#include <linux/rbtree.h> struct proc_ns_operations ;
@@ -20,6 +21,14 @@ struct ns_common {const struct proc_ns_operations *ops;unsigned int inum;refcount_t count; +union { +struct { +u64 ns_id; +struct rb_node ns_tree_node; +struct list_head ns_list_node; + }; +struct rcu_head ns_rcu; + }; };#define to_ns_common (__ns) \
include/linux/nstree.h new file +89 -0
89 lines not on this page · raw message
include/linux/proc_ns.h +3 -0
@@ -79,6 +79,9 @@ static inline int ns_common_init(struct ns_common *ns,refcount_set (&ns->count,1 ); ns->stashed =NULL ; ns->ops = ops; + ns->ns_id =0 ; +RB_CLEAR_NODE (&ns->ns_tree_node); +INIT_LIST_HEAD (&ns->ns_list_node);return 0 ; }
kernel/Makefile +1 -1
@@ -8,7 +8,7 @@ obj-y = fork.o exec_domain.o panic.o \ sysctl.o capability.o ptrace.o user.o \ signal.o sys.o umh.o workqueue.o pid.o task_work.o \ extable.o params.o \ - kthread.o sys_ni.o nsproxy.o \ + kthread.o sys_ni.o nsproxy.o nstree.o \ notifier.o ksysfs.o cred.o reboot.o \ async.o range.o smpboot.o ucount.o regset.o ksyms_common.o
kernel/nstree.c new file +233 -0
233 lines not on this page · raw message
Signature · 1 line
2.47.3
Christian Brauner
maintainerPATCH 17/32 mnt: support iterator
In reply to Christian BraunerMove the mount namespace to the generic iterator.
This allows us to drop a bunch of members from struct mnt_namespace.
t
--- fs/mount.h | 10 +--- fs/namespace.c | 141 +++++++++++++-------------------------------------------- fs/nsfs.c | 4 +- 3 files changed, 35 insertions(+), 120 deletions(-)
fs/mount.h +2 -8
@@ -17,11 +17,7 @@ struct mnt_namespace { };struct user_namespace *user_ns;struct ucounts *ucounts; -u64 seq;/* Sequence number to prevent loops */ -union { -wait_queue_head_t poll; -struct rcu_head mnt_ns_rcu; - }; +wait_queue_head_t poll;u64 seq_origin;/* Sequence number of origin mount namespace */ u64 event;#ifdef CONFIG_FSNOTIFY
@@ -30,8 +26,6 @@ struct mnt_namespace {#endif unsigned int nr_mounts;/* # of mounts in the namespace */ unsigned int pending_mounts; -struct rb_node mnt_ns_tree_node;/* node in the mnt_ns_tree */ -struct list_head mnt_ns_list;/* entry in the sequential list of mounts namespace */ refcount_t passive;/* number references not pinning @mounts */ } __randomize_layout;
@@ -173,7 +167,7 @@ static inline bool is_local_mountpoint(const struct dentry *dentry)static inline bool is_anon_ns (struct mnt_namespace *ns) { -return ns->seq ==0 ; +return ns->ns.ns_id ==0 ; }static inline bool anon_ns_root (const struct mount *m)
fs/namespace.c +31 -110
257 lines not on this page · raw message
fs/nsfs.c +2 -2
@@ -139,7 +139,7 @@ static int copy_ns_info_to_user(const struct mnt_namespace *mnt_ns,* the size value will be set to the size the kernel knows about. */ kinfo->size =min (usize,sizeof (*kinfo)); - kinfo->mnt_ns_id = mnt_ns->seq; + kinfo->mnt_ns_id = mnt_ns->ns.ns_id; kinfo->nr_mounts =READ_ONCE (mnt_ns->nr_mounts);/* Subtract the root mount of the mount namespace. */ if (kinfo->nr_mounts)
@@ -221,7 +221,7 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl, mnt_ns = container_of(ns, struct mnt_namespace, ns); idp = (__u64 __user *)arg; - id = mnt_ns->seq; + id = mnt_ns->ns.ns_id;return put_user (id, idp); }case NS_GET_PID_FROM_PIDNS :
Signature · 1 line
2.47.3
Re: [PATCH 17/32] mnt: support iterator
Quoted 4 lines
Move the mount namespace to the generic iterator.
This allows us to drop a bunch of members from structmnt_namespace.
tSigned-off-by Christian Brauner <[email protected]>
There is weird "t" here floating at the right
Signature · 1 line
Askar Safin
Christian Brauner
maintainerPATCH 18/32 cgroup: support iterator
In reply to Christian BraunerSupport the generic namespace iterator and lookup infrastructure to support file handles for namespaces.
--- kernel/cgroup/cgroup.c | 2 ++ kernel/cgroup/namespace.c | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-)
kernel/cgroup/cgroup.c +2 -0
@@ -59,6 +59,7 @@#include <linux/sched/cputime.h> #include <linux/sched/deadline.h> #include <linux/psi.h> +#include <linux/nstree.h> #include <net/sock.h> #define CREATE_TRACE_POINTS
@@ -6312,6 +6313,7 @@ int __init cgroup_init(void)WARN_ON (register_filesystem (&cpuset_fs_type));#endif +ns_tree_add (&init_cgroup_ns);return 0 ; }
kernel/cgroup/namespace.c +5 -2
@@ -5,7 +5,7 @@#include <linux/slab.h> #include <linux/nsproxy.h> #include <linux/proc_ns.h> - +#include <linux/nstree.h> /* cgroup namespaces */
@@ -30,16 +30,19 @@ static struct cgroup_namespace *alloc_cgroup_ns(void) ret =ns_common_init (&new_ns->ns, &cgroupns_operations, true);if (ret)return ERR_PTR (ret); +ns_tree_add (new_ns);return no_free_ptr (new_ns); }void free_cgroup_ns (struct cgroup_namespace *ns) { +ns_tree_remove (ns);put_css_set (ns->root_cset);dec_cgroup_namespaces (ns->ucounts);put_user_ns (ns->user_ns);ns_free_inum (&ns->ns); -kfree (ns); +/* Concurrent nstree traversal depends on a grace period. */ +kfree_rcu (ns, ns.ns_rcu); }EXPORT_SYMBOL (free_cgroup_ns);
Signature · 1 line
2.47.3
Re: [PATCH 18/32] cgroup: support iterator
Quoted 2 lines · Christian Brauner
Support the generic namespace iterator and lookup infrastructure to support file handles for namespaces.
The patch subject seems a bit too generic and could be misleading. Maybe it should mention it's for namespaces? Other than that,
Thanks.
Signature · 1 line
tejun
Christian Brauner
maintainerPATCH 19/32 ipc: support iterator
In reply to Christian BraunerSupport the generic namespace iterator and lookup infrastructure to support file handles for namespaces.
--- ipc/msgutil.c | 1 + ipc/namespace.c | 3 +++ ipc/shm.c | 2 ++ 3 files changed, 6 insertions(+)
ipc/msgutil.c +1 -0
@@ -15,6 +15,7 @@#include <linux/proc_ns.h> #include <linux/uaccess.h> #include <linux/sched.h> +#include <linux/nstree.h> #include "util.h"
ipc/namespace.c +3 -0
@@ -15,6 +15,7 @@#include <linux/mount.h> #include <linux/user_namespace.h> #include <linux/proc_ns.h> +#include <linux/nstree.h> #include <linux/sched/task.h> #include "util.h"
@@ -85,6 +86,7 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,sem_init_ns (ns);shm_init_ns (ns); +ns_tree_add (ns);return ns;
@@ -201,6 +203,7 @@ void put_ipc_ns(struct ipc_namespace *ns)mq_clear_sbinfo (ns);spin_unlock (&mq_lock); +ns_tree_remove (ns);if (llist_add (&ns->mnt_llist, &free_ipc_list))schedule_work (&free_ipc_work); }
ipc/shm.c +2 -0
@@ -45,6 +45,7 @@#include <linux/mount.h> #include <linux/ipc_namespace.h> #include <linux/rhashtable.h> +#include <linux/nstree.h> #include <linux/uaccess.h>
@@ -148,6 +149,7 @@ void shm_exit_ns(struct ipc_namespace *ns)static int __init ipc_ns_init (void ) {shm_init_ns (&init_ipc_ns); +ns_tree_add (&init_ipc_ns);return 0 ; }
Signature · 1 line
2.47.3
Christian Brauner
maintainerPATCH 20/32 net: support iterator
In reply to Christian BraunerSupport the generic namespace iterator and lookup infrastructure to support file handles for namespaces.
The network namespace has a separate list with different lifetime rules which we can just leave in tact. We have a similar concept for mount namespaces as well where it is on two differenet lists for different purposes.
--- include/net/net_namespace.h | 1 + net/core/net_namespace.c | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-)
include/net/net_namespace.h +1 -0
@@ -11,6 +11,7 @@#include <linux/list.h> #include <linux/sysctl.h> #include <linux/uidgid.h> +#include <linux/nstree.h> #include <net/flow.h> #include <net/netns/core.h>
net/core/net_namespace.c +6 -2
@@ -20,6 +20,7 @@#include <linux/sched/task.h> #include <linux/uidgid.h> #include <linux/proc_fs.h> +#include <linux/nstree.h> #include <net/aligned_data.h> #include <net/sock.h>
@@ -445,7 +446,7 @@ static __net_init int setup_net(struct net *net)LIST_HEAD (net_exit_list);int error =0 ; - net->net_cookie =atomic64_inc_return (&net_aligned_data.net_cookie); + net->net_cookie =ns_tree_gen_id (&net->ns);list_for_each_entry (ops, &pernet_list, list) { error =ops_init (ops, net);
@@ -455,6 +456,7 @@ static __net_init int setup_net(struct net *net)down_write (&net_rwsem);list_add_tail_rcu (&net->list, &net_namespace_list);up_write (&net_rwsem); +ns_tree_add_raw (net); out:return error;
@@ -675,8 +677,10 @@ static void cleanup_net(struct work_struct *work)/* Don't let anyone else find us. */ down_write (&net_rwsem); -llist_for_each_entry (net, net_kill_list, cleanup_list) +llist_for_each_entry (net, net_kill_list, cleanup_list) { +ns_tree_remove (net);list_del_rcu (&net->list); + }/* Cache last net. After we unlock rtnl, no one new net * added to net_namespace_list can assign nsid pointer * to a net from net_kill_list (see peernet2id_alloc()).
Signature · 1 line
2.47.3
Christian Brauner
maintainerPATCH 21/32 pid: support iterator
In reply to Christian BraunerSupport the generic namespace iterator and lookup infrastructure to support file handles for namespaces.
--- kernel/pid_namespace.c | 4 ++++ 1 file changed, 4 insertions(+)
kernel/pid_namespace.c +4 -0
@@ -23,6 +23,7 @@#include <linux/sched/task.h> #include <linux/sched/signal.h> #include <linux/idr.h> +#include <linux/nstree.h> #include <uapi/linux/wait.h> #include "pid_sysctl.h"
@@ -122,6 +123,7 @@ static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns ns->memfd_noexec_scope =pidns_memfd_noexec_scope (parent_pid_ns);#endif +ns_tree_add (ns);return ns; out_free_inum:
@@ -147,6 +149,7 @@ static void delayed_free_pidns(struct rcu_head *p)static void destroy_pid_namespace (struct pid_namespace *ns) { +ns_tree_remove (ns);unregister_pidns_sysctls (ns);ns_free_inum (&ns->ns);
@@ -473,6 +476,7 @@ static __init int pid_namespaces_init(void)#endif register_pid_ns_sysctl_table_vm (); +ns_tree_add (&init_pid_ns);return 0 ; }
Signature · 1 line
2.47.3
Christian Brauner
maintainerPATCH 22/32 time: support iterator
In reply to Christian BraunerSupport the generic namespace iterator and lookup infrastructure to support file handles for namespaces.
--- include/linux/time_namespace.h | 5 +++++ init/main.c | 2 ++ kernel/time/namespace.c | 13 +++++++++++-- 3 files changed, 18 insertions(+), 2 deletions(-)
include/linux/time_namespace.h +5 -0
@@ -33,6 +33,7 @@ struct time_namespace {extern struct time_namespace init_time_ns;#ifdef CONFIG_TIME_NS +void __init time_ns_init (void );extern int vdso_join_timens (struct task_struct *task,struct time_namespace *ns);extern void timens_commit (struct task_struct *tsk,struct time_namespace *ns);
@@ -108,6 +109,10 @@ static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim) }#else +static inline void __init time_ns_init (void ) +{ +} +static inline int vdso_join_timens (struct task_struct *task,struct time_namespace *ns) {
init/main.c +2 -0
@@ -103,6 +103,7 @@#include <linux/randomize_kstack.h> #include <linux/pidfs.h> #include <linux/ptdump.h> +#include <linux/time_namespace.h> #include <net/net_namespace.h> #include <asm/io.h>
@@ -1072,6 +1073,7 @@ void start_kernel(void)fork_init ();proc_caches_init ();uts_ns_init (); +time_ns_init ();key_init ();security_init ();dbg_late_init ();
kernel/time/namespace.c +11 -2
@@ -12,6 +12,7 @@#include <linux/seq_file.h> #include <linux/proc_ns.h> #include <linux/export.h> +#include <linux/nstree.h> #include <linux/time.h> #include <linux/slab.h> #include <linux/cred.h>
@@ -88,7 +89,7 @@ static struct time_namespace *clone_time_ns(struct user_namespace *user_ns, goto fail; err = -ENOMEM ; - ns =kmalloc (sizeof (*ns),GFP_KERNEL_ACCOUNT ); + ns =kzalloc (sizeof (*ns),GFP_KERNEL_ACCOUNT );if (!ns) goto fail_dec;
@@ -104,6 +105,7 @@ static struct time_namespace *clone_time_ns(struct user_namespace *user_ns, ns->user_ns =get_user_ns (user_ns); ns->offsets = old_ns->offsets; ns->frozen_offsets = false; +ns_tree_add (ns);return ns; fail_free_page:
@@ -250,11 +252,13 @@ static void timens_set_vvar_page(struct task_struct *task,void free_time_ns (struct time_namespace *ns) { +ns_tree_remove (ns);dec_time_namespaces (ns->ucounts);put_user_ns (ns->user_ns);ns_free_inum (&ns->ns);__free_page (ns->vvar_page); -kfree (ns); +/* Concurrent nstree traversal depends on a grace period. */ +kfree_rcu (ns, ns.ns_rcu); }static struct time_namespace *to_time_ns (struct ns_common *ns)
@@ -487,3 +491,8 @@ struct time_namespace init_time_ns = { .ns.ops = &timens_operations, .frozen_offsets = true, }; + +void __init time_ns_init (void ) +{ +ns_tree_add (&init_time_ns); +}
Signature · 1 line
2.47.3
Re: [PATCH 22/32] time: support iterator
Quoted 4 lines · Christian Brauner
Support the generic namespace iterator and lookup infrastructure to support file handles for namespaces.
Signed-off-by Christian Brauner <[email protected]>
Christian Brauner
maintainerPATCH 23/32 userns: support iterator
In reply to Christian BraunerSupport the generic namespace iterator and lookup infrastructure to support file handles for namespaces.
--- kernel/user_namespace.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/user_namespace.c +6 -1
@@ -21,6 +21,7 @@#include <linux/fs_struct.h> #include <linux/bsearch.h> #include <linux/sort.h> +#include <linux/nstree.h> static struct kmem_cache *user_ns_cachep__ro_after_init ;static DEFINE_MUTEX (userns_state_mutex);
@@ -158,6 +159,7 @@ int create_user_ns(struct cred *new) goto fail_keyring;set_cred_user_ns (new, ns); +ns_tree_add (ns);return 0 ; fail_keyring: #ifdef CONFIG_PERSISTENT_KEYRINGS
@@ -200,6 +202,7 @@ static void free_user_ns(struct work_struct *work) do { struct ucounts *ucounts = ns->ucounts; parent = ns->parent; + ns_tree_remove(ns); if (ns->gid_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) { kfree(ns->gid_map.forward); kfree(ns->gid_map.reverse);
@@ -218,7 +221,8 @@ static void free_user_ns(struct work_struct *work)retire_userns_sysctls (ns);key_free_user_ns (ns);ns_free_inum (&ns->ns); -kmem_cache_free (user_ns_cachep, ns); +/* Concurrent nstree traversal depends on a grace period. */ +kfree_rcu (ns, ns.ns_rcu);dec_user_namespaces (ucounts); ns = parent; }while (refcount_dec_and_test (&parent->ns.count));
@@ -1412,6 +1416,7 @@ const struct proc_ns_operations userns_operations = {static __init int user_namespaces_init (void ) { user_ns_cachep =KMEM_CACHE (user_namespace,SLAB_PANIC |SLAB_ACCOUNT ); +ns_tree_add (&init_user_ns);return 0 ; }subsys_initcall (user_namespaces_init);
Signature · 1 line
2.47.3
Christian Brauner
maintainerPATCH 24/32 uts: support iterator
In reply to Christian BraunerSupport the generic namespace iterator and lookup infrastructure to support file handles for namespaces.
--- kernel/utsname.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/utsname.c +6 -1
@@ -13,6 +13,7 @@#include <linux/cred.h> #include <linux/user_namespace.h> #include <linux/proc_ns.h> +#include <linux/nstree.h> #include <linux/sched/task.h> static struct kmem_cache *uts_ns_cache__ro_after_init ;
@@ -58,6 +59,7 @@ static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,memcpy (&ns->name, &old_ns->name,sizeof (ns->name)); ns->user_ns =get_user_ns (user_ns);up_read (&uts_sem); +ns_tree_add (ns);return ns; fail_free:
@@ -93,10 +95,12 @@ struct uts_namespace *copy_utsname(unsigned long flags,void free_uts_ns (struct uts_namespace *ns) { +ns_tree_remove (ns);dec_uts_namespaces (ns->ucounts);put_user_ns (ns->user_ns);ns_free_inum (&ns->ns); -kmem_cache_free (uts_ns_cache, ns); +/* Concurrent nstree traversal depends on a grace period. */ +kfree_rcu (ns, ns.ns_rcu); }static inline struct uts_namespace *to_uts_ns (struct ns_common *ns)
@@ -162,4 +166,5 @@ void __init uts_ns_init(void) offsetof(struct uts_namespace , name), sizeof_field(struct uts_namespace, name), NULL); +ns_tree_add (&init_uts_ns); }
Signature · 1 line
2.47.3
Christian Brauner
maintainerPATCH 25/32 ns: add to_<type>_ns() to respective headers
In reply to Christian BraunerEvery namespace type has a container_of(ns, <ns_type>, ns) static inline function that is currently not exposed in the header. So we have a bunch of places that open-code it via container_of(). Move it to the headers so we can use it directly.
--- include/linux/cgroup.h | 5 +++++ include/linux/ipc_namespace.h | 5 +++++ include/linux/pid_namespace.h | 5 +++++ include/linux/time_namespace.h | 4 ++++ include/linux/user_namespace.h | 5 +++++ include/linux/utsname.h | 5 +++++ include/net/net_namespace.h | 5 +++++ ipc/namespace.c | 5 ----- kernel/cgroup/namespace.c | 5 ----- kernel/pid_namespace.c | 5 ----- kernel/time/namespace.c | 5 ----- kernel/user_namespace.c | 5 ----- kernel/utsname.c | 5 ----- net/core/net_namespace.c | 5 ----- 14 files changed, 34 insertions(+), 35 deletions(-)
include/linux/cgroup.h +5 -0
@@ -794,6 +794,11 @@ extern struct cgroup_namespace init_cgroup_ns;#ifdef CONFIG_CGROUPS +static inline struct cgroup_namespace *to_cg_ns (struct ns_common *ns) +{ + return container_of(ns, struct cgroup_namespace, ns); +} +void free_cgroup_ns (struct cgroup_namespace *ns); struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
include/linux/ipc_namespace.h +5 -0
@@ -129,6 +129,11 @@ static inline int mq_init_ns(struct ipc_namespace *ns) { return 0; }#endif #if defined(CONFIG_IPC_NS ) +static inline struct ipc_namespace *to_ipc_ns (struct ns_common *ns) +{ + return container_of(ns, struct ipc_namespace, ns); +} +extern struct ipc_namespace *copy_ipcs (unsigned long flags,struct user_namespace *user_ns,struct ipc_namespace *ns);
include/linux/pid_namespace.h +5 -0
@@ -54,6 +54,11 @@ extern struct pid_namespace init_pid_ns;#define PIDNS_ADDING (1U << 31)#ifdef CONFIG_PID_NS +static inline struct pid_namespace *to_pid_ns (struct ns_common *ns) +{ + return container_of(ns, struct pid_namespace, ns); +} +static inline struct pid_namespace *get_pid_ns (struct pid_namespace *ns) {if (ns != &init_pid_ns)
include/linux/time_namespace.h +4 -0
@@ -33,6 +33,10 @@ struct time_namespace {extern struct time_namespace init_time_ns;#ifdef CONFIG_TIME_NS +static inline struct time_namespace *to_time_ns (struct ns_common *ns) +{ + return container_of(ns, struct time_namespace, ns); +}void __init time_ns_init (void );extern int vdso_join_timens (struct task_struct *task,struct time_namespace *ns);
include/linux/user_namespace.h +5 -0
@@ -168,6 +168,11 @@ static inline void set_userns_rlimit_max(struct user_namespace *ns,#ifdef CONFIG_USER_NS +static inline struct user_namespace *to_user_ns (struct ns_common *ns) +{ + return container_of(ns, struct user_namespace, ns); +} +static inline struct user_namespace *get_user_ns (struct user_namespace *ns) {if (ns)
include/linux/utsname.h +5 -0
@@ -30,6 +30,11 @@ struct uts_namespace {extern struct uts_namespace init_uts_ns;#ifdef CONFIG_UTS_NS +static inline struct uts_namespace *to_uts_ns (struct ns_common *ns) +{ + return container_of(ns, struct uts_namespace, ns); +} +static inline void get_uts_ns (struct uts_namespace *ns) {refcount_inc (&ns->ns.count);
include/net/net_namespace.h +5 -0
@@ -263,6 +263,11 @@ void ipx_unregister_sysctl(void);#ifdef CONFIG_NET_NS void __put_net (struct net *net); +static inline struct net *to_net_ns (struct ns_common *ns) +{ + return container_of(ns, struct net, ns); +} +/* Try using get_net_track() instead */ static inline struct net *get_net (struct net *net) {
ipc/namespace.c +0 -5
@@ -209,11 +209,6 @@ void put_ipc_ns(struct ipc_namespace *ns) } } -static inline struct ipc_namespace *to_ipc_ns (struct ns_common *ns) -{ - return container_of(ns, struct ipc_namespace, ns); -} -static struct ns_common *ipcns_get (struct task_struct *task) {struct ipc_namespace *ns =NULL ;
kernel/cgroup/namespace.c +0 -5
@@ -89,11 +89,6 @@ struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,return new_ns; } -static inline struct cgroup_namespace *to_cg_ns (struct ns_common *ns) -{ - return container_of(ns, struct cgroup_namespace, ns); -} -static int cgroupns_install (struct nsset *nsset,struct ns_common *ns) {struct nsproxy *nsproxy = nsset->nsproxy;
kernel/pid_namespace.c +0 -5
@@ -345,11 +345,6 @@ int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)return 0 ; } -static inline struct pid_namespace *to_pid_ns (struct ns_common *ns) -{ - return container_of(ns, struct pid_namespace, ns); -} -static struct ns_common *pidns_get (struct task_struct *task) {struct pid_namespace *ns;
kernel/time/namespace.c +0 -5
@@ -261,11 +261,6 @@ void free_time_ns(struct time_namespace *ns)kfree_rcu (ns, ns.ns_rcu); } -static struct time_namespace *to_time_ns (struct ns_common *ns) -{ - return container_of(ns, struct time_namespace, ns); -} -static struct ns_common *timens_get (struct task_struct *task) {struct time_namespace *ns =NULL ;
kernel/user_namespace.c +0 -5
@@ -1325,11 +1325,6 @@ bool current_in_userns(const struct user_namespace *target_ns) }EXPORT_SYMBOL (current_in_userns); -static inline struct user_namespace *to_user_ns (struct ns_common *ns) -{ - return container_of(ns, struct user_namespace, ns); -} -static struct ns_common *userns_get (struct task_struct *task) {struct user_namespace *user_ns;
kernel/utsname.c +0 -5
@@ -103,11 +103,6 @@ void free_uts_ns(struct uts_namespace *ns)kfree_rcu (ns, ns.ns_rcu); } -static inline struct uts_namespace *to_uts_ns (struct ns_common *ns) -{ - return container_of(ns, struct uts_namespace, ns); -} -static struct ns_common *utsns_get (struct task_struct *task) {struct uts_namespace *ns =NULL ;
net/core/net_namespace.c +0 -5
@@ -1539,11 +1539,6 @@ static struct ns_common *netns_get(struct task_struct *task)return net ? &net->ns :NULL ; } -static inline struct net *to_net_ns (struct ns_common *ns) -{ - return container_of(ns, struct net, ns); -} -static void netns_put (struct ns_common *ns) {put_net (to_net_ns (ns));
Signature · 1 line
2.47.3
Re: [PATCH 25/32] ns: add to_<type>_ns() to respective headers
Quoted 4 lines · Christian Brauner
Every namespace type has a
container_of(ns, <ns_type>, ns)static inline function that is currently not exposed in the header. So we have a bunch of places that open-code it viacontainer_of(). Move it to the headers so we can use it directly.
Yes please! Feel free to add my
Quoted 245 lines
Signed-off-by Christian Brauner <[email protected]>--- include/linux/cgroup.h | 5 +++++ include/linux/ipc_namespace.h | 5 +++++ include/linux/pid_namespace.h | 5 +++++ include/linux/time_namespace.h | 4 ++++ include/linux/user_namespace.h | 5 +++++ include/linux/utsname.h | 5 +++++ include/net/net_namespace.h | 5 +++++ ipc/namespace.c | 5 ----- kernel/cgroup/namespace.c | 5 ----- kernel/pid_namespace.c | 5 ----- kernel/time/namespace.c | 5 ----- kernel/user_namespace.c | 5 ----- kernel/utsname.c | 5 ----- net/core/net_namespace.c | 5 ----- 14 files changed, 34 insertions(+), 35 deletions(-)include/linux/cgroup.h +5
-0@@ -794,6 +794,11 @@ extern struct cgroup_namespace init_cgroup_ns;#ifdef CONFIG_CGROUPS +static inline struct cgroup_namespace *to_cg_ns (struct ns_common *ns) +{ + return container_of(ns, struct cgroup_namespace, ns); +} +void free_cgroup_ns (struct cgroup_namespace *ns); struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,include/linux/ipc_namespace.h +5
-0@@ -129,6 +129,11 @@ static inline int mq_init_ns(struct ipc_namespace *ns) { return 0; }#endif #if defined(CONFIG_IPC_NS ) +static inline struct ipc_namespace *to_ipc_ns (struct ns_common *ns) +{ + return container_of(ns, struct ipc_namespace, ns); +} +extern struct ipc_namespace *copy_ipcs (unsigned long flags,struct user_namespace *user_ns,struct ipc_namespace *ns);include/linux/pid_namespace.h +5
-0@@ -54,6 +54,11 @@ extern struct pid_namespace init_pid_ns;#define PIDNS_ADDING (1U << 31)#ifdef CONFIG_PID_NS +static inline struct pid_namespace *to_pid_ns (struct ns_common *ns) +{ + return container_of(ns, struct pid_namespace, ns); +} +static inline struct pid_namespace *get_pid_ns (struct pid_namespace *ns) {if (ns != &init_pid_ns)include/linux/time_namespace.h +4
-0@@ -33,6 +33,10 @@ struct time_namespace {extern struct time_namespace init_time_ns;#ifdef CONFIG_TIME_NS +static inline struct time_namespace *to_time_ns (struct ns_common *ns) +{ + return container_of(ns, struct time_namespace, ns); +}void __init time_ns_init (void );extern int vdso_join_timens (struct task_struct *task,struct time_namespace *ns);include/linux/user_namespace.h +5
-0@@ -168,6 +168,11 @@ static inline void set_userns_rlimit_max(struct user_namespace *ns,#ifdef CONFIG_USER_NS +static inline struct user_namespace *to_user_ns (struct ns_common *ns) +{ + return container_of(ns, struct user_namespace, ns); +} +static inline struct user_namespace *get_user_ns (struct user_namespace *ns) {if (ns)include/linux/utsname.h +5
-0@@ -30,6 +30,11 @@ struct uts_namespace {extern struct uts_namespace init_uts_ns;#ifdef CONFIG_UTS_NS +static inline struct uts_namespace *to_uts_ns (struct ns_common *ns) +{ + return container_of(ns, struct uts_namespace, ns); +} +static inline void get_uts_ns (struct uts_namespace *ns) {refcount_inc (&ns->ns.count);include/net/net_namespace.h +5
-0@@ -263,6 +263,11 @@ void ipx_unregister_sysctl(void);#ifdef CONFIG_NET_NS void __put_net (struct net *net); +static inline struct net *to_net_ns (struct ns_common *ns) +{ + return container_of(ns, struct net, ns); +} +/* Try using get_net_track() instead */ static inline struct net *get_net (struct net *net) {ipc/namespace.c +0
-5@@ -209,11 +209,6 @@ void put_ipc_ns(struct ipc_namespace *ns) } } -static inline struct ipc_namespace *to_ipc_ns (struct ns_common *ns) -{ - return container_of(ns, struct ipc_namespace, ns); -} -static struct ns_common *ipcns_get (struct task_struct *task) {struct ipc_namespace *ns =NULL ;kernel/cgroup/namespace.c +0
-5@@ -89,11 +89,6 @@ struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,return new_ns; } -static inline struct cgroup_namespace *to_cg_ns (struct ns_common *ns) -{ - return container_of(ns, struct cgroup_namespace, ns); -} -static int cgroupns_install (struct nsset *nsset,struct ns_common *ns) {struct nsproxy *nsproxy = nsset->nsproxy;kernel/pid_namespace.c +0
-5@@ -345,11 +345,6 @@ int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)return 0 ; } -static inline struct pid_namespace *to_pid_ns (struct ns_common *ns) -{ - return container_of(ns, struct pid_namespace, ns); -} -static struct ns_common *pidns_get (struct task_struct *task) {struct pid_namespace *ns;kernel/time/namespace.c +0
-5@@ -261,11 +261,6 @@ void free_time_ns(struct time_namespace *ns)kfree_rcu (ns, ns.ns_rcu); } -static struct time_namespace *to_time_ns (struct ns_common *ns) -{ - return container_of(ns, struct time_namespace, ns); -} -static struct ns_common *timens_get (struct task_struct *task) {struct time_namespace *ns =NULL ;kernel/user_namespace.c +0
-5@@ -1325,11 +1325,6 @@ bool current_in_userns(const struct user_namespace *target_ns) }EXPORT_SYMBOL (current_in_userns); -static inline struct user_namespace *to_user_ns (struct ns_common *ns) -{ - return container_of(ns, struct user_namespace, ns); -} -static struct ns_common *userns_get (struct task_struct *task) {struct user_namespace *user_ns;kernel/utsname.c +0
-5@@ -103,11 +103,6 @@ void free_uts_ns(struct uts_namespace *ns)kfree_rcu (ns, ns.ns_rcu); } -static inline struct uts_namespace *to_uts_ns (struct ns_common *ns) -{ - return container_of(ns, struct uts_namespace, ns); -} -static struct ns_common *utsns_get (struct task_struct *task) {struct uts_namespace *ns =NULL ;net/core/net_namespace.c +0
-5@@ -1539,11 +1539,6 @@ static struct ns_common *netns_get(struct task_struct *task)return net ? &net->ns :NULL ; } -static inline struct net *to_net_ns (struct ns_common *ns) -{ - return container_of(ns, struct net, ns); -} -static void netns_put (struct ns_common *ns) {put_net (to_net_ns (ns));Signature · 2 lines
-- 2.47.3
Signature · 4 lines
Aleksa Sarai Senior Software Engineer (Containers) SUSE Linux GmbH https://www.cyphar.com/
Re: [PATCH 25/32] ns: add to_<type>_ns() to respective headers
In reply to Christian BraunerQuoted 6 lines · Christian Brauner
Every namespace type has a
container_of(ns, <ns_type>, ns)static inline function that is currently not exposed in the header. So we have a bunch of places that open-code it viacontainer_of(). Move it to the headers so we can use it directly.Signed-off-by Christian Brauner <[email protected]>
Christian Brauner
maintainerPATCH 26/32 nsfs: add current_in_namespace()
In reply to Christian BraunerAdd a helper to easily check whether a given namespace is the caller's current namespace. This is currently open-coded in a lot of places. Simply switch on the type and compare the results.
--- include/linux/nsfs.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
include/linux/nsfs.h +15 -1
@@ -5,6 +5,8 @@#define _LINUX_NSFS_H#include <linux/ns_common.h> +#include <linux/cred.h> +#include <linux/pid_namespace.h> struct path ;struct task_struct ;
@@ -22,5 +24,17 @@ int ns_get_name(char *buf, size_t size, struct task_struct *task, const struct proc_ns_operations *ns_ops);void nsfs_init (void ); -#endif /* _LINUX_NSFS_H */ +#define __current_namespace_from_type (__ns) \ + _Generic((__ns), \ + struct cgroup_namespace *: current->nsproxy->cgroup_ns, \ + struct ipc_namespace *: current->nsproxy->ipc_ns, \ + struct net *: current->nsproxy->net_ns, \ + struct pid_namespace *: task_active_pid_ns(current), \ + struct mnt_namespace *: current->nsproxy->mnt_ns, \ + struct time_namespace *: current->nsproxy->time_ns, \ + struct user_namespace *: current_user_ns(), \ + struct uts_namespace *: current->nsproxy->uts_ns) + +#define current_in_namespace (__ns) (__current_namespace_from_type(__ns) == __ns) +#endif /* _LINUX_NSFS_H */
Signature · 1 line
2.47.3
Re: [PATCH 26/32] nsfs: add current_in_namespace()
Quoted 5 lines · Christian Brauner
Add a helper to easily check whether a given namespace is the caller's current namespace. This is currently open-coded in a lot of places. Simply switch on the type and compare the results.
Signed-off-by Christian Brauner <[email protected]>
Looks good, feel free to add my
Quoted 40 lines
--- include/linux/nsfs.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)include/linux/nsfs.h +15
-1@@ -5,6 +5,8 @@#define _LINUX_NSFS_H#include <linux/ns_common.h> +#include <linux/cred.h> +#include <linux/pid_namespace.h> struct path ;struct task_struct ;@@ -22,5 +24,17 @@ int ns_get_name(char *buf, size_t size, struct task_struct *task, const struct proc_ns_operations *ns_ops);void nsfs_init (void ); -#endif /* _LINUX_NSFS_H */ +#define __current_namespace_from_type (__ns) \ + _Generic((__ns), \ + struct cgroup_namespace *: current->nsproxy->cgroup_ns, \ + struct ipc_namespace *: current->nsproxy->ipc_ns, \ + struct net *: current->nsproxy->net_ns, \ + struct pid_namespace *: task_active_pid_ns(current), \ + struct mnt_namespace *: current->nsproxy->mnt_ns, \ + struct time_namespace *: current->nsproxy->time_ns, \ + struct user_namespace *: current_user_ns(), \ + struct uts_namespace *: current->nsproxy->uts_ns) + +#define current_in_namespace (__ns) (__current_namespace_from_type(__ns) == __ns) +#endif /* _LINUX_NSFS_H */ Signature · 2 lines
-- 2.47.3
Signature · 4 lines
Aleksa Sarai Senior Software Engineer (Containers) SUSE Linux GmbH https://www.cyphar.com/
Christian Brauner
maintainerPATCH 27/32 nsfs: support file handles
In reply to Christian BraunerA while ago we added support for file handles to pidfs so pidfds can be encoded and decoded as file handles. Userspace has adopted this quickly and it's proven very useful. Pidfd file handles are exhaustive meaning they don't require a handle on another pidfd to pass to open_by_handle_at() so it can derive the filesystem to decode in.
Implement the exhaustive file handles for namespaces as well.
--- fs/nsfs.c | 176 +++++++++++++++++++++++++++++++++++++++++++++++ include/linux/exportfs.h | 6 ++ 2 files changed, 182 insertions(+)
fs/nsfs.c +176 -0
@@ -13,6 +13,12 @@#include <linux/nsfs.h> #include <linux/uaccess.h> #include <linux/mnt_namespace.h> +#include <linux/ipc_namespace.h> +#include <linux/time_namespace.h> +#include <linux/utsname.h> +#include <linux/exportfs.h> +#include <linux/nstree.h> +#include <net/net_namespace.h> #include "mount.h" #include "internal.h"
@@ -417,12 +423,182 @@ static const struct stashed_operations nsfs_stashed_ops = { .put_data = nsfs_put_data, }; +struct nsfs_fid { +u64 ns_id; +u32 ns_type; +u32 ns_inum; +} __attribute__ ((packed)); + +#define NSFS_FID_SIZE (sizeof(struct nsfs_fid) / sizeof(u32)) + +static int nsfs_encode_fh (struct inode *inode,u32 *fh,int *max_len, +struct inode *parent) +{ +struct nsfs_fid *fid = (struct nsfs_fid *)fh; +struct ns_common *ns = inode->i_private; +int len = *max_len; + +/* +* TODO: +* For hierarchical namespaces we should start to encode the +* parent namespace. Then userspace can walk a namespace +* hierarchy purely based on file handles. +*/ +if (parent) +return FILEID_INVALID ; + +if (len <NSFS_FID_SIZE ) { + *max_len =NSFS_FID_SIZE ; +return FILEID_INVALID ; + } + + len =NSFS_FID_SIZE ; + + fid->ns_id = ns->ns_id; + fid->ns_type = ns->ops->type; + fid->ns_inum = inode->i_ino; + *max_len = len; +return FILEID_NSFS ; +} + +static struct dentry *nsfs_fh_to_dentry (struct super_block *sb,struct fid *fh, +int fh_len,int fh_type) +{ + struct path path __free(path_put) = {}; +struct nsfs_fid *fid = (struct nsfs_fid *)fh; +struct user_namespace *owning_ns =NULL ; +struct ns_common *ns; +int ret; + +if (fh_len <NSFS_FID_SIZE ) +return NULL ; + +switch (fh_type) { +case FILEID_NSFS : +break ; +default : +return NULL ; + } + +scoped_guard (rcu) { + ns =ns_tree_lookup_rcu (fid->ns_id, fid->ns_type); +if (!ns) +return NULL ; + +VFS_WARN_ON_ONCE (ns->ns_id != fid->ns_id); +VFS_WARN_ON_ONCE (ns->ops->type != fid->ns_type); +VFS_WARN_ON_ONCE (ns->inum != fid->ns_inum); + +if (!refcount_inc_not_zero (&ns->count)) +return NULL ; + } + +switch (ns->ops->type) { +#ifdef CONFIG_CGROUPS +case CLONE_NEWCGROUP : +if (!current_in_namespace (to_cg_ns (ns))) + owning_ns =to_cg_ns (ns)->user_ns; +break ; +#endif +#ifdef CONFIG_IPC_NS +case CLONE_NEWIPC : +if (!current_in_namespace (to_ipc_ns (ns))) + owning_ns =to_ipc_ns (ns)->user_ns; +break ; +#endif +case CLONE_NEWNS : +if (!current_in_namespace (to_mnt_ns (ns))) + owning_ns =to_mnt_ns (ns)->user_ns; +break ; +#ifdef CONFIG_NET_NS +case CLONE_NEWNET : +if (!current_in_namespace (to_net_ns (ns))) + owning_ns =to_net_ns (ns)->user_ns; +break ; +#endif +#ifdef CONFIG_PID_NS +case CLONE_NEWPID : +if (!current_in_namespace (to_pid_ns (ns))) { + owning_ns =to_pid_ns (ns)->user_ns; + }else if (!READ_ONCE (to_pid_ns (ns)->child_reaper)) { + ns->ops->put (ns); +return ERR_PTR (-EPERM ); + } +break ; +#endif +#ifdef CONFIG_TIME_NS +case CLONE_NEWTIME : +if (!current_in_namespace (to_time_ns (ns))) + owning_ns =to_time_ns (ns)->user_ns; +break ; +#endif +#ifdef CONFIG_USER_NS +case CLONE_NEWUSER : +if (!current_in_namespace (to_user_ns (ns))) + owning_ns =to_user_ns (ns); +break ; +#endif +#ifdef CONFIG_UTS_NS +case CLONE_NEWUTS : +if (!current_in_namespace (to_uts_ns (ns))) + owning_ns =to_uts_ns (ns)->user_ns; +break ; +#endif +default : +return ERR_PTR (-EOPNOTSUPP ); + } + +if (owning_ns && !ns_capable (owning_ns,CAP_SYS_ADMIN )) { + ns->ops->put (ns); +return ERR_PTR (-EPERM ); + } + +/* path_from_stashed() unconditionally consumes the reference. */ + ret =path_from_stashed (&ns->stashed, nsfs_mnt, ns, &path); +if (ret) +return ERR_PTR (ret); + +return no_free_ptr (path.dentry); +} + +/* +* Make sure that we reject any nonsensical flags that users pass via +* open_by_handle_at(). +*/ +#define VALID_FILE_HANDLE_OPEN_FLAGS \ + (O_RDONLY | O_WRONLY | O_RDWR | O_NONBLOCK | O_CLOEXEC | O_EXCL) + +static int nsfs_export_permission (struct handle_to_path_ctx *ctx, +unsigned int oflags) +{ +if (oflags & ~(VALID_FILE_HANDLE_OPEN_FLAGS |O_LARGEFILE )) +return -EINVAL ; + +/* nsfs_fh_to_dentry() is performs further permission checks. */ +return 0 ; +} + +static struct file *nsfs_export_open (struct path *path,unsigned int oflags) +{ +/* Clear O_LARGEFILE as open_by_handle_at() forces it. */ + oflags &= ~O_LARGEFILE ; +return file_open_root (path,"" , oflags,0 ); +} + +static const struct export_operations nsfs_export_operations = { + .encode_fh = nsfs_encode_fh, + .fh_to_dentry = nsfs_fh_to_dentry, + .open = nsfs_export_open, + .permission = nsfs_export_permission, +}; +static int nsfs_init_fs_context (struct fs_context *fc) {struct pseudo_fs_context *ctx =init_pseudo (fc,NSFS_MAGIC );if (!ctx)return -ENOMEM ; ctx->ops = &nsfs_ops; + ctx->eops = &nsfs_export_operations; ctx->dops = &ns_dentry_operations; fc->s_fs_info = (void *)&nsfs_stashed_ops;return 0 ;
include/linux/exportfs.h +6 -0
@@ -122,6 +122,12 @@ enum fid_type {FILEID_BCACHEFS_WITHOUT_PARENT =0xb1 ,FILEID_BCACHEFS_WITH_PARENT =0xb2 , +/* +* +* 64 bit namespace identifier, 32 bit namespace type, 32 bit inode number. +*/ + FILEID_NSFS = 0xf1, +/* * 64 bit unique kernfs id */
Signature · 1 line
2.47.3
Re: [PATCH 27/32] nsfs: support file handles
Quoted 10 lines · Christian Brauner
A while ago we added support for file handles to pidfs so pidfds can be encoded and decoded as file handles. Userspace has adopted this quickly and it's proven very useful.
Pidfd file handles are exhaustive meaning
they don't require a handle on another pidfd to pass toopen_by_handle_at()so it can derive the filesystem to decode in.Implement the exhaustive file handles for namespaces as well.
I think you decide to split the "exhaustive" part to another patch, so better drop this paragraph?
I am missing an explanation about the permissions for opening these file handles.
My understanding of the code is that the opener needs to meet one of the conditions:
1. user has CAP_SYS_ADMIN in the userns owning the opened namespace
2. current task is in the opened namespace
But I do not fully understand the rationale behind the 2nd condition, that is, when is it useful?
And as far as I can tell, your selftest does not cover this condition (only both true or both false)?
I suggest to start with allowing only the useful and important cases, so if cond #1 is useful enough, drop cond #2 and we can add it later if needed and then your selftests already cover cond #1 true and false.
Quoted 2 lines
Signed-off-by Christian Brauner <[email protected]>
After documenting the permissions, with ot without dropping cond #2 feel free to add:
Quoted 226 lines
--- fs/nsfs.c | 176 +++++++++++++++++++++++++++++++++++++++++++++++ include/linux/exportfs.h | 6 ++ 2 files changed, 182 insertions(+)fs/nsfs.c +176
-0@@ -13,6 +13,12 @@#include <linux/nsfs.h> #include <linux/uaccess.h> #include <linux/mnt_namespace.h> +#include <linux/ipc_namespace.h> +#include <linux/time_namespace.h> +#include <linux/utsname.h> +#include <linux/exportfs.h> +#include <linux/nstree.h> +#include <net/net_namespace.h> #include "mount.h" #include "internal.h" @@ -417,12 +423,182 @@ static const struct stashed_operations nsfs_stashed_ops = { .put_data = nsfs_put_data, }; +struct nsfs_fid { +u64 ns_id; +u32 ns_type; +u32 ns_inum; +} __attribute__ ((packed)); + +#define NSFS_FID_SIZE (sizeof(struct nsfs_fid) / sizeof(u32)) + +static int nsfs_encode_fh (struct inode *inode,u32 *fh,int *max_len, +struct inode *parent) +{ +struct nsfs_fid *fid = (struct nsfs_fid *)fh; +struct ns_common *ns = inode->i_private; +int len = *max_len; + +/* +* TODO: +* For hierarchical namespaces we should start to encode the +* parent namespace. Then userspace can walk a namespace +* hierarchy purely based on file handles. +*/ +if (parent) +return FILEID_INVALID ; + +if (len <NSFS_FID_SIZE ) { + *max_len =NSFS_FID_SIZE ; +return FILEID_INVALID ; + } + + len =NSFS_FID_SIZE ; + + fid->ns_id = ns->ns_id; + fid->ns_type = ns->ops->type; + fid->ns_inum = inode->i_ino; + *max_len = len; +return FILEID_NSFS ; +} + +static struct dentry *nsfs_fh_to_dentry (struct super_block *sb,struct fid *fh, +int fh_len,int fh_type) +{ + struct path path __free(path_put) = {}; +struct nsfs_fid *fid = (struct nsfs_fid *)fh; +struct user_namespace *owning_ns =NULL ; +struct ns_common *ns; +int ret; + +if (fh_len <NSFS_FID_SIZE ) +return NULL ; + +switch (fh_type) { +case FILEID_NSFS : +break ; +default : +return NULL ; + } + +scoped_guard (rcu) { + ns =ns_tree_lookup_rcu (fid->ns_id, fid->ns_type); +if (!ns) +return NULL ; + +VFS_WARN_ON_ONCE (ns->ns_id != fid->ns_id); +VFS_WARN_ON_ONCE (ns->ops->type != fid->ns_type); +VFS_WARN_ON_ONCE (ns->inum != fid->ns_inum); + +if (!refcount_inc_not_zero (&ns->count)) +return NULL ; + } + +switch (ns->ops->type) { +#ifdef CONFIG_CGROUPS +case CLONE_NEWCGROUP : +if (!current_in_namespace (to_cg_ns (ns))) + owning_ns =to_cg_ns (ns)->user_ns; +break ; +#endif +#ifdef CONFIG_IPC_NS +case CLONE_NEWIPC : +if (!current_in_namespace (to_ipc_ns (ns))) + owning_ns =to_ipc_ns (ns)->user_ns; +break ; +#endif +case CLONE_NEWNS : +if (!current_in_namespace (to_mnt_ns (ns))) + owning_ns =to_mnt_ns (ns)->user_ns; +break ; +#ifdef CONFIG_NET_NS +case CLONE_NEWNET : +if (!current_in_namespace (to_net_ns (ns))) + owning_ns =to_net_ns (ns)->user_ns; +break ; +#endif +#ifdef CONFIG_PID_NS +case CLONE_NEWPID : +if (!current_in_namespace (to_pid_ns (ns))) { + owning_ns =to_pid_ns (ns)->user_ns; + }else if (!READ_ONCE (to_pid_ns (ns)->child_reaper)) { + ns->ops->put (ns); +return ERR_PTR (-EPERM ); + } +break ; +#endif +#ifdef CONFIG_TIME_NS +case CLONE_NEWTIME : +if (!current_in_namespace (to_time_ns (ns))) + owning_ns =to_time_ns (ns)->user_ns; +break ; +#endif +#ifdef CONFIG_USER_NS +case CLONE_NEWUSER : +if (!current_in_namespace (to_user_ns (ns))) + owning_ns =to_user_ns (ns); +break ; +#endif +#ifdef CONFIG_UTS_NS +case CLONE_NEWUTS : +if (!current_in_namespace (to_uts_ns (ns))) + owning_ns =to_uts_ns (ns)->user_ns; +break ; +#endif +default : +return ERR_PTR (-EOPNOTSUPP ); + } + +if (owning_ns && !ns_capable (owning_ns,CAP_SYS_ADMIN )) { + ns->ops->put (ns); +return ERR_PTR (-EPERM ); + } + +/* path_from_stashed() unconditionally consumes the reference. */ + ret =path_from_stashed (&ns->stashed, nsfs_mnt, ns, &path); +if (ret) +return ERR_PTR (ret); + +return no_free_ptr (path.dentry); +} + +/* +* Make sure that we reject any nonsensical flags that users pass via +* open_by_handle_at(). +*/ +#define VALID_FILE_HANDLE_OPEN_FLAGS \ + (O_RDONLY | O_WRONLY | O_RDWR | O_NONBLOCK | O_CLOEXEC | O_EXCL) + +static int nsfs_export_permission (struct handle_to_path_ctx *ctx, +unsigned int oflags) +{ +if (oflags & ~(VALID_FILE_HANDLE_OPEN_FLAGS |O_LARGEFILE )) +return -EINVAL ; + +/* nsfs_fh_to_dentry() is performs further permission checks. */ +return 0 ; +} + +static struct file *nsfs_export_open (struct path *path,unsigned int oflags) +{ +/* Clear O_LARGEFILE as open_by_handle_at() forces it. */ + oflags &= ~O_LARGEFILE ; +return file_open_root (path,"" , oflags,0 ); +} + +static const struct export_operations nsfs_export_operations = { + .encode_fh = nsfs_encode_fh, + .fh_to_dentry = nsfs_fh_to_dentry, + .open = nsfs_export_open, + .permission = nsfs_export_permission, +}; +static int nsfs_init_fs_context (struct fs_context *fc) {struct pseudo_fs_context *ctx =init_pseudo (fc,NSFS_MAGIC );if (!ctx)return -ENOMEM ; ctx->ops = &nsfs_ops; + ctx->eops = &nsfs_export_operations; ctx->dops = &ns_dentry_operations; fc->s_fs_info = (void *)&nsfs_stashed_ops;return 0 ;include/linux/exportfs.h +6
-1@@ -122,6 +122,12 @@ enum fid_type {FILEID_BCACHEFS_WITHOUT_PARENT =0xb1 ,FILEID_BCACHEFS_WITH_PARENT =0xb2 , +/* +* +* 64 bit namespace identifier, 32 bit namespace type, 32 bit inode number. +*/ + FILEID_NSFS = 0xf1, +/* * 64 bit unique kernfs id */ --2.47.3
Christian Brauner
maintainerRe: [PATCH 27/32] nsfs: support file handles
Quoted 6 lines · Amir Goldstein
Quoted 4 lines · Christian Brauner
A while ago we added support for file handles to pidfs so pidfds can be encoded and decoded as file handles. Userspace has adopted this quickly and it's proven very useful.
Quoted 8 lines
Quoted 5 lines
Pidfd file handles are exhaustive meaning
they don't require a handle on another pidfd to pass toopen_by_handle_at()so it can derive the filesystem to decode in.Implement the exhaustive file handles for namespaces as well.
I think you decide to split the "exhaustive" part to another patch, so better drop this paragraph?
Yes, good point. I've dont that.
Quoted 7 lines
I am missing an explanation about the permissions for opening these file handles.
My understanding of the code is that the opener needs to meet one of the conditions:
1. user hasCAP_SYS_ADMINin the userns owning the opened namespace
2. current task is in the opened namespace
Yes.
Quoted 3 lines
But I do not fully understand the rationale behind the 2nd condition, that is, when is it useful?
A caller is always able to open a file descriptor to it's own set of namespaces. File handles will behave the same way.
Quoted 2 lines
And as far as I can tell, your selftest does not cover this condition (only both true or both false)?
I've added this now.
Quoted 6 lines
I suggest to start with allowing only the useful and important cases, so if cond #1 is useful enough, drop cond #2 and we can add it later if needed and then your selftests already cover cond #1 true and false.
Quoted 1 line
Quoted 6 lines
Quoted 1 line
Signed-off-by Christian Brauner <[email protected]>After documenting the permissions, with ot without dropping cond #2 feel free to add:
Reviewed-by Amir Goldstein <[email protected]>
Thanks!
Quoted 227 lines
Quoted 227 lines
--- fs/nsfs.c | 176 +++++++++++++++++++++++++++++++++++++++++++++++ include/linux/exportfs.h | 6 ++ 2 files changed, 182 insertions(+)fs/nsfs.c +176
-0@@ -13,6 +13,12 @@#include <linux/nsfs.h> #include <linux/uaccess.h> #include <linux/mnt_namespace.h> +#include <linux/ipc_namespace.h> +#include <linux/time_namespace.h> +#include <linux/utsname.h> +#include <linux/exportfs.h> +#include <linux/nstree.h> +#include <net/net_namespace.h> #include "mount.h" #include "internal.h" @@ -417,12 +423,182 @@ static const struct stashed_operations nsfs_stashed_ops = { .put_data = nsfs_put_data, }; +struct nsfs_fid { +u64 ns_id; +u32 ns_type; +u32 ns_inum; +} __attribute__ ((packed)); + +#define NSFS_FID_SIZE (sizeof(struct nsfs_fid) / sizeof(u32)) + +static int nsfs_encode_fh (struct inode *inode,u32 *fh,int *max_len, +struct inode *parent) +{ +struct nsfs_fid *fid = (struct nsfs_fid *)fh; +struct ns_common *ns = inode->i_private; +int len = *max_len; + +/* +* TODO: +* For hierarchical namespaces we should start to encode the +* parent namespace. Then userspace can walk a namespace +* hierarchy purely based on file handles. +*/ +if (parent) +return FILEID_INVALID ; + +if (len <NSFS_FID_SIZE ) { + *max_len =NSFS_FID_SIZE ; +return FILEID_INVALID ; + } + + len =NSFS_FID_SIZE ; + + fid->ns_id = ns->ns_id; + fid->ns_type = ns->ops->type; + fid->ns_inum = inode->i_ino; + *max_len = len; +return FILEID_NSFS ; +} + +static struct dentry *nsfs_fh_to_dentry (struct super_block *sb,struct fid *fh, +int fh_len,int fh_type) +{ + struct path path __free(path_put) = {}; +struct nsfs_fid *fid = (struct nsfs_fid *)fh; +struct user_namespace *owning_ns =NULL ; +struct ns_common *ns; +int ret; + +if (fh_len <NSFS_FID_SIZE ) +return NULL ; + +switch (fh_type) { +case FILEID_NSFS : +break ; +default : +return NULL ; + } + +scoped_guard (rcu) { + ns =ns_tree_lookup_rcu (fid->ns_id, fid->ns_type); +if (!ns) +return NULL ; + +VFS_WARN_ON_ONCE (ns->ns_id != fid->ns_id); +VFS_WARN_ON_ONCE (ns->ops->type != fid->ns_type); +VFS_WARN_ON_ONCE (ns->inum != fid->ns_inum); + +if (!refcount_inc_not_zero (&ns->count)) +return NULL ; + } + +switch (ns->ops->type) { +#ifdef CONFIG_CGROUPS +case CLONE_NEWCGROUP : +if (!current_in_namespace (to_cg_ns (ns))) + owning_ns =to_cg_ns (ns)->user_ns; +break ; +#endif +#ifdef CONFIG_IPC_NS +case CLONE_NEWIPC : +if (!current_in_namespace (to_ipc_ns (ns))) + owning_ns =to_ipc_ns (ns)->user_ns; +break ; +#endif +case CLONE_NEWNS : +if (!current_in_namespace (to_mnt_ns (ns))) + owning_ns =to_mnt_ns (ns)->user_ns; +break ; +#ifdef CONFIG_NET_NS +case CLONE_NEWNET : +if (!current_in_namespace (to_net_ns (ns))) + owning_ns =to_net_ns (ns)->user_ns; +break ; +#endif +#ifdef CONFIG_PID_NS +case CLONE_NEWPID : +if (!current_in_namespace (to_pid_ns (ns))) { + owning_ns =to_pid_ns (ns)->user_ns; + }else if (!READ_ONCE (to_pid_ns (ns)->child_reaper)) { + ns->ops->put (ns); +return ERR_PTR (-EPERM ); + } +break ; +#endif +#ifdef CONFIG_TIME_NS +case CLONE_NEWTIME : +if (!current_in_namespace (to_time_ns (ns))) + owning_ns =to_time_ns (ns)->user_ns; +break ; +#endif +#ifdef CONFIG_USER_NS +case CLONE_NEWUSER : +if (!current_in_namespace (to_user_ns (ns))) + owning_ns =to_user_ns (ns); +break ; +#endif +#ifdef CONFIG_UTS_NS +case CLONE_NEWUTS : +if (!current_in_namespace (to_uts_ns (ns))) + owning_ns =to_uts_ns (ns)->user_ns; +break ; +#endif +default : +return ERR_PTR (-EOPNOTSUPP ); + } + +if (owning_ns && !ns_capable (owning_ns,CAP_SYS_ADMIN )) { + ns->ops->put (ns); +return ERR_PTR (-EPERM ); + } + +/* path_from_stashed() unconditionally consumes the reference. */ + ret =path_from_stashed (&ns->stashed, nsfs_mnt, ns, &path); +if (ret) +return ERR_PTR (ret); + +return no_free_ptr (path.dentry); +} + +/* +* Make sure that we reject any nonsensical flags that users pass via +* open_by_handle_at(). +*/ +#define VALID_FILE_HANDLE_OPEN_FLAGS \ + (O_RDONLY | O_WRONLY | O_RDWR | O_NONBLOCK | O_CLOEXEC | O_EXCL) + +static int nsfs_export_permission (struct handle_to_path_ctx *ctx, +unsigned int oflags) +{ +if (oflags & ~(VALID_FILE_HANDLE_OPEN_FLAGS |O_LARGEFILE )) +return -EINVAL ; + +/* nsfs_fh_to_dentry() is performs further permission checks. */ +return 0 ; +} + +static struct file *nsfs_export_open (struct path *path,unsigned int oflags) +{ +/* Clear O_LARGEFILE as open_by_handle_at() forces it. */ + oflags &= ~O_LARGEFILE ; +return file_open_root (path,"" , oflags,0 ); +} + +static const struct export_operations nsfs_export_operations = { + .encode_fh = nsfs_encode_fh, + .fh_to_dentry = nsfs_fh_to_dentry, + .open = nsfs_export_open, + .permission = nsfs_export_permission, +}; +static int nsfs_init_fs_context (struct fs_context *fc) {struct pseudo_fs_context *ctx =init_pseudo (fc,NSFS_MAGIC );if (!ctx)return -ENOMEM ; ctx->ops = &nsfs_ops; + ctx->eops = &nsfs_export_operations; ctx->dops = &ns_dentry_operations; fc->s_fs_info = (void *)&nsfs_stashed_ops;return 0 ;include/linux/exportfs.h +6
-1@@ -122,6 +122,12 @@ enum fid_type {FILEID_BCACHEFS_WITHOUT_PARENT =0xb1 ,FILEID_BCACHEFS_WITH_PARENT =0xb2 , +/* +* +* 64 bit namespace identifier, 32 bit namespace type, 32 bit inode number. +*/ + FILEID_NSFS = 0xf1, +/* * 64 bit unique kernfs id */ --2.47.3
Amir Goldstein
reviewerRe: [PATCH 27/32] nsfs: support file handles
Quoted 27 lines · Christian Brauner
Quoted 6 lines · Amir Goldstein
Quoted 4 lines · Christian Brauner
A while ago we added support for file handles to pidfs so pidfds can be encoded and decoded as file handles. Userspace has adopted this quickly and it's proven very useful.
Quoted 8 lines
Quoted 5 lines
Pidfd file handles are exhaustive meaning
they don't require a handle on another pidfd to pass toopen_by_handle_at()so it can derive the filesystem to decode in.Implement the exhaustive file handles for namespaces as well.
I think you decide to split the "exhaustive" part to another patch, so better drop this paragraph?
Yes, good point. I've dont that.
Quoted 7 lines
I am missing an explanation about the permissions for opening these file handles.
My understanding of the code is that the opener needs to meet one of the conditions:
1. user hasCAP_SYS_ADMINin the userns owning the opened namespace
2. current task is in the opened namespace
Quoted 9 lines
Yes.
Quoted 3 lines
But I do not fully understand the rationale behind the 2nd condition, that is, when is it useful?
A caller is always able to open a file descriptor to it's own set of namespaces. File handles will behave the same way.
I understand why it's safe, and I do not object to it at all,
I just feel that I do not fully understand the use case of how ns file handles are expected to be used.
A process can always open /proc/self/ns/mnt
What's the use case where a process may need to open its own ns by handle?
I will explain. For CAP_SYS_ADMIN I can see why keeping handles that do not keep an elevated refcount of ns object could be useful in the same way that an NFS client keeps file handles without keeping the file object alive.
But if you do not have CAP_SYS_ADMIN and can only open your own ns by handle, what is the application that could make use of this? and what's the benefit of such application keeping a file handle instead of ns fd?
Sorry. I feel that I may be missing something in the big picture.
Thanks,
Amir.
Christian Brauner
maintainerRe: [PATCH 27/32] nsfs: support file handles
Quoted 44 lines · Amir Goldstein
Quoted 27 lines · Christian Brauner
Quoted 6 lines · Amir Goldstein
Quoted 4 lines · Christian Brauner
A while ago we added support for file handles to pidfs so pidfds can be encoded and decoded as file handles. Userspace has adopted this quickly and it's proven very useful.
Quoted 8 lines
Quoted 5 lines
Pidfd file handles are exhaustive meaning
they don't require a handle on another pidfd to pass toopen_by_handle_at()so it can derive the filesystem to decode in.Implement the exhaustive file handles for namespaces as well.
I think you decide to split the "exhaustive" part to another patch, so better drop this paragraph?
Yes, good point. I've dont that.
Quoted 7 lines
I am missing an explanation about the permissions for opening these file handles.
My understanding of the code is that the opener needs to meet one of the conditions:
1. user hasCAP_SYS_ADMINin the userns owning the opened namespace
2. current task is in the opened namespaceQuoted 9 lines
Yes.
Quoted 3 lines
But I do not fully understand the rationale behind the 2nd condition, that is, when is it useful?
A caller is always able to open a file descriptor to it's own set of namespaces. File handles will behave the same way.
I understand why it's safe, and I do not object to it at all,
I just feel that I do not fully understand the use case of how ns file handles are expected to be used.
A process can always open/proc/self/ns/mnt
What's the use case where a process may need to open its own ns by handle?
Quoted 8 lines
I will explain. For
CAP_SYS_ADMINI can see why keeping handles that do not keep an elevated refcount of ns object could be useful in the same way that an NFS client keeps file handles without keeping the file object alive.But if you do not have
CAP_SYS_ADMINand can only open your own ns by handle, what is the application that could make use of this? and what's the benefit of such application keeping a file handle instead of ns fd?
A process is not always able to open /proc/self/ns/. That requires procfs to be mounted and for /proc/self/ or /proc/self/ns/ to not be overmounted. However, they can derive a namespace fd from their own pidfd. And that also always works if it's their own namespace.
There's no need to introduce unnecessary behavioral differences between /proc/self/ns/, pidfd-derived namespace fs, and file-handle-derived namespace fds. That's just going to be confusing.
The other thing is that there are legitimate use-case for encoding your own namespace. For example, you might store file handles to your set of namespaces in a file on-disk so you can verify when you get rexeced that they're still valid and so on. This is akin to the pidfd use-case.
Or just plainly for namespace comparison reasons where you keep a file handle to your own namespaces and can then easily check against others.
Amir Goldstein
reviewerRe: [PATCH 27/32] nsfs: support file handles
Quoted 64 lines · Christian Brauner
Quoted 44 lines · Amir Goldstein
Quoted 27 lines · Christian Brauner
Quoted 6 lines · Amir Goldstein
Quoted 4 lines · Christian Brauner
A while ago we added support for file handles to pidfs so pidfds can be encoded and decoded as file handles. Userspace has adopted this quickly and it's proven very useful.
Quoted 8 lines
Quoted 5 lines
Pidfd file handles are exhaustive meaning
they don't require a handle on another pidfd to pass toopen_by_handle_at()so it can derive the filesystem to decode in.Implement the exhaustive file handles for namespaces as well.
I think you decide to split the "exhaustive" part to another patch, so better drop this paragraph?
Yes, good point. I've dont that.
Quoted 7 lines
I am missing an explanation about the permissions for opening these file handles.
My understanding of the code is that the opener needs to meet one of the conditions:
1. user hasCAP_SYS_ADMINin the userns owning the opened namespace
2. current task is in the opened namespaceQuoted 9 lines
Yes.
Quoted 3 lines
But I do not fully understand the rationale behind the 2nd condition, that is, when is it useful?
A caller is always able to open a file descriptor to it's own set of namespaces. File handles will behave the same way.
I understand why it's safe, and I do not object to it at all,
I just feel that I do not fully understand the use case of how ns file handles are expected to be used.
A process can always open/proc/self/ns/mnt
What's the use case where a process may need to open its own ns by handle?Quoted 8 lines
I will explain. For
CAP_SYS_ADMINI can see why keeping handles that do not keep an elevated refcount of ns object could be useful in the same way that an NFS client keeps file handles without keeping the file object alive.But if you do not have
CAP_SYS_ADMINand can only open your own ns by handle, what is the application that could make use of this? and what's the benefit of such application keeping a file handle instead of ns fd?A process is not always able to open
/proc/self/ns/. That requires procfs to be mounted and for/proc/self/or/proc/self/ns/to not be overmounted. However, they can derive a namespace fd from their own pidfd. And that also always works if it's their own namespace.There's no need to introduce unnecessary behavioral differences between
/proc/self/ns/, pidfd-derived namespace fs, and file-handle-derived namespace fds. That's just going to be confusing.
Quoted 7 lines
The other thing is that there are legitimate use-case for encoding your own namespace. For example, you might store file handles to your set of namespaces in a file on-disk so you can verify when you get rexeced that they're still valid and so on. This is akin to the pidfd use-case.
Or just plainly for namespace comparison reasons where you keep a file handle to your own namespaces and can then easily check against others.
OK. As I said no objections I was just curious about this use case.
FWIW, comparing current ns to a stored file handle does not really require permission to open_by_handle_at(). name_to_handle_at() the current ns and binary compare to the stored file handle should be a viable option.
This was exactly the reason for introducing AT_HANDLE_FID, so that fanotify unprivileged watcher with no permission to open_by_handle_at() could compare an fid reported in an event with another fid they obtained earlier with name_to_handle_at() and kept in a map.
Thanks for the explanation!
Amir.
Re: [PATCH 27/32] nsfs: support file handles
In reply to Christian BraunerQuoted 54 lines · Christian Brauner
Quoted 44 lines · Amir Goldstein
Quoted 27 lines · Christian Brauner
Quoted 6 lines · Amir Goldstein
Quoted 4 lines · Christian Brauner
A while ago we added support for file handles to pidfs so pidfds can be encoded and decoded as file handles. Userspace has adopted this quickly and it's proven very useful.
Quoted 8 lines
Quoted 5 lines
Pidfd file handles are exhaustive meaning
they don't require a handle on another pidfd to pass toopen_by_handle_at()so it can derive the filesystem to decode in.Implement the exhaustive file handles for namespaces as well.
I think you decide to split the "exhaustive" part to another patch, so better drop this paragraph?
Yes, good point. I've dont that.
Quoted 7 lines
I am missing an explanation about the permissions for opening these file handles.
My understanding of the code is that the opener needs to meet one of the conditions:
1. user hasCAP_SYS_ADMINin the userns owning the opened namespace
2. current task is in the opened namespaceQuoted 9 lines
Yes.
Quoted 3 lines
But I do not fully understand the rationale behind the 2nd condition, that is, when is it useful?
A caller is always able to open a file descriptor to it's own set of namespaces. File handles will behave the same way.
I understand why it's safe, and I do not object to it at all,
I just feel that I do not fully understand the use case of how ns file handles are expected to be used.
A process can always open/proc/self/ns/mnt
What's the use case where a process may need to open its own ns by handle?Quoted 8 lines
I will explain. For
CAP_SYS_ADMINI can see why keeping handles that do not keep an elevated refcount of ns object could be useful in the same way that an NFS client keeps file handles without keeping the file object alive.But if you do not have
CAP_SYS_ADMINand can only open your own ns by handle, what is the application that could make use of this? and what's the benefit of such application keeping a file handle instead of ns fd?
Quoted 4 lines
A process is not always able to open
/proc/self/ns/. That requires procfs to be mounted and for/proc/self/or/proc/self/ns/to not be overmounted. However, they can derive a namespace fd from their own pidfd. And that also always works if it's their own namespace.
It's also important to note that if /proc/self and /proc/thread-self are overmounted, you can get into scenarios where /proc/$pid will refer to the wrong process (container runtimes run into this scenario a lot -- when configuring a container there is a point where we are in a new pidns but still see the host /proc, which leads to lots of fun bugs).
Quoted 11 lines
There's no need to introduce unnecessary behavioral differences between
/proc/self/ns/, pidfd-derived namespace fs, and file-handle-derived namespace fds. That's just going to be confusing.The other thing is that there are legitimate use-case for encoding your own namespace. For example, you might store file handles to your set of namespaces in a file on-disk so you can verify when you get rexeced that they're still valid and so on. This is akin to the pidfd use-case.
Or just plainly for namespace comparison reasons where you keep a file handle to your own namespaces and can then easily check against others.
I agree wholeheartedly.
Signature · 4 lines
Aleksa Sarai Senior Software Engineer (Containers) SUSE Linux GmbH https://www.cyphar.com/
Christian Brauner
maintainerPATCH 28/32 nsfs: support exhaustive file handles
In reply to Christian BraunerPidfd file handles are exhaustive meaning they don't require a handle on another pidfd to pass to open_by_handle_at() so it can derive the filesystem to decode in. Instead it can be derived from the file handle itself. The same is possible for namespace file handles.
--- fs/fhandle.c | 6 ++++++ fs/internal.h | 1 + fs/nsfs.c | 10 ++++++++++ include/uapi/linux/fcntl.h | 1 + 4 files changed, 18 insertions(+)
fs/fhandle.c +6 -0
@@ -11,6 +11,7 @@#include <linux/personality.h> #include <linux/uaccess.h> #include <linux/compat.h> +#include <linux/nsfs.h> #include "internal.h" #include "mount.h"
@@ -189,6 +190,11 @@ static int get_path_anchor(int fd, struct path *root)return 0 ; } +if (fd ==FD_NSFS_ROOT ) { +nsfs_get_root (root); +return 0 ; + } +return -EBADF ; }
fs/internal.h +1 -0
@@ -355,3 +355,4 @@ int anon_inode_getattr(struct mnt_idmap *idmap, const struct path *path,int anon_inode_setattr (struct mnt_idmap *idmap,struct dentry *dentry,struct iattr *attr);void pidfs_get_root (struct path *path); +void nsfs_get_root (struct path *path);
fs/nsfs.c +10 -0
@@ -25,6 +25,14 @@static struct vfsmount *nsfs_mnt; +static struct path nsfs_root_path = {}; + +void nsfs_get_root (struct path *path) +{ + *path = nsfs_root_path; +path_get (path); +} +static long ns_ioctl (struct file *filp,unsigned int ioctl,unsigned long arg);static const struct file_operations ns_file_operations = {
@@ -616,4 +624,6 @@ void __init nsfs_init(void)if (IS_ERR (nsfs_mnt))panic ("can't set nsfs up\n" ); nsfs_mnt->mnt_sb->s_flags &= ~SB_NOUSER ; + nsfs_root_path.mnt = nsfs_mnt; + nsfs_root_path.dentry = nsfs_mnt->mnt_root; }
include/uapi/linux/fcntl.h +1 -0
@@ -111,6 +111,7 @@#define PIDFD_SELF_THREAD_GROUP -10001/* Current thread group leader. */ #define FD_PIDFS_ROOT -10002/* Root of the pidfs filesystem */ +#define FD_NSFS_ROOT -10003/* Root of the nsfs filesystem */ #define FD_INVALID -10009/* Invalid file descriptor: -10000 - EBADF = -10009 */ /* Generic flags for the *at(2) family of syscalls. */
Signature · 1 line
2.47.3
Re: [PATCH 28/32] nsfs: support exhaustive file handles
Quoted 7 lines · Christian Brauner
Pidfd file handles are exhaustive meaning they don't require a handle on another pidfd to pass to
open_by_handle_at()so it can derive the filesystem to decode in. Instead it can be derived from the file handle itself. The same is possible for namespace file handles.Signed-off-by Christian Brauner <[email protected]>
Quoted 82 lines
--- fs/fhandle.c | 6 ++++++ fs/internal.h | 1 + fs/nsfs.c | 10 ++++++++++ include/uapi/linux/fcntl.h | 1 + 4 files changed, 18 insertions(+)fs/fhandle.c +6
-0@@ -11,6 +11,7 @@#include <linux/personality.h> #include <linux/uaccess.h> #include <linux/compat.h> +#include <linux/nsfs.h> #include "internal.h" #include "mount.h" @@ -189,6 +190,11 @@ static int get_path_anchor(int fd, struct path *root)return 0 ; } +if (fd ==FD_NSFS_ROOT ) { +nsfs_get_root (root); +return 0 ; + } +return -EBADF ; }fs/internal.h +1
-0@@ -355,3 +355,4 @@ int anon_inode_getattr(struct mnt_idmap *idmap, const struct path *path,int anon_inode_setattr (struct mnt_idmap *idmap,struct dentry *dentry,struct iattr *attr);void pidfs_get_root (struct path *path); +void nsfs_get_root (struct path *path);fs/nsfs.c +10
-0@@ -25,6 +25,14 @@static struct vfsmount *nsfs_mnt; +static struct path nsfs_root_path = {}; + +void nsfs_get_root (struct path *path) +{ + *path = nsfs_root_path; +path_get (path); +} +static long ns_ioctl (struct file *filp,unsigned int ioctl,unsigned long arg);static const struct file_operations ns_file_operations = {@@ -616,4 +624,6 @@ void __init nsfs_init(void)if (IS_ERR (nsfs_mnt))panic ("can't set nsfs up\n" ); nsfs_mnt->mnt_sb->s_flags &= ~SB_NOUSER ; + nsfs_root_path.mnt = nsfs_mnt; + nsfs_root_path.dentry = nsfs_mnt->mnt_root; }include/uapi/linux/fcntl.h +1
-1@@ -111,6 +111,7 @@#define PIDFD_SELF_THREAD_GROUP -10001/* Current thread group leader. */ #define FD_PIDFS_ROOT -10002/* Root of the pidfs filesystem */ +#define FD_NSFS_ROOT -10003/* Root of the nsfs filesystem */ #define FD_INVALID -10009/* Invalid file descriptor: -10000 - EBADF = -10009 */ /* Generic flags for the *at(2) family of syscalls. */ --2.47.3
Christian Brauner
maintainerPATCH 29/32 nsfs: add missing id retrieval support
In reply to Christian BraunerThe mount namespace has supported id retrieval for a while already. Add support for the other types as well.
--- fs/nsfs.c | 74 +++++++++++++++++++++++++++++++++++++++-------- include/uapi/linux/nsfs.h | 12 ++++++-- 2 files changed, 72 insertions(+), 14 deletions(-)
fs/nsfs.c +62 -12
@@ -173,6 +173,13 @@ static bool nsfs_ioctl_valid(unsigned int cmd)case NS_GET_NSTYPE :case NS_GET_OWNER_UID :case NS_GET_MNTNS_ID : +case NS_GET_NETNS_ID : +case NS_GET_CGROUPNS_ID : +case NS_GET_IPCNS_ID : +case NS_GET_UTSNS_ID : +case NS_GET_PIDNS_ID : +case NS_GET_TIMENS_ID : +case NS_GET_USERNS_ID :case NS_GET_PID_FROM_PIDNS :case NS_GET_TGID_FROM_PIDNS :case NS_GET_PID_IN_PIDNS :
@@ -226,18 +233,6 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl, argp = (uid_t __user *) arg; uid =from_kuid_munged (current_user_ns (), user_ns->owner);return put_user (uid, argp); -case NS_GET_MNTNS_ID : { -__u64 __user *idp; -__u64 id; - -if (ns->ops->type !=CLONE_NEWNS ) -return -EINVAL ; - - mnt_ns = container_of(ns, struct mnt_namespace, ns); - idp = (__u64 __user *)arg; - id = mnt_ns->ns.ns_id; -return put_user (id, idp); - }case NS_GET_PID_FROM_PIDNS : fallthrough;case NS_GET_TGID_FROM_PIDNS :
@@ -283,6 +278,61 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl, ret = -ESRCH ;return ret; } +case NS_GET_MNTNS_ID : + fallthrough; +case NS_GET_NETNS_ID : + fallthrough; +case NS_GET_CGROUPNS_ID : + fallthrough; +case NS_GET_IPCNS_ID : + fallthrough; +case NS_GET_UTSNS_ID : + fallthrough; +case NS_GET_PIDNS_ID : + fallthrough; +case NS_GET_TIMENS_ID : + fallthrough; +case NS_GET_USERNS_ID : { +__u64 __user *idp; +__u64 id; +int expected_type; + +switch (ioctl) { +case NS_GET_MNTNS_ID : + expected_type =CLONE_NEWNS ; +break ; +case NS_GET_NETNS_ID : + expected_type =CLONE_NEWNET ; +break ; +case NS_GET_CGROUPNS_ID : + expected_type =CLONE_NEWCGROUP ; +break ; +case NS_GET_IPCNS_ID : + expected_type =CLONE_NEWIPC ; +break ; +case NS_GET_UTSNS_ID : + expected_type =CLONE_NEWUTS ; +break ; +case NS_GET_PIDNS_ID : + expected_type =CLONE_NEWPID ; +break ; +case NS_GET_TIMENS_ID : + expected_type =CLONE_NEWTIME ; +break ; +case NS_GET_USERNS_ID : + expected_type =CLONE_NEWUSER ; +break ; +default : +return -EINVAL ; + } + +if (ns->ops->type != expected_type) +return -EINVAL ; + + idp = (__u64 __user *)arg; + id = ns->ns_id; +return put_user (id, idp); + } }/* extensible ioctls */
include/uapi/linux/nsfs.h +10 -2
@@ -16,8 +16,6 @@#define NS_GET_NSTYPE _IO(NSIO, 0x3)/* Get owner UID (in the caller's user namespace) for a user namespace */ #define NS_GET_OWNER_UID _IO(NSIO, 0x4) -/* Get the id for a mount namespace */ -#define NS_GET_MNTNS_ID _IOR(NSIO, 0x5, __u64)/* Translate pid from target pid namespace into the caller's pid namespace. */ #define NS_GET_PID_FROM_PIDNS _IOR(NSIO, 0x6, int)/* Return thread-group leader id of pid in the callers pid namespace. */
@@ -42,6 +40,16 @@ struct mnt_ns_info {/* Get previous namespace. */ #define NS_MNT_GET_PREV _IOR(NSIO, 12, struct mnt_ns_info) +/* Retrieve namespace identifiers. */ +#define NS_GET_MNTNS_ID _IOR(NSIO, 5, __u64) +#define NS_GET_NETNS_ID _IOR(NSIO, 13, __u64) +#define NS_GET_CGROUPNS_ID _IOR(NSIO, 14, __u64) +#define NS_GET_IPCNS_ID _IOR(NSIO, 15, __u64) +#define NS_GET_UTSNS_ID _IOR(NSIO, 16, __u64) +#define NS_GET_PIDNS_ID _IOR(NSIO, 17, __u64) +#define NS_GET_TIMENS_ID _IOR(NSIO, 18, __u64) +#define NS_GET_USERNS_ID _IOR(NSIO, 19, __u64) +enum init_ns_ino {IPC_NS_INIT_INO =0xEFFFFFFFU ,UTS_NS_INIT_INO =0xEFFFFFFEU ,
Signature · 1 line
2.47.3
Re: [PATCH 29/32] nsfs: add missing id retrieval support
Quoted 90 lines · Christian Brauner
The mount namespace has supported id retrieval for a while already. Add support for the other types as well.
Signed-off-by Christian Brauner <[email protected]>--- fs/nsfs.c | 74 +++++++++++++++++++++++++++++++++++++++-------- include/uapi/linux/nsfs.h | 12 ++++++-- 2 files changed, 72 insertions(+), 14 deletions(-)fs/nsfs.c +47
-12@@ -173,6 +173,13 @@ static bool nsfs_ioctl_valid(unsigned int cmd)case NS_GET_NSTYPE :case NS_GET_OWNER_UID :case NS_GET_MNTNS_ID : +case NS_GET_NETNS_ID : +case NS_GET_CGROUPNS_ID : +case NS_GET_IPCNS_ID : +case NS_GET_UTSNS_ID : +case NS_GET_PIDNS_ID : +case NS_GET_TIMENS_ID : +case NS_GET_USERNS_ID :case NS_GET_PID_FROM_PIDNS :case NS_GET_TGID_FROM_PIDNS :case NS_GET_PID_IN_PIDNS :@@ -226,18 +233,6 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl, argp = (uid_t __user *) arg; uid =from_kuid_munged (current_user_ns (), user_ns->owner);return put_user (uid, argp); -case NS_GET_MNTNS_ID : { -__u64 __user *idp; -__u64 id; - -if (ns->ops->type !=CLONE_NEWNS ) -return -EINVAL ; - - mnt_ns = container_of(ns, struct mnt_namespace, ns); - idp = (__u64 __user *)arg; - id = mnt_ns->ns.ns_id; -return put_user (id, idp); - }case NS_GET_PID_FROM_PIDNS : fallthrough;case NS_GET_TGID_FROM_PIDNS :@@ -283,6 +278,61 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl, ret = -ESRCH ;return ret; } +case NS_GET_MNTNS_ID : + fallthrough; +case NS_GET_NETNS_ID : + fallthrough; +case NS_GET_CGROUPNS_ID : + fallthrough; +case NS_GET_IPCNS_ID : + fallthrough; +case NS_GET_UTSNS_ID : + fallthrough; +case NS_GET_PIDNS_ID : + fallthrough; +case NS_GET_TIMENS_ID : + fallthrough; +case NS_GET_USERNS_ID : { +__u64 __user *idp; +__u64 id; +int expected_type; + +switch (ioctl) { +case NS_GET_MNTNS_ID : + expected_type =CLONE_NEWNS ; +break ; +case NS_GET_NETNS_ID : + expected_type =CLONE_NEWNET ; +break ; +case NS_GET_CGROUPNS_ID : + expected_type =CLONE_NEWCGROUP ; +break ; +case NS_GET_IPCNS_ID : + expected_type =CLONE_NEWIPC ; +break ; +case NS_GET_UTSNS_ID : + expected_type =CLONE_NEWUTS ; +break ; +case NS_GET_PIDNS_ID : + expected_type =CLONE_NEWPID ; +break ; +case NS_GET_TIMENS_ID : + expected_type =CLONE_NEWTIME ;
fs/nsfs.c static long ns_ioctl(struct file *filp, unsigned int ioctl, · line 330
Quoted 10 lines
fs/nsfs.c +10
-0+break ; +case NS_GET_USERNS_ID : + expected_type =CLONE_NEWUSER ; +break ; +default : +return -EINVAL ; + } + +if (ns->ops->type != expected_type) +return -EINVAL ;
While I get that having this be per-ns-type lets programs avoid being tricked into thinking that one namespace ID is actually another namespace, it feels a bit ugly to have to add a new ioctl for every new namespace.
If we added a way to get the CLONE_* flag for a namespace (NS_GET_TYPE) we could have just NS_GET_ID. Of course, we would have to trust userspace to do the right thing...
Quoted 42 lines
fs/nsfs.c +5
-0+ + idp = (__u64 __user *)arg; + id = ns->ns_id; +return put_user (id, idp); + } }/* extensible ioctls */ include/uapi/linux/nsfs.h +10
-2@@ -16,8 +16,6 @@#define NS_GET_NSTYPE _IO(NSIO, 0x3)/* Get owner UID (in the caller's user namespace) for a user namespace */ #define NS_GET_OWNER_UID _IO(NSIO, 0x4) -/* Get the id for a mount namespace */ -#define NS_GET_MNTNS_ID _IOR(NSIO, 0x5, __u64)/* Translate pid from target pid namespace into the caller's pid namespace. */ #define NS_GET_PID_FROM_PIDNS _IOR(NSIO, 0x6, int)/* Return thread-group leader id of pid in the callers pid namespace. */ @@ -42,6 +40,16 @@ struct mnt_ns_info {/* Get previous namespace. */ #define NS_MNT_GET_PREV _IOR(NSIO, 12, struct mnt_ns_info) +/* Retrieve namespace identifiers. */ +#define NS_GET_MNTNS_ID _IOR(NSIO, 5, __u64) +#define NS_GET_NETNS_ID _IOR(NSIO, 13, __u64) +#define NS_GET_CGROUPNS_ID _IOR(NSIO, 14, __u64) +#define NS_GET_IPCNS_ID _IOR(NSIO, 15, __u64) +#define NS_GET_UTSNS_ID _IOR(NSIO, 16, __u64) +#define NS_GET_PIDNS_ID _IOR(NSIO, 17, __u64) +#define NS_GET_TIMENS_ID _IOR(NSIO, 18, __u64) +#define NS_GET_USERNS_ID _IOR(NSIO, 19, __u64) +enum init_ns_ino {IPC_NS_INIT_INO =0xEFFFFFFFU ,UTS_NS_INIT_INO =0xEFFFFFFEU ,Signature · 2 lines
-- 2.47.3
Signature · 4 lines
Aleksa Sarai Senior Software Engineer (Containers) SUSE Linux GmbH https://www.cyphar.com/
Christian Brauner
maintainerRe: [PATCH 29/32] nsfs: add missing id retrieval support
Quoted 107 lines · Aleksa Sarai
Quoted 90 lines · Christian Brauner
The mount namespace has supported id retrieval for a while already. Add support for the other types as well.
Signed-off-by Christian Brauner <[email protected]>--- fs/nsfs.c | 74 +++++++++++++++++++++++++++++++++++++++-------- include/uapi/linux/nsfs.h | 12 ++++++-- 2 files changed, 72 insertions(+), 14 deletions(-)fs/nsfs.c +47
-12@@ -173,6 +173,13 @@ static bool nsfs_ioctl_valid(unsigned int cmd)case NS_GET_NSTYPE :case NS_GET_OWNER_UID :case NS_GET_MNTNS_ID : +case NS_GET_NETNS_ID : +case NS_GET_CGROUPNS_ID : +case NS_GET_IPCNS_ID : +case NS_GET_UTSNS_ID : +case NS_GET_PIDNS_ID : +case NS_GET_TIMENS_ID : +case NS_GET_USERNS_ID :case NS_GET_PID_FROM_PIDNS :case NS_GET_TGID_FROM_PIDNS :case NS_GET_PID_IN_PIDNS :@@ -226,18 +233,6 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl, argp = (uid_t __user *) arg; uid =from_kuid_munged (current_user_ns (), user_ns->owner);return put_user (uid, argp); -case NS_GET_MNTNS_ID : { -__u64 __user *idp; -__u64 id; - -if (ns->ops->type !=CLONE_NEWNS ) -return -EINVAL ; - - mnt_ns = container_of(ns, struct mnt_namespace, ns); - idp = (__u64 __user *)arg; - id = mnt_ns->ns.ns_id; -return put_user (id, idp); - }case NS_GET_PID_FROM_PIDNS : fallthrough;case NS_GET_TGID_FROM_PIDNS :@@ -283,6 +278,61 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl, ret = -ESRCH ;return ret; } +case NS_GET_MNTNS_ID : + fallthrough; +case NS_GET_NETNS_ID : + fallthrough; +case NS_GET_CGROUPNS_ID : + fallthrough; +case NS_GET_IPCNS_ID : + fallthrough; +case NS_GET_UTSNS_ID : + fallthrough; +case NS_GET_PIDNS_ID : + fallthrough; +case NS_GET_TIMENS_ID : + fallthrough; +case NS_GET_USERNS_ID : { +__u64 __user *idp; +__u64 id; +int expected_type; + +switch (ioctl) { +case NS_GET_MNTNS_ID : + expected_type =CLONE_NEWNS ; +break ; +case NS_GET_NETNS_ID : + expected_type =CLONE_NEWNET ; +break ; +case NS_GET_CGROUPNS_ID : + expected_type =CLONE_NEWCGROUP ; +break ; +case NS_GET_IPCNS_ID : + expected_type =CLONE_NEWIPC ; +break ; +case NS_GET_UTSNS_ID : + expected_type =CLONE_NEWUTS ; +break ; +case NS_GET_PIDNS_ID : + expected_type =CLONE_NEWPID ; +break ; +case NS_GET_TIMENS_ID : + expected_type =CLONE_NEWTIME ;fs/nsfs.c static long ns_ioctl(struct file *filp, unsigned int ioctl, · line 330
Quoted 10 lines
fs/nsfs.c +10
-0+break ; +case NS_GET_USERNS_ID : + expected_type =CLONE_NEWUSER ; +break ; +default : +return -EINVAL ; + } + +if (ns->ops->type != expected_type) +return -EINVAL ;While I get that having this be per-ns-type lets programs avoid being tricked into thinking that one namespace ID is actually another namespace, it feels a bit ugly to have to add a new ioctl for every new namespace.
Quoted 1 line
If we added a way to get the CLONE_* flag for a namespace (
NS_GET_TYPE)
That exists afaict: NS_GET_NSTYPE.
Quoted 2 lines
we could have just
NS_GET_ID. Of course, we would have to trust userspace to do the right thing...
So NS_GET_ID can just return the id and be done with it. If userspace wants to know what type it is they can issue a separate ioctl. But since the id space is shared all ids of all namespaces can be compared with each other reliably. So really for comparision you wouldn't need to care. IOW, yes.
Quoted 49 lines
Quoted 43 lines
fs/nsfs.c +5
-0+ + idp = (__u64 __user *)arg; + id = ns->ns_id; +return put_user (id, idp); + } }/* extensible ioctls */ include/uapi/linux/nsfs.h +10
-2@@ -16,8 +16,6 @@#define NS_GET_NSTYPE _IO(NSIO, 0x3)/* Get owner UID (in the caller's user namespace) for a user namespace */ #define NS_GET_OWNER_UID _IO(NSIO, 0x4) -/* Get the id for a mount namespace */ -#define NS_GET_MNTNS_ID _IOR(NSIO, 0x5, __u64)/* Translate pid from target pid namespace into the caller's pid namespace. */ #define NS_GET_PID_FROM_PIDNS _IOR(NSIO, 0x6, int)/* Return thread-group leader id of pid in the callers pid namespace. */ @@ -42,6 +40,16 @@ struct mnt_ns_info {/* Get previous namespace. */ #define NS_MNT_GET_PREV _IOR(NSIO, 12, struct mnt_ns_info) +/* Retrieve namespace identifiers. */ +#define NS_GET_MNTNS_ID _IOR(NSIO, 5, __u64) +#define NS_GET_NETNS_ID _IOR(NSIO, 13, __u64) +#define NS_GET_CGROUPNS_ID _IOR(NSIO, 14, __u64) +#define NS_GET_IPCNS_ID _IOR(NSIO, 15, __u64) +#define NS_GET_UTSNS_ID _IOR(NSIO, 16, __u64) +#define NS_GET_PIDNS_ID _IOR(NSIO, 17, __u64) +#define NS_GET_TIMENS_ID _IOR(NSIO, 18, __u64) +#define NS_GET_USERNS_ID _IOR(NSIO, 19, __u64) +enum init_ns_ino {IPC_NS_INIT_INO =0xEFFFFFFFU ,UTS_NS_INIT_INO =0xEFFFFFFEU ,Signature · 2 lines
-- 2.47.3Signature · 4 lines
Aleksa Sarai Senior Software Engineer (Containers) SUSE Linux GmbH https://www.cyphar.com/
Re: [PATCH 29/32] nsfs: add missing id retrieval support
Quoted 108 lines · Christian Brauner
Quoted 107 lines · Aleksa Sarai
Quoted 90 lines · Christian Brauner
The mount namespace has supported id retrieval for a while already. Add support for the other types as well.
Signed-off-by Christian Brauner <[email protected]>--- fs/nsfs.c | 74 +++++++++++++++++++++++++++++++++++++++-------- include/uapi/linux/nsfs.h | 12 ++++++-- 2 files changed, 72 insertions(+), 14 deletions(-)fs/nsfs.c +47
-12@@ -173,6 +173,13 @@ static bool nsfs_ioctl_valid(unsigned int cmd)case NS_GET_NSTYPE :case NS_GET_OWNER_UID :case NS_GET_MNTNS_ID : +case NS_GET_NETNS_ID : +case NS_GET_CGROUPNS_ID : +case NS_GET_IPCNS_ID : +case NS_GET_UTSNS_ID : +case NS_GET_PIDNS_ID : +case NS_GET_TIMENS_ID : +case NS_GET_USERNS_ID :case NS_GET_PID_FROM_PIDNS :case NS_GET_TGID_FROM_PIDNS :case NS_GET_PID_IN_PIDNS :@@ -226,18 +233,6 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl, argp = (uid_t __user *) arg; uid =from_kuid_munged (current_user_ns (), user_ns->owner);return put_user (uid, argp); -case NS_GET_MNTNS_ID : { -__u64 __user *idp; -__u64 id; - -if (ns->ops->type !=CLONE_NEWNS ) -return -EINVAL ; - - mnt_ns = container_of(ns, struct mnt_namespace, ns); - idp = (__u64 __user *)arg; - id = mnt_ns->ns.ns_id; -return put_user (id, idp); - }case NS_GET_PID_FROM_PIDNS : fallthrough;case NS_GET_TGID_FROM_PIDNS :@@ -283,6 +278,61 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl, ret = -ESRCH ;return ret; } +case NS_GET_MNTNS_ID : + fallthrough; +case NS_GET_NETNS_ID : + fallthrough; +case NS_GET_CGROUPNS_ID : + fallthrough; +case NS_GET_IPCNS_ID : + fallthrough; +case NS_GET_UTSNS_ID : + fallthrough; +case NS_GET_PIDNS_ID : + fallthrough; +case NS_GET_TIMENS_ID : + fallthrough; +case NS_GET_USERNS_ID : { +__u64 __user *idp; +__u64 id; +int expected_type; + +switch (ioctl) { +case NS_GET_MNTNS_ID : + expected_type =CLONE_NEWNS ; +break ; +case NS_GET_NETNS_ID : + expected_type =CLONE_NEWNET ; +break ; +case NS_GET_CGROUPNS_ID : + expected_type =CLONE_NEWCGROUP ; +break ; +case NS_GET_IPCNS_ID : + expected_type =CLONE_NEWIPC ; +break ; +case NS_GET_UTSNS_ID : + expected_type =CLONE_NEWUTS ; +break ; +case NS_GET_PIDNS_ID : + expected_type =CLONE_NEWPID ; +break ; +case NS_GET_TIMENS_ID : + expected_type =CLONE_NEWTIME ;fs/nsfs.c static long ns_ioctl(struct file *filp, unsigned int ioctl, · line 330
Quoted 10 lines
fs/nsfs.c +10
-0+break ; +case NS_GET_USERNS_ID : + expected_type =CLONE_NEWUSER ; +break ; +default : +return -EINVAL ; + } + +if (ns->ops->type != expected_type) +return -EINVAL ;While I get that having this be per-ns-type lets programs avoid being tricked into thinking that one namespace ID is actually another namespace, it feels a bit ugly to have to add a new ioctl for every new namespace.
Quoted 3 lines
Quoted 1 line
If we added a way to get the CLONE_* flag for a namespace (
NS_GET_TYPE)That exists afaict:
NS_GET_NSTYPE.
D'oh, yeah that's all you need.
Quoted 8 lines
Quoted 2 lines
we could have just
NS_GET_ID. Of course, we would have to trust userspace to do the right thing...So
NS_GET_IDcan just return the id and be done with it. If userspace wants to know what type it is they can issue a separate ioctl. But since the id space is shared all ids of all namespaces can be compared with each other reliably. So really for comparision you wouldn't need to care. IOW, yes.
Ah, I didn't realise they're all in the same id-space -- in that case it makes even more sense to just have a single NS_GET_ID IMHO.
Quoted 50 lines
Quoted 48 lines
Quoted 42 lines
fs/nsfs.c +5
-0+ + idp = (__u64 __user *)arg; + id = ns->ns_id; +return put_user (id, idp); + } }/* extensible ioctls */ include/uapi/linux/nsfs.h +10
-2@@ -16,8 +16,6 @@#define NS_GET_NSTYPE _IO(NSIO, 0x3)/* Get owner UID (in the caller's user namespace) for a user namespace */ #define NS_GET_OWNER_UID _IO(NSIO, 0x4) -/* Get the id for a mount namespace */ -#define NS_GET_MNTNS_ID _IOR(NSIO, 0x5, __u64)/* Translate pid from target pid namespace into the caller's pid namespace. */ #define NS_GET_PID_FROM_PIDNS _IOR(NSIO, 0x6, int)/* Return thread-group leader id of pid in the callers pid namespace. */ @@ -42,6 +40,16 @@ struct mnt_ns_info {/* Get previous namespace. */ #define NS_MNT_GET_PREV _IOR(NSIO, 12, struct mnt_ns_info) +/* Retrieve namespace identifiers. */ +#define NS_GET_MNTNS_ID _IOR(NSIO, 5, __u64) +#define NS_GET_NETNS_ID _IOR(NSIO, 13, __u64) +#define NS_GET_CGROUPNS_ID _IOR(NSIO, 14, __u64) +#define NS_GET_IPCNS_ID _IOR(NSIO, 15, __u64) +#define NS_GET_UTSNS_ID _IOR(NSIO, 16, __u64) +#define NS_GET_PIDNS_ID _IOR(NSIO, 17, __u64) +#define NS_GET_TIMENS_ID _IOR(NSIO, 18, __u64) +#define NS_GET_USERNS_ID _IOR(NSIO, 19, __u64) +enum init_ns_ino {IPC_NS_INIT_INO =0xEFFFFFFFU ,UTS_NS_INIT_INO =0xEFFFFFFEU ,Signature · 2 lines
-- 2.47.3Signature · 4 lines
Aleksa Sarai Senior Software Engineer (Containers) SUSE Linux GmbH https://www.cyphar.com/
Signature · 4 lines
Aleksa Sarai Senior Software Engineer (Containers) SUSE Linux GmbH https://www.cyphar.com/
Christian Brauner
maintainerPATCH 30/32 tools: update nsfs.h uapi header
In reply to Christian BraunerUpdate the nsfs.h tools header to the uapi/nsfs.h header so we can rely on it in the selftests.
--- tools/include/uapi/linux/nsfs.h | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-)
tools/include/uapi/linux/nsfs.h +21 -2
@@ -16,8 +16,6 @@#define NS_GET_NSTYPE _IO(NSIO, 0x3)/* Get owner UID (in the caller's user namespace) for a user namespace */ #define NS_GET_OWNER_UID _IO(NSIO, 0x4) -/* Get the id for a mount namespace */ -#define NS_GET_MNTNS_ID _IOR(NSIO, 0x5, __u64)/* Translate pid from target pid namespace into the caller's pid namespace. */ #define NS_GET_PID_FROM_PIDNS _IOR(NSIO, 0x6, int)/* Return thread-group leader id of pid in the callers pid namespace. */
@@ -42,4 +40,25 @@ struct mnt_ns_info {/* Get previous namespace. */ #define NS_MNT_GET_PREV _IOR(NSIO, 12, struct mnt_ns_info) +/* Retrieve namespace identifiers. */ +#define NS_GET_MNTNS_ID _IOR(NSIO, 5, __u64) +#define NS_GET_NETNS_ID _IOR(NSIO, 13, __u64) +#define NS_GET_CGROUPNS_ID _IOR(NSIO, 14, __u64) +#define NS_GET_IPCNS_ID _IOR(NSIO, 15, __u64) +#define NS_GET_UTSNS_ID _IOR(NSIO, 16, __u64) +#define NS_GET_PIDNS_ID _IOR(NSIO, 17, __u64) +#define NS_GET_TIMENS_ID _IOR(NSIO, 18, __u64) +#define NS_GET_USERNS_ID _IOR(NSIO, 19, __u64) + +enum init_ns_ino { +IPC_NS_INIT_INO =0xEFFFFFFFU , +UTS_NS_INIT_INO =0xEFFFFFFEU , +USER_NS_INIT_INO =0xEFFFFFFDU , +PID_NS_INIT_INO =0xEFFFFFFCU , +CGROUP_NS_INIT_INO =0xEFFFFFFBU , +TIME_NS_INIT_INO =0xEFFFFFFAU , +NET_NS_INIT_INO =0xEFFFFFF9U , +MNT_NS_INIT_INO =0xEFFFFFF8U , +}; +#endif /* __LINUX_NSFS_H */
Signature · 1 line
2.47.3
Christian Brauner
maintainerPATCH 31/32 selftests/namespaces: add identifier selftests
In reply to Christian BraunerAdd a bunch of selftests for the identifier retrieval ioctls.
--- tools/testing/selftests/namespaces/.gitignore | 1 + tools/testing/selftests/namespaces/Makefile | 7 + tools/testing/selftests/namespaces/config | 7 + tools/testing/selftests/namespaces/nsid_test.c | 986 +++++++++++++++++++++++++ 4 files changed, 1001 insertions(+)
tools/testing/selftests/namespaces/.gitignore new file +1 -0
@@ -0,0 +1 @@ +nsid_test
tools/testing/selftests/namespaces/Makefile new file +7 -0
@@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0-only +CFLAGS += -Wall -O0 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES) + +TEST_GEN_PROGS := nsid_test + +include ../lib.mk +
tools/testing/selftests/namespaces/config new file +7 -0
@@ -0,0 +1,7 @@ +CONFIG_UTS_NS=y +CONFIG_TIME_NS=y +CONFIG_IPC_NS=y +CONFIG_USER_NS=y +CONFIG_PID_NS=y +CONFIG_NET_NS=y +CONFIG_CGROUPS=y
tools/testing/selftests/namespaces/nsid_test.c new file +986 -0
986 lines not on this page · raw message
Signature · 1 line
2.47.3
Christian Brauner
maintainerPATCH 32/32 selftests/namespaces: add file handle selftests
In reply to Christian BraunerAdd a bunch of selftests for namespace file handles.
--- tools/testing/selftests/namespaces/.gitignore | 1 + tools/testing/selftests/namespaces/Makefile | 2 +- .../selftests/namespaces/file_handle_test.c | 1410 ++++++++++++++++++++ 3 files changed, 1412 insertions(+), 1 deletion(-)
tools/testing/selftests/namespaces/.gitignore +1 -0
@@ -1 +1,2 @@ nsid_test +file_handle_test
tools/testing/selftests/namespaces/Makefile +1 -1
@@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only CFLAGS += -Wall -O0 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES) -TEST_GEN_PROGS := nsid_test +TEST_GEN_PROGS := nsid_test file_handle_test include ../lib.mk
tools/testing/selftests/namespaces/file_handle_test.c new file +1410 -0
1,410 lines not on this page · raw message
Signature · 1 line
2.47.3
Re: [PATCH 32/32] selftests/namespaces: add file handle selftests
Quoted 4 lines · Christian Brauner
Add a bunch of selftests for namespace file handles.
Signed-off-by Christian Brauner <[email protected]>
Obviously, I did not go over every single line, but for the general test template and test coverage you may add:
However, see my comment on file handle support patch. The test matrix is incomplete.
Maybe it would be complete if test is run as root and then as non root, but then I think the test needs some changes for running as root and opening non-self ns.
I am not sure what the standard is wrt running the selftests as root /non-root.
I see that the userns isolation tests do:
/* Map current uid/gid to root in the new namespace */
Are you assuming that non root is running this test or am I missing something?
Wouldn't mapping uid 0 to uid 0 in the new userns cause the test to fail because opening by handle will succeed?
Thanks,
Amir.
Quoted 1,446 lines
--- tools/testing/selftests/namespaces/.gitignore | 1 + tools/testing/selftests/namespaces/Makefile | 2 +- .../selftests/namespaces/file_handle_test.c | 1410 ++++++++++++++++++++ 3 files changed, 1412 insertions(+), 1 deletion(-)tools/testing/selftests/namespaces/.gitignore +1
-0@@ -1 +1,2 @@ nsid_test +file_handle_testtools/testing/selftests/namespaces/Makefile +1
-1@@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only CFLAGS += -Wall -O0 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES) -TEST_GEN_PROGS := nsid_test +TEST_GEN_PROGS := nsid_test file_handle_test include ../lib.mktools/testing/selftests/namespaces/file_handle_test.c new file +1410
-1@@ -0,0 +1,1410 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include <errno.h> +#include <fcntl.h> +#include <grp.h> +#include <limits.h> +#include <sched.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/mount.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> +#include <linux/unistd.h> +#include "../kselftest_harness.h" + +#ifndef FD_NSFS_ROOT +#define FD_NSFS_ROOT -10003/* Root of the nsfs filesystem */ +#endif + +TEST (nsfs_net_handle) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +char ns_path[256 ]; +struct stat st1, st2; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Open a namespace file descriptor */ +snprintf (ns_path,sizeof (ns_path),"/proc/self/ns/net" ); + ns_fd =open (ns_path,O_RDONLY ); +ASSERT_GE (ns_fd,0 ); + +/* Get handle for the namespace */ + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +ASSERT_GT (handle->handle_bytes,0 ); + +/* Try to open using FD_NSFS_ROOT */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); +if (fd <0 && (errno ==EINVAL || errno ==EOPNOTSUPP )) { + SKIP(free(handle); close(ns_fd); + return, + "open_by_handle_at with FD_NSFS_ROOT not supported"); + } +ASSERT_GE (fd,0 ); + +/* Verify we opened the correct namespace */ +ASSERT_EQ (fstat (ns_fd, &st1),0 ); +ASSERT_EQ (fstat (fd, &st2),0 ); +ASSERT_EQ (st1.st_ino, st2.st_ino); +ASSERT_EQ (st1.st_dev, st2.st_dev); + +close (fd); +close (ns_fd); +free (handle); +} + +TEST (nsfs_uts_handle) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +char ns_path[256 ]; +struct stat st1, st2; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Open UTS namespace file descriptor */ +snprintf (ns_path,sizeof (ns_path),"/proc/self/ns/uts" ); + ns_fd =open (ns_path,O_RDONLY ); +ASSERT_GE (ns_fd,0 ); + +/* Get handle for the namespace */ + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +ASSERT_GT (handle->handle_bytes,0 ); + +/* Try to open using FD_NSFS_ROOT */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); +if (fd <0 && (errno ==EINVAL || errno ==EOPNOTSUPP )) { + SKIP(free(handle); close(ns_fd); + return, + "open_by_handle_at with FD_NSFS_ROOT not supported"); + } +ASSERT_GE (fd,0 ); + +/* Verify we opened the correct namespace */ +ASSERT_EQ (fstat (ns_fd, &st1),0 ); +ASSERT_EQ (fstat (fd, &st2),0 ); +ASSERT_EQ (st1.st_ino, st2.st_ino); +ASSERT_EQ (st1.st_dev, st2.st_dev); + +close (fd); +close (ns_fd); +free (handle); +} + +TEST (nsfs_ipc_handle) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +char ns_path[256 ]; +struct stat st1, st2; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Open IPC namespace file descriptor */ +snprintf (ns_path,sizeof (ns_path),"/proc/self/ns/ipc" ); + ns_fd =open (ns_path,O_RDONLY ); +ASSERT_GE (ns_fd,0 ); + +/* Get handle for the namespace */ + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +ASSERT_GT (handle->handle_bytes,0 ); + +/* Try to open using FD_NSFS_ROOT */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); +if (fd <0 && (errno ==EINVAL || errno ==EOPNOTSUPP )) { + SKIP(free(handle); close(ns_fd); + return, + "open_by_handle_at with FD_NSFS_ROOT not supported"); + } +ASSERT_GE (fd,0 ); + +/* Verify we opened the correct namespace */ +ASSERT_EQ (fstat (ns_fd, &st1),0 ); +ASSERT_EQ (fstat (fd, &st2),0 ); +ASSERT_EQ (st1.st_ino, st2.st_ino); +ASSERT_EQ (st1.st_dev, st2.st_dev); + +close (fd); +close (ns_fd); +free (handle); +} + +TEST (nsfs_pid_handle) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +char ns_path[256 ]; +struct stat st1, st2; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Open PID namespace file descriptor */ +snprintf (ns_path,sizeof (ns_path),"/proc/self/ns/pid" ); + ns_fd =open (ns_path,O_RDONLY ); +ASSERT_GE (ns_fd,0 ); + +/* Get handle for the namespace */ + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +ASSERT_GT (handle->handle_bytes,0 ); + +/* Try to open using FD_NSFS_ROOT */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); +if (fd <0 && (errno ==EINVAL || errno ==EOPNOTSUPP )) { + SKIP(free(handle); close(ns_fd); + return, + "open_by_handle_at with FD_NSFS_ROOT not supported"); + } +ASSERT_GE (fd,0 ); + +/* Verify we opened the correct namespace */ +ASSERT_EQ (fstat (ns_fd, &st1),0 ); +ASSERT_EQ (fstat (fd, &st2),0 ); +ASSERT_EQ (st1.st_ino, st2.st_ino); +ASSERT_EQ (st1.st_dev, st2.st_dev); + +close (fd); +close (ns_fd); +free (handle); +} + +TEST (nsfs_mnt_handle) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +char ns_path[256 ]; +struct stat st1, st2; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Open mount namespace file descriptor */ +snprintf (ns_path,sizeof (ns_path),"/proc/self/ns/mnt" ); + ns_fd =open (ns_path,O_RDONLY ); +ASSERT_GE (ns_fd,0 ); + +/* Get handle for the namespace */ + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +ASSERT_GT (handle->handle_bytes,0 ); + +/* Try to open using FD_NSFS_ROOT */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); +if (fd <0 && (errno ==EINVAL || errno ==EOPNOTSUPP )) { + SKIP(free(handle); close(ns_fd); + return, + "open_by_handle_at with FD_NSFS_ROOT not supported"); + } +ASSERT_GE (fd,0 ); + +/* Verify we opened the correct namespace */ +ASSERT_EQ (fstat (ns_fd, &st1),0 ); +ASSERT_EQ (fstat (fd, &st2),0 ); +ASSERT_EQ (st1.st_ino, st2.st_ino); +ASSERT_EQ (st1.st_dev, st2.st_dev); + +close (fd); +close (ns_fd); +free (handle); +} + +TEST (nsfs_user_handle) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +char ns_path[256 ]; +struct stat st1, st2; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Open user namespace file descriptor */ +snprintf (ns_path,sizeof (ns_path),"/proc/self/ns/user" ); + ns_fd =open (ns_path,O_RDONLY ); +ASSERT_GE (ns_fd,0 ); + +/* Get handle for the namespace */ + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +ASSERT_GT (handle->handle_bytes,0 ); + +/* Try to open using FD_NSFS_ROOT */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); +if (fd <0 && (errno ==EINVAL || errno ==EOPNOTSUPP )) { + SKIP(free(handle); close(ns_fd); + return, + "open_by_handle_at with FD_NSFS_ROOT not supported"); + } +ASSERT_GE (fd,0 ); + +/* Verify we opened the correct namespace */ +ASSERT_EQ (fstat (ns_fd, &st1),0 ); +ASSERT_EQ (fstat (fd, &st2),0 ); +ASSERT_EQ (st1.st_ino, st2.st_ino); +ASSERT_EQ (st1.st_dev, st2.st_dev); + +close (fd); +close (ns_fd); +free (handle); +} + +TEST (nsfs_cgroup_handle) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +char ns_path[256 ]; +struct stat st1, st2; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Open cgroup namespace file descriptor */ +snprintf (ns_path,sizeof (ns_path),"/proc/self/ns/cgroup" ); + ns_fd =open (ns_path,O_RDONLY ); +if (ns_fd <0 ) { + SKIP(free(handle); return, "cgroup namespace not available"); + } + +/* Get handle for the namespace */ + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +ASSERT_GT (handle->handle_bytes,0 ); + +/* Try to open using FD_NSFS_ROOT */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); +if (fd <0 && (errno ==EINVAL || errno ==EOPNOTSUPP )) { + SKIP(free(handle); close(ns_fd); + return, + "open_by_handle_at with FD_NSFS_ROOT not supported"); + } +ASSERT_GE (fd,0 ); + +/* Verify we opened the correct namespace */ +ASSERT_EQ (fstat (ns_fd, &st1),0 ); +ASSERT_EQ (fstat (fd, &st2),0 ); +ASSERT_EQ (st1.st_ino, st2.st_ino); +ASSERT_EQ (st1.st_dev, st2.st_dev); + +close (fd); +close (ns_fd); +free (handle); +} + +TEST (nsfs_time_handle) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +char ns_path[256 ]; +struct stat st1, st2; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Open time namespace file descriptor */ +snprintf (ns_path,sizeof (ns_path),"/proc/self/ns/time" ); + ns_fd =open (ns_path,O_RDONLY ); +if (ns_fd <0 ) { + SKIP(free(handle); return, "time namespace not available"); + } + +/* Get handle for the namespace */ + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +ASSERT_GT (handle->handle_bytes,0 ); + +/* Try to open using FD_NSFS_ROOT */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); +if (fd <0 && (errno ==EINVAL || errno ==EOPNOTSUPP )) { + SKIP(free(handle); close(ns_fd); + return, + "open_by_handle_at with FD_NSFS_ROOT not supported"); + } +ASSERT_GE (fd,0 ); + +/* Verify we opened the correct namespace */ +ASSERT_EQ (fstat (ns_fd, &st1),0 ); +ASSERT_EQ (fstat (fd, &st2),0 ); +ASSERT_EQ (st1.st_ino, st2.st_ino); +ASSERT_EQ (st1.st_dev, st2.st_dev); + +close (fd); +close (ns_fd); +free (handle); +} + +TEST (nsfs_user_net_namespace_isolation) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +pid_t pid; +int status; +int pipefd[2 ]; +char result; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Create pipe for communication */ +ASSERT_EQ (pipe (pipefd),0 ); + +/* Get handle for current network namespace */ + ns_fd =open ("/proc/self/ns/net" ,O_RDONLY ); +ASSERT_GE (ns_fd,0 ); + + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); close(pipefd[0]); +close (pipefd[1 ]); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +close (ns_fd); + + pid =fork (); +ASSERT_GE (pid,0 ); + +if (pid ==0 ) { +/* Child process */ +close (pipefd[0 ]); + +/* First create new user namespace to drop privileges */ + ret =unshare (CLONE_NEWUSER ); +if (ret <0 ) { +write (pipefd[1 ],"U" , +1 );/* Unable to create user namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Write uid/gid mappings to maintain some capabilities */ +int uid_map_fd =open ("/proc/self/uid_map" ,O_WRONLY ); +int gid_map_fd =open ("/proc/self/gid_map" ,O_WRONLY ); +int setgroups_fd =open ("/proc/self/setgroups" ,O_WRONLY ); + +if (uid_map_fd <0 || gid_map_fd <0 || setgroups_fd <0 ) { +write (pipefd[1 ],"M" ,1 );/* Unable to set mappings */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Disable setgroups to allow gid mapping */ +write (setgroups_fd,"deny" ,4 ); +close (setgroups_fd); + +/* Map current uid/gid to root in the new namespace */ +char mapping[64 ]; +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getuid ()); +write (uid_map_fd, mapping,strlen (mapping)); +close (uid_map_fd); + +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getgid ()); +write (gid_map_fd, mapping,strlen (mapping)); +close (gid_map_fd); + +/* Now create new network namespace */ + ret =unshare (CLONE_NEWNET ); +if (ret <0 ) { +write (pipefd[1 ],"N" , +1 );/* Unable to create network namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Try to open parent's network namespace handle from new user+net namespace */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); + +if (fd >=0 ) { +/* Should NOT succeed - we're in a different user namespace */ +write (pipefd[1 ],"S" ,1 );/* Unexpected success */ +close (fd); + }else if (errno ==ESTALE ) { +/* Expected: Stale file handle */ +write (pipefd[1 ],"P" ,1 ); + }else { +/* Other error */ +write (pipefd[1 ],"F" ,1 ); + } + +close (pipefd[1 ]); +exit (0 ); + } + +/* Parent process */ +close (pipefd[1 ]); +ASSERT_EQ (read (pipefd[0 ], &result,1 ),1 ); + +waitpid (pid, &status,0 ); +ASSERT_TRUE (WIFEXITED (status)); +ASSERT_EQ (WEXITSTATUS (status),0 ); + +if (result =='U' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new user namespace"); + } +if (result =='M' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot set uid/gid mappings"); + } +if (result =='N' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new network namespace"); + } + +/* Should fail with permission denied since we're in a different user namespace */ +ASSERT_EQ (result,'P' ); + +close (pipefd[0 ]); +free (handle); +} + +TEST (nsfs_user_uts_namespace_isolation) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +pid_t pid; +int status; +int pipefd[2 ]; +char result; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Create pipe for communication */ +ASSERT_EQ (pipe (pipefd),0 ); + +/* Get handle for current UTS namespace */ + ns_fd =open ("/proc/self/ns/uts" ,O_RDONLY ); +ASSERT_GE (ns_fd,0 ); + + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); close(pipefd[0]); +close (pipefd[1 ]); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +close (ns_fd); + + pid =fork (); +ASSERT_GE (pid,0 ); + +if (pid ==0 ) { +/* Child process */ +close (pipefd[0 ]); + +/* First create new user namespace to drop privileges */ + ret =unshare (CLONE_NEWUSER ); +if (ret <0 ) { +write (pipefd[1 ],"U" , +1 );/* Unable to create user namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Write uid/gid mappings to maintain some capabilities */ +int uid_map_fd =open ("/proc/self/uid_map" ,O_WRONLY ); +int gid_map_fd =open ("/proc/self/gid_map" ,O_WRONLY ); +int setgroups_fd =open ("/proc/self/setgroups" ,O_WRONLY ); + +if (uid_map_fd <0 || gid_map_fd <0 || setgroups_fd <0 ) { +write (pipefd[1 ],"M" ,1 );/* Unable to set mappings */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Disable setgroups to allow gid mapping */ +write (setgroups_fd,"deny" ,4 ); +close (setgroups_fd); + +/* Map current uid/gid to root in the new namespace */ +char mapping[64 ]; +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getuid ()); +write (uid_map_fd, mapping,strlen (mapping)); +close (uid_map_fd); + +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getgid ()); +write (gid_map_fd, mapping,strlen (mapping)); +close (gid_map_fd); + +/* Now create new UTS namespace */ + ret =unshare (CLONE_NEWUTS ); +if (ret <0 ) { +write (pipefd[1 ],"N" , +1 );/* Unable to create UTS namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Try to open parent's UTS namespace handle from new user+uts namespace */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); + +if (fd >=0 ) { +/* Should NOT succeed - we're in a different user namespace */ +write (pipefd[1 ],"S" ,1 );/* Unexpected success */ +close (fd); + }else if (errno ==ESTALE ) { +/* Expected: Stale file handle */ +write (pipefd[1 ],"P" ,1 ); + }else { +/* Other error */ +write (pipefd[1 ],"F" ,1 ); + } + +close (pipefd[1 ]); +exit (0 ); + } + +/* Parent process */ +close (pipefd[1 ]); +ASSERT_EQ (read (pipefd[0 ], &result,1 ),1 ); + +waitpid (pid, &status,0 ); +ASSERT_TRUE (WIFEXITED (status)); +ASSERT_EQ (WEXITSTATUS (status),0 ); + +if (result =='U' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new user namespace"); + } +if (result =='M' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot set uid/gid mappings"); + } +if (result =='N' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new UTS namespace"); + } + +/* Should fail with ESTALE since we're in a different user namespace */ +ASSERT_EQ (result,'P' ); + +close (pipefd[0 ]); +free (handle); +} + +TEST (nsfs_user_ipc_namespace_isolation) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +pid_t pid; +int status; +int pipefd[2 ]; +char result; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Create pipe for communication */ +ASSERT_EQ (pipe (pipefd),0 ); + +/* Get handle for current IPC namespace */ + ns_fd =open ("/proc/self/ns/ipc" ,O_RDONLY ); +ASSERT_GE (ns_fd,0 ); + + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); close(pipefd[0]); +close (pipefd[1 ]); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +close (ns_fd); + + pid =fork (); +ASSERT_GE (pid,0 ); + +if (pid ==0 ) { +/* Child process */ +close (pipefd[0 ]); + +/* First create new user namespace to drop privileges */ + ret =unshare (CLONE_NEWUSER ); +if (ret <0 ) { +write (pipefd[1 ],"U" , +1 );/* Unable to create user namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Write uid/gid mappings to maintain some capabilities */ +int uid_map_fd =open ("/proc/self/uid_map" ,O_WRONLY ); +int gid_map_fd =open ("/proc/self/gid_map" ,O_WRONLY ); +int setgroups_fd =open ("/proc/self/setgroups" ,O_WRONLY ); + +if (uid_map_fd <0 || gid_map_fd <0 || setgroups_fd <0 ) { +write (pipefd[1 ],"M" ,1 );/* Unable to set mappings */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Disable setgroups to allow gid mapping */ +write (setgroups_fd,"deny" ,4 ); +close (setgroups_fd); + +/* Map current uid/gid to root in the new namespace */ +char mapping[64 ]; +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getuid ()); +write (uid_map_fd, mapping,strlen (mapping)); +close (uid_map_fd); + +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getgid ()); +write (gid_map_fd, mapping,strlen (mapping)); +close (gid_map_fd); + +/* Now create new IPC namespace */ + ret =unshare (CLONE_NEWIPC ); +if (ret <0 ) { +write (pipefd[1 ],"N" , +1 );/* Unable to create IPC namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Try to open parent's IPC namespace handle from new user+ipc namespace */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); + +if (fd >=0 ) { +/* Should NOT succeed - we're in a different user namespace */ +write (pipefd[1 ],"S" ,1 );/* Unexpected success */ +close (fd); + }else if (errno ==ESTALE ) { +/* Expected: Stale file handle */ +write (pipefd[1 ],"P" ,1 ); + }else { +/* Other error */ +write (pipefd[1 ],"F" ,1 ); + } + +close (pipefd[1 ]); +exit (0 ); + } + +/* Parent process */ +close (pipefd[1 ]); +ASSERT_EQ (read (pipefd[0 ], &result,1 ),1 ); + +waitpid (pid, &status,0 ); +ASSERT_TRUE (WIFEXITED (status)); +ASSERT_EQ (WEXITSTATUS (status),0 ); + +if (result =='U' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new user namespace"); + } +if (result =='M' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot set uid/gid mappings"); + } +if (result =='N' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new IPC namespace"); + } + +/* Should fail with ESTALE since we're in a different user namespace */ +ASSERT_EQ (result,'P' ); + +close (pipefd[0 ]); +free (handle); +} + +TEST (nsfs_user_mnt_namespace_isolation) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +pid_t pid; +int status; +int pipefd[2 ]; +char result; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Create pipe for communication */ +ASSERT_EQ (pipe (pipefd),0 ); + +/* Get handle for current mount namespace */ + ns_fd =open ("/proc/self/ns/mnt" ,O_RDONLY ); +ASSERT_GE (ns_fd,0 ); + + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); close(pipefd[0]); +close (pipefd[1 ]); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +close (ns_fd); + + pid =fork (); +ASSERT_GE (pid,0 ); + +if (pid ==0 ) { +/* Child process */ +close (pipefd[0 ]); + +/* First create new user namespace to drop privileges */ + ret =unshare (CLONE_NEWUSER ); +if (ret <0 ) { +write (pipefd[1 ],"U" , +1 );/* Unable to create user namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Write uid/gid mappings to maintain some capabilities */ +int uid_map_fd =open ("/proc/self/uid_map" ,O_WRONLY ); +int gid_map_fd =open ("/proc/self/gid_map" ,O_WRONLY ); +int setgroups_fd =open ("/proc/self/setgroups" ,O_WRONLY ); + +if (uid_map_fd <0 || gid_map_fd <0 || setgroups_fd <0 ) { +write (pipefd[1 ],"M" ,1 );/* Unable to set mappings */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Disable setgroups to allow gid mapping */ +write (setgroups_fd,"deny" ,4 ); +close (setgroups_fd); + +/* Map current uid/gid to root in the new namespace */ +char mapping[64 ]; +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getuid ()); +write (uid_map_fd, mapping,strlen (mapping)); +close (uid_map_fd); + +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getgid ()); +write (gid_map_fd, mapping,strlen (mapping)); +close (gid_map_fd); + +/* Now create new mount namespace */ + ret =unshare (CLONE_NEWNS ); +if (ret <0 ) { +write (pipefd[1 ],"N" , +1 );/* Unable to create mount namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Try to open parent's mount namespace handle from new user+mnt namespace */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); + +if (fd >=0 ) { +/* Should NOT succeed - we're in a different user namespace */ +write (pipefd[1 ],"S" ,1 );/* Unexpected success */ +close (fd); + }else if (errno ==ESTALE ) { +/* Expected: Stale file handle */ +write (pipefd[1 ],"P" ,1 ); + }else { +/* Other error */ +write (pipefd[1 ],"F" ,1 ); + } + +close (pipefd[1 ]); +exit (0 ); + } + +/* Parent process */ +close (pipefd[1 ]); +ASSERT_EQ (read (pipefd[0 ], &result,1 ),1 ); + +waitpid (pid, &status,0 ); +ASSERT_TRUE (WIFEXITED (status)); +ASSERT_EQ (WEXITSTATUS (status),0 ); + +if (result =='U' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new user namespace"); + } +if (result =='M' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot set uid/gid mappings"); + } +if (result =='N' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new mount namespace"); + } + +/* Should fail with ESTALE since we're in a different user namespace */ +ASSERT_EQ (result,'P' ); + +close (pipefd[0 ]); +free (handle); +} + +TEST (nsfs_user_cgroup_namespace_isolation) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +pid_t pid; +int status; +int pipefd[2 ]; +char result; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Create pipe for communication */ +ASSERT_EQ (pipe (pipefd),0 ); + +/* Get handle for current cgroup namespace */ + ns_fd =open ("/proc/self/ns/cgroup" ,O_RDONLY ); +if (ns_fd <0 ) { + SKIP(free(handle); close(pipefd[0]); close(pipefd[1]); + return, "cgroup namespace not available"); + } + + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); close(pipefd[0]); +close (pipefd[1 ]); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +close (ns_fd); + + pid =fork (); +ASSERT_GE (pid,0 ); + +if (pid ==0 ) { +/* Child process */ +close (pipefd[0 ]); + +/* First create new user namespace to drop privileges */ + ret =unshare (CLONE_NEWUSER ); +if (ret <0 ) { +write (pipefd[1 ],"U" , +1 );/* Unable to create user namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Write uid/gid mappings to maintain some capabilities */ +int uid_map_fd =open ("/proc/self/uid_map" ,O_WRONLY ); +int gid_map_fd =open ("/proc/self/gid_map" ,O_WRONLY ); +int setgroups_fd =open ("/proc/self/setgroups" ,O_WRONLY ); + +if (uid_map_fd <0 || gid_map_fd <0 || setgroups_fd <0 ) { +write (pipefd[1 ],"M" ,1 );/* Unable to set mappings */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Disable setgroups to allow gid mapping */ +write (setgroups_fd,"deny" ,4 ); +close (setgroups_fd); + +/* Map current uid/gid to root in the new namespace */ +char mapping[64 ]; +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getuid ()); +write (uid_map_fd, mapping,strlen (mapping)); +close (uid_map_fd); + +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getgid ()); +write (gid_map_fd, mapping,strlen (mapping)); +close (gid_map_fd); + +/* Now create new cgroup namespace */ + ret =unshare (CLONE_NEWCGROUP ); +if (ret <0 ) { +write (pipefd[1 ],"N" , +1 );/* Unable to create cgroup namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Try to open parent's cgroup namespace handle from new user+cgroup namespace */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); + +if (fd >=0 ) { +/* Should NOT succeed - we're in a different user namespace */ +write (pipefd[1 ],"S" ,1 );/* Unexpected success */ +close (fd); + }else if (errno ==ESTALE ) { +/* Expected: Stale file handle */ +write (pipefd[1 ],"P" ,1 ); + }else { +/* Other error */ +write (pipefd[1 ],"F" ,1 ); + } + +close (pipefd[1 ]); +exit (0 ); + } + +/* Parent process */ +close (pipefd[1 ]); +ASSERT_EQ (read (pipefd[0 ], &result,1 ),1 ); + +waitpid (pid, &status,0 ); +ASSERT_TRUE (WIFEXITED (status)); +ASSERT_EQ (WEXITSTATUS (status),0 ); + +if (result =='U' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new user namespace"); + } +if (result =='M' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot set uid/gid mappings"); + } +if (result =='N' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new cgroup namespace"); + } + +/* Should fail with ESTALE since we're in a different user namespace */ +ASSERT_EQ (result,'P' ); + +close (pipefd[0 ]); +free (handle); +} + +TEST (nsfs_user_pid_namespace_isolation) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +pid_t pid; +int status; +int pipefd[2 ]; +char result; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Create pipe for communication */ +ASSERT_EQ (pipe (pipefd),0 ); + +/* Get handle for current PID namespace */ + ns_fd =open ("/proc/self/ns/pid" ,O_RDONLY ); +ASSERT_GE (ns_fd,0 ); + + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); close(pipefd[0]); +close (pipefd[1 ]); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +close (ns_fd); + + pid =fork (); +ASSERT_GE (pid,0 ); + +if (pid ==0 ) { +/* Child process */ +close (pipefd[0 ]); + +/* First create new user namespace to drop privileges */ + ret =unshare (CLONE_NEWUSER ); +if (ret <0 ) { +write (pipefd[1 ],"U" , +1 );/* Unable to create user namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Write uid/gid mappings to maintain some capabilities */ +int uid_map_fd =open ("/proc/self/uid_map" ,O_WRONLY ); +int gid_map_fd =open ("/proc/self/gid_map" ,O_WRONLY ); +int setgroups_fd =open ("/proc/self/setgroups" ,O_WRONLY ); + +if (uid_map_fd <0 || gid_map_fd <0 || setgroups_fd <0 ) { +write (pipefd[1 ],"M" ,1 );/* Unable to set mappings */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Disable setgroups to allow gid mapping */ +write (setgroups_fd,"deny" ,4 ); +close (setgroups_fd); + +/* Map current uid/gid to root in the new namespace */ +char mapping[64 ]; +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getuid ()); +write (uid_map_fd, mapping,strlen (mapping)); +close (uid_map_fd); + +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getgid ()); +write (gid_map_fd, mapping,strlen (mapping)); +close (gid_map_fd); + +/* Now create new PID namespace - requires fork to take effect */ + ret =unshare (CLONE_NEWPID ); +if (ret <0 ) { +write (pipefd[1 ],"N" , +1 );/* Unable to create PID namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Fork again for PID namespace to take effect */ +pid_t child_pid =fork (); +if (child_pid <0 ) { +write (pipefd[1 ],"N" , +1 );/* Unable to fork in PID namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +if (child_pid ==0 ) { +/* Grandchild in new PID namespace */ +/* Try to open parent's PID namespace handle from new user+pid namespace */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); + +if (fd >=0 ) { +/* Should NOT succeed - we're in a different user namespace */ +write (pipefd[1 ],"S" , +1 );/* Unexpected success */ +close (fd); + }else if (errno ==ESTALE ) { +/* Expected: Stale file handle */ +write (pipefd[1 ],"P" ,1 ); + }else { +/* Other error */ +write (pipefd[1 ],"F" ,1 ); + } + +close (pipefd[1 ]); +exit (0 ); + } + +/* Wait for grandchild */ +waitpid (child_pid,NULL ,0 ); +exit (0 ); + } + +/* Parent process */ +close (pipefd[1 ]); +ASSERT_EQ (read (pipefd[0 ], &result,1 ),1 ); + +waitpid (pid, &status,0 ); +ASSERT_TRUE (WIFEXITED (status)); +ASSERT_EQ (WEXITSTATUS (status),0 ); + +if (result =='U' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new user namespace"); + } +if (result =='M' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot set uid/gid mappings"); + } +if (result =='N' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new PID namespace"); + } + +/* Should fail with ESTALE since we're in a different user namespace */ +ASSERT_EQ (result,'P' ); + +close (pipefd[0 ]); +free (handle); +} + +TEST (nsfs_user_time_namespace_isolation) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +pid_t pid; +int status; +int pipefd[2 ]; +char result; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Create pipe for communication */ +ASSERT_EQ (pipe (pipefd),0 ); + +/* Get handle for current time namespace */ + ns_fd =open ("/proc/self/ns/time" ,O_RDONLY ); +if (ns_fd <0 ) { + SKIP(free(handle); close(pipefd[0]); close(pipefd[1]); + return, "time namespace not available"); + } + + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); close(pipefd[0]); +close (pipefd[1 ]); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +close (ns_fd); + + pid =fork (); +ASSERT_GE (pid,0 ); + +if (pid ==0 ) { +/* Child process */ +close (pipefd[0 ]); + +/* First create new user namespace to drop privileges */ + ret =unshare (CLONE_NEWUSER ); +if (ret <0 ) { +write (pipefd[1 ],"U" , +1 );/* Unable to create user namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Write uid/gid mappings to maintain some capabilities */ +int uid_map_fd =open ("/proc/self/uid_map" ,O_WRONLY ); +int gid_map_fd =open ("/proc/self/gid_map" ,O_WRONLY ); +int setgroups_fd =open ("/proc/self/setgroups" ,O_WRONLY ); + +if (uid_map_fd <0 || gid_map_fd <0 || setgroups_fd <0 ) { +write (pipefd[1 ],"M" ,1 );/* Unable to set mappings */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Disable setgroups to allow gid mapping */ +write (setgroups_fd,"deny" ,4 ); +close (setgroups_fd); + +/* Map current uid/gid to root in the new namespace */ +char mapping[64 ]; +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getuid ()); +write (uid_map_fd, mapping,strlen (mapping)); +close (uid_map_fd); + +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getgid ()); +write (gid_map_fd, mapping,strlen (mapping)); +close (gid_map_fd); + +/* Now create new time namespace - requires fork to take effect */ + ret =unshare (CLONE_NEWTIME ); +if (ret <0 ) { +write (pipefd[1 ],"N" , +1 );/* Unable to create time namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Fork again for time namespace to take effect */ +pid_t child_pid =fork (); +if (child_pid <0 ) { +write (pipefd[1 ],"N" , +1 );/* Unable to fork in time namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +if (child_pid ==0 ) { +/* Grandchild in new time namespace */ +/* Try to open parent's time namespace handle from new user+time namespace */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); + +if (fd >=0 ) { +/* Should NOT succeed - we're in a different user namespace */ +write (pipefd[1 ],"S" , +1 );/* Unexpected success */ +close (fd); + }else if (errno ==ESTALE ) { +/* Expected: Stale file handle */ +write (pipefd[1 ],"P" ,1 ); + }else { +/* Other error */ +write (pipefd[1 ],"F" ,1 ); + } + +close (pipefd[1 ]); +exit (0 ); + } + +/* Wait for grandchild */ +waitpid (child_pid,NULL ,0 ); +exit (0 ); + } + +/* Parent process */ +close (pipefd[1 ]); +ASSERT_EQ (read (pipefd[0 ], &result,1 ),1 ); + +waitpid (pid, &status,0 ); +ASSERT_TRUE (WIFEXITED (status)); +ASSERT_EQ (WEXITSTATUS (status),0 ); + +if (result =='U' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new user namespace"); + } +if (result =='M' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot set uid/gid mappings"); + } +if (result =='N' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new time namespace"); + } + +/* Should fail with ESTALE since we're in a different user namespace */ +ASSERT_EQ (result,'P' ); + +close (pipefd[0 ]); +free (handle); +} + +TEST (nsfs_open_flags) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +char ns_path[256 ]; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Open a namespace file descriptor */ +snprintf (ns_path,sizeof (ns_path),"/proc/self/ns/net" ); + ns_fd =open (ns_path,O_RDONLY ); +ASSERT_GE (ns_fd,0 ); + +/* Get handle for the namespace */ + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +ASSERT_GT (handle->handle_bytes,0 ); + +/* Test invalid flags that should fail */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_WRONLY ); +ASSERT_LT (fd,0 ); +ASSERT_EQ (errno,EPERM ); + + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDWR ); +ASSERT_LT (fd,0 ); +ASSERT_EQ (errno,EPERM ); + + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_CREAT ); +ASSERT_LT (fd,0 ); +ASSERT_EQ (errno,EINVAL ); + + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_TRUNC ); +ASSERT_LT (fd,0 ); +ASSERT_EQ (errno,EINVAL ); + + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_APPEND ); +ASSERT_LT (fd,0 ); +ASSERT_EQ (errno,EINVAL ); + + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_DIRECT ); +ASSERT_LT (fd,0 ); +ASSERT_EQ (errno,EINVAL ); + +close (ns_fd); +free (handle); +} + +TEST_HARNESS_MAIN --2.47.3
Christian Brauner
maintainerRe: [PATCH 32/32] selftests/namespaces: add file handle selftests
Quoted 6 lines · Amir Goldstein
Quoted 4 lines · Christian Brauner
Add a bunch of selftests for namespace file handles.
Signed-off-by Christian Brauner <[email protected]>
Quoted 7 lines
Obviously, I did not go over every single line, but for the general test template and test coverage you may add:
Reviewed-by Amir Goldstein <[email protected]>However, see my comment on file handle support patch. The test matrix is incomplete.
I mean, I'll just drop to non-root in the non-cross ns tests:
/* Drop to unprivileged uid/gid */ ASSERT_EQ(setresgid(65534, 65534, 65534), 0); /* nogroup */ ASSERT_EQ(setresuid(65534, 65534, 65534), 0); /* nobody */
Quoted 4 lines
Maybe it would be complete if test is run as root and then as non root, but then I think the test needs some changes for running as root and opening non-self ns.
Quoted 8 lines
I am not sure what the standard is wrt running the selftests as root /non-root.
I see that the userns isolation tests do:
/* Map current uid/gid to root in the new namespace */Are you assuming that non root is running this test or am I missing something?
No, I'm not assuming that. I just need a new user namespace and become root in it to assume privilege over it so I can test that decoding doesn't work from an ancestor userns owned namespace.
Quoted 1,454 lines
Wouldn't mapping uid 0 to uid 0 in the new userns cause the test to fail because opening by handle will succeed?
Thanks,
Amir.Quoted 1,446 lines
--- tools/testing/selftests/namespaces/.gitignore | 1 + tools/testing/selftests/namespaces/Makefile | 2 +- .../selftests/namespaces/file_handle_test.c | 1410 ++++++++++++++++++++ 3 files changed, 1412 insertions(+), 1 deletion(-)tools/testing/selftests/namespaces/.gitignore +1
-0@@ -1 +1,2 @@ nsid_test +file_handle_testtools/testing/selftests/namespaces/Makefile +1
-1@@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only CFLAGS += -Wall -O0 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES) -TEST_GEN_PROGS := nsid_test +TEST_GEN_PROGS := nsid_test file_handle_test include ../lib.mktools/testing/selftests/namespaces/file_handle_test.c new file +1410
-1@@ -0,0 +1,1410 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include <errno.h> +#include <fcntl.h> +#include <grp.h> +#include <limits.h> +#include <sched.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/mount.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> +#include <linux/unistd.h> +#include "../kselftest_harness.h" + +#ifndef FD_NSFS_ROOT +#define FD_NSFS_ROOT -10003/* Root of the nsfs filesystem */ +#endif + +TEST (nsfs_net_handle) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +char ns_path[256 ]; +struct stat st1, st2; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Open a namespace file descriptor */ +snprintf (ns_path,sizeof (ns_path),"/proc/self/ns/net" ); + ns_fd =open (ns_path,O_RDONLY ); +ASSERT_GE (ns_fd,0 ); + +/* Get handle for the namespace */ + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +ASSERT_GT (handle->handle_bytes,0 ); + +/* Try to open using FD_NSFS_ROOT */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); +if (fd <0 && (errno ==EINVAL || errno ==EOPNOTSUPP )) { + SKIP(free(handle); close(ns_fd); + return, + "open_by_handle_at with FD_NSFS_ROOT not supported"); + } +ASSERT_GE (fd,0 ); + +/* Verify we opened the correct namespace */ +ASSERT_EQ (fstat (ns_fd, &st1),0 ); +ASSERT_EQ (fstat (fd, &st2),0 ); +ASSERT_EQ (st1.st_ino, st2.st_ino); +ASSERT_EQ (st1.st_dev, st2.st_dev); + +close (fd); +close (ns_fd); +free (handle); +} + +TEST (nsfs_uts_handle) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +char ns_path[256 ]; +struct stat st1, st2; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Open UTS namespace file descriptor */ +snprintf (ns_path,sizeof (ns_path),"/proc/self/ns/uts" ); + ns_fd =open (ns_path,O_RDONLY ); +ASSERT_GE (ns_fd,0 ); + +/* Get handle for the namespace */ + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +ASSERT_GT (handle->handle_bytes,0 ); + +/* Try to open using FD_NSFS_ROOT */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); +if (fd <0 && (errno ==EINVAL || errno ==EOPNOTSUPP )) { + SKIP(free(handle); close(ns_fd); + return, + "open_by_handle_at with FD_NSFS_ROOT not supported"); + } +ASSERT_GE (fd,0 ); + +/* Verify we opened the correct namespace */ +ASSERT_EQ (fstat (ns_fd, &st1),0 ); +ASSERT_EQ (fstat (fd, &st2),0 ); +ASSERT_EQ (st1.st_ino, st2.st_ino); +ASSERT_EQ (st1.st_dev, st2.st_dev); + +close (fd); +close (ns_fd); +free (handle); +} + +TEST (nsfs_ipc_handle) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +char ns_path[256 ]; +struct stat st1, st2; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Open IPC namespace file descriptor */ +snprintf (ns_path,sizeof (ns_path),"/proc/self/ns/ipc" ); + ns_fd =open (ns_path,O_RDONLY ); +ASSERT_GE (ns_fd,0 ); + +/* Get handle for the namespace */ + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +ASSERT_GT (handle->handle_bytes,0 ); + +/* Try to open using FD_NSFS_ROOT */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); +if (fd <0 && (errno ==EINVAL || errno ==EOPNOTSUPP )) { + SKIP(free(handle); close(ns_fd); + return, + "open_by_handle_at with FD_NSFS_ROOT not supported"); + } +ASSERT_GE (fd,0 ); + +/* Verify we opened the correct namespace */ +ASSERT_EQ (fstat (ns_fd, &st1),0 ); +ASSERT_EQ (fstat (fd, &st2),0 ); +ASSERT_EQ (st1.st_ino, st2.st_ino); +ASSERT_EQ (st1.st_dev, st2.st_dev); + +close (fd); +close (ns_fd); +free (handle); +} + +TEST (nsfs_pid_handle) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +char ns_path[256 ]; +struct stat st1, st2; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Open PID namespace file descriptor */ +snprintf (ns_path,sizeof (ns_path),"/proc/self/ns/pid" ); + ns_fd =open (ns_path,O_RDONLY ); +ASSERT_GE (ns_fd,0 ); + +/* Get handle for the namespace */ + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +ASSERT_GT (handle->handle_bytes,0 ); + +/* Try to open using FD_NSFS_ROOT */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); +if (fd <0 && (errno ==EINVAL || errno ==EOPNOTSUPP )) { + SKIP(free(handle); close(ns_fd); + return, + "open_by_handle_at with FD_NSFS_ROOT not supported"); + } +ASSERT_GE (fd,0 ); + +/* Verify we opened the correct namespace */ +ASSERT_EQ (fstat (ns_fd, &st1),0 ); +ASSERT_EQ (fstat (fd, &st2),0 ); +ASSERT_EQ (st1.st_ino, st2.st_ino); +ASSERT_EQ (st1.st_dev, st2.st_dev); + +close (fd); +close (ns_fd); +free (handle); +} + +TEST (nsfs_mnt_handle) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +char ns_path[256 ]; +struct stat st1, st2; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Open mount namespace file descriptor */ +snprintf (ns_path,sizeof (ns_path),"/proc/self/ns/mnt" ); + ns_fd =open (ns_path,O_RDONLY ); +ASSERT_GE (ns_fd,0 ); + +/* Get handle for the namespace */ + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +ASSERT_GT (handle->handle_bytes,0 ); + +/* Try to open using FD_NSFS_ROOT */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); +if (fd <0 && (errno ==EINVAL || errno ==EOPNOTSUPP )) { + SKIP(free(handle); close(ns_fd); + return, + "open_by_handle_at with FD_NSFS_ROOT not supported"); + } +ASSERT_GE (fd,0 ); + +/* Verify we opened the correct namespace */ +ASSERT_EQ (fstat (ns_fd, &st1),0 ); +ASSERT_EQ (fstat (fd, &st2),0 ); +ASSERT_EQ (st1.st_ino, st2.st_ino); +ASSERT_EQ (st1.st_dev, st2.st_dev); + +close (fd); +close (ns_fd); +free (handle); +} + +TEST (nsfs_user_handle) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +char ns_path[256 ]; +struct stat st1, st2; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Open user namespace file descriptor */ +snprintf (ns_path,sizeof (ns_path),"/proc/self/ns/user" ); + ns_fd =open (ns_path,O_RDONLY ); +ASSERT_GE (ns_fd,0 ); + +/* Get handle for the namespace */ + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +ASSERT_GT (handle->handle_bytes,0 ); + +/* Try to open using FD_NSFS_ROOT */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); +if (fd <0 && (errno ==EINVAL || errno ==EOPNOTSUPP )) { + SKIP(free(handle); close(ns_fd); + return, + "open_by_handle_at with FD_NSFS_ROOT not supported"); + } +ASSERT_GE (fd,0 ); + +/* Verify we opened the correct namespace */ +ASSERT_EQ (fstat (ns_fd, &st1),0 ); +ASSERT_EQ (fstat (fd, &st2),0 ); +ASSERT_EQ (st1.st_ino, st2.st_ino); +ASSERT_EQ (st1.st_dev, st2.st_dev); + +close (fd); +close (ns_fd); +free (handle); +} + +TEST (nsfs_cgroup_handle) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +char ns_path[256 ]; +struct stat st1, st2; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Open cgroup namespace file descriptor */ +snprintf (ns_path,sizeof (ns_path),"/proc/self/ns/cgroup" ); + ns_fd =open (ns_path,O_RDONLY ); +if (ns_fd <0 ) { + SKIP(free(handle); return, "cgroup namespace not available"); + } + +/* Get handle for the namespace */ + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +ASSERT_GT (handle->handle_bytes,0 ); + +/* Try to open using FD_NSFS_ROOT */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); +if (fd <0 && (errno ==EINVAL || errno ==EOPNOTSUPP )) { + SKIP(free(handle); close(ns_fd); + return, + "open_by_handle_at with FD_NSFS_ROOT not supported"); + } +ASSERT_GE (fd,0 ); + +/* Verify we opened the correct namespace */ +ASSERT_EQ (fstat (ns_fd, &st1),0 ); +ASSERT_EQ (fstat (fd, &st2),0 ); +ASSERT_EQ (st1.st_ino, st2.st_ino); +ASSERT_EQ (st1.st_dev, st2.st_dev); + +close (fd); +close (ns_fd); +free (handle); +} + +TEST (nsfs_time_handle) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +char ns_path[256 ]; +struct stat st1, st2; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Open time namespace file descriptor */ +snprintf (ns_path,sizeof (ns_path),"/proc/self/ns/time" ); + ns_fd =open (ns_path,O_RDONLY ); +if (ns_fd <0 ) { + SKIP(free(handle); return, "time namespace not available"); + } + +/* Get handle for the namespace */ + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +ASSERT_GT (handle->handle_bytes,0 ); + +/* Try to open using FD_NSFS_ROOT */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); +if (fd <0 && (errno ==EINVAL || errno ==EOPNOTSUPP )) { + SKIP(free(handle); close(ns_fd); + return, + "open_by_handle_at with FD_NSFS_ROOT not supported"); + } +ASSERT_GE (fd,0 ); + +/* Verify we opened the correct namespace */ +ASSERT_EQ (fstat (ns_fd, &st1),0 ); +ASSERT_EQ (fstat (fd, &st2),0 ); +ASSERT_EQ (st1.st_ino, st2.st_ino); +ASSERT_EQ (st1.st_dev, st2.st_dev); + +close (fd); +close (ns_fd); +free (handle); +} + +TEST (nsfs_user_net_namespace_isolation) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +pid_t pid; +int status; +int pipefd[2 ]; +char result; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Create pipe for communication */ +ASSERT_EQ (pipe (pipefd),0 ); + +/* Get handle for current network namespace */ + ns_fd =open ("/proc/self/ns/net" ,O_RDONLY ); +ASSERT_GE (ns_fd,0 ); + + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); close(pipefd[0]); +close (pipefd[1 ]); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +close (ns_fd); + + pid =fork (); +ASSERT_GE (pid,0 ); + +if (pid ==0 ) { +/* Child process */ +close (pipefd[0 ]); + +/* First create new user namespace to drop privileges */ + ret =unshare (CLONE_NEWUSER ); +if (ret <0 ) { +write (pipefd[1 ],"U" , +1 );/* Unable to create user namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Write uid/gid mappings to maintain some capabilities */ +int uid_map_fd =open ("/proc/self/uid_map" ,O_WRONLY ); +int gid_map_fd =open ("/proc/self/gid_map" ,O_WRONLY ); +int setgroups_fd =open ("/proc/self/setgroups" ,O_WRONLY ); + +if (uid_map_fd <0 || gid_map_fd <0 || setgroups_fd <0 ) { +write (pipefd[1 ],"M" ,1 );/* Unable to set mappings */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Disable setgroups to allow gid mapping */ +write (setgroups_fd,"deny" ,4 ); +close (setgroups_fd); + +/* Map current uid/gid to root in the new namespace */ +char mapping[64 ]; +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getuid ()); +write (uid_map_fd, mapping,strlen (mapping)); +close (uid_map_fd); + +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getgid ()); +write (gid_map_fd, mapping,strlen (mapping)); +close (gid_map_fd); + +/* Now create new network namespace */ + ret =unshare (CLONE_NEWNET ); +if (ret <0 ) { +write (pipefd[1 ],"N" , +1 );/* Unable to create network namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Try to open parent's network namespace handle from new user+net namespace */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); + +if (fd >=0 ) { +/* Should NOT succeed - we're in a different user namespace */ +write (pipefd[1 ],"S" ,1 );/* Unexpected success */ +close (fd); + }else if (errno ==ESTALE ) { +/* Expected: Stale file handle */ +write (pipefd[1 ],"P" ,1 ); + }else { +/* Other error */ +write (pipefd[1 ],"F" ,1 ); + } + +close (pipefd[1 ]); +exit (0 ); + } + +/* Parent process */ +close (pipefd[1 ]); +ASSERT_EQ (read (pipefd[0 ], &result,1 ),1 ); + +waitpid (pid, &status,0 ); +ASSERT_TRUE (WIFEXITED (status)); +ASSERT_EQ (WEXITSTATUS (status),0 ); + +if (result =='U' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new user namespace"); + } +if (result =='M' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot set uid/gid mappings"); + } +if (result =='N' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new network namespace"); + } + +/* Should fail with permission denied since we're in a different user namespace */ +ASSERT_EQ (result,'P' ); + +close (pipefd[0 ]); +free (handle); +} + +TEST (nsfs_user_uts_namespace_isolation) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +pid_t pid; +int status; +int pipefd[2 ]; +char result; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Create pipe for communication */ +ASSERT_EQ (pipe (pipefd),0 ); + +/* Get handle for current UTS namespace */ + ns_fd =open ("/proc/self/ns/uts" ,O_RDONLY ); +ASSERT_GE (ns_fd,0 ); + + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); close(pipefd[0]); +close (pipefd[1 ]); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +close (ns_fd); + + pid =fork (); +ASSERT_GE (pid,0 ); + +if (pid ==0 ) { +/* Child process */ +close (pipefd[0 ]); + +/* First create new user namespace to drop privileges */ + ret =unshare (CLONE_NEWUSER ); +if (ret <0 ) { +write (pipefd[1 ],"U" , +1 );/* Unable to create user namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Write uid/gid mappings to maintain some capabilities */ +int uid_map_fd =open ("/proc/self/uid_map" ,O_WRONLY ); +int gid_map_fd =open ("/proc/self/gid_map" ,O_WRONLY ); +int setgroups_fd =open ("/proc/self/setgroups" ,O_WRONLY ); + +if (uid_map_fd <0 || gid_map_fd <0 || setgroups_fd <0 ) { +write (pipefd[1 ],"M" ,1 );/* Unable to set mappings */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Disable setgroups to allow gid mapping */ +write (setgroups_fd,"deny" ,4 ); +close (setgroups_fd); + +/* Map current uid/gid to root in the new namespace */ +char mapping[64 ]; +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getuid ()); +write (uid_map_fd, mapping,strlen (mapping)); +close (uid_map_fd); + +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getgid ()); +write (gid_map_fd, mapping,strlen (mapping)); +close (gid_map_fd); + +/* Now create new UTS namespace */ + ret =unshare (CLONE_NEWUTS ); +if (ret <0 ) { +write (pipefd[1 ],"N" , +1 );/* Unable to create UTS namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Try to open parent's UTS namespace handle from new user+uts namespace */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); + +if (fd >=0 ) { +/* Should NOT succeed - we're in a different user namespace */ +write (pipefd[1 ],"S" ,1 );/* Unexpected success */ +close (fd); + }else if (errno ==ESTALE ) { +/* Expected: Stale file handle */ +write (pipefd[1 ],"P" ,1 ); + }else { +/* Other error */ +write (pipefd[1 ],"F" ,1 ); + } + +close (pipefd[1 ]); +exit (0 ); + } + +/* Parent process */ +close (pipefd[1 ]); +ASSERT_EQ (read (pipefd[0 ], &result,1 ),1 ); + +waitpid (pid, &status,0 ); +ASSERT_TRUE (WIFEXITED (status)); +ASSERT_EQ (WEXITSTATUS (status),0 ); + +if (result =='U' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new user namespace"); + } +if (result =='M' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot set uid/gid mappings"); + } +if (result =='N' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new UTS namespace"); + } + +/* Should fail with ESTALE since we're in a different user namespace */ +ASSERT_EQ (result,'P' ); + +close (pipefd[0 ]); +free (handle); +} + +TEST (nsfs_user_ipc_namespace_isolation) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +pid_t pid; +int status; +int pipefd[2 ]; +char result; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Create pipe for communication */ +ASSERT_EQ (pipe (pipefd),0 ); + +/* Get handle for current IPC namespace */ + ns_fd =open ("/proc/self/ns/ipc" ,O_RDONLY ); +ASSERT_GE (ns_fd,0 ); + + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); close(pipefd[0]); +close (pipefd[1 ]); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +close (ns_fd); + + pid =fork (); +ASSERT_GE (pid,0 ); + +if (pid ==0 ) { +/* Child process */ +close (pipefd[0 ]); + +/* First create new user namespace to drop privileges */ + ret =unshare (CLONE_NEWUSER ); +if (ret <0 ) { +write (pipefd[1 ],"U" , +1 );/* Unable to create user namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Write uid/gid mappings to maintain some capabilities */ +int uid_map_fd =open ("/proc/self/uid_map" ,O_WRONLY ); +int gid_map_fd =open ("/proc/self/gid_map" ,O_WRONLY ); +int setgroups_fd =open ("/proc/self/setgroups" ,O_WRONLY ); + +if (uid_map_fd <0 || gid_map_fd <0 || setgroups_fd <0 ) { +write (pipefd[1 ],"M" ,1 );/* Unable to set mappings */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Disable setgroups to allow gid mapping */ +write (setgroups_fd,"deny" ,4 ); +close (setgroups_fd); + +/* Map current uid/gid to root in the new namespace */ +char mapping[64 ]; +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getuid ()); +write (uid_map_fd, mapping,strlen (mapping)); +close (uid_map_fd); + +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getgid ()); +write (gid_map_fd, mapping,strlen (mapping)); +close (gid_map_fd); + +/* Now create new IPC namespace */ + ret =unshare (CLONE_NEWIPC ); +if (ret <0 ) { +write (pipefd[1 ],"N" , +1 );/* Unable to create IPC namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Try to open parent's IPC namespace handle from new user+ipc namespace */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); + +if (fd >=0 ) { +/* Should NOT succeed - we're in a different user namespace */ +write (pipefd[1 ],"S" ,1 );/* Unexpected success */ +close (fd); + }else if (errno ==ESTALE ) { +/* Expected: Stale file handle */ +write (pipefd[1 ],"P" ,1 ); + }else { +/* Other error */ +write (pipefd[1 ],"F" ,1 ); + } + +close (pipefd[1 ]); +exit (0 ); + } + +/* Parent process */ +close (pipefd[1 ]); +ASSERT_EQ (read (pipefd[0 ], &result,1 ),1 ); + +waitpid (pid, &status,0 ); +ASSERT_TRUE (WIFEXITED (status)); +ASSERT_EQ (WEXITSTATUS (status),0 ); + +if (result =='U' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new user namespace"); + } +if (result =='M' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot set uid/gid mappings"); + } +if (result =='N' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new IPC namespace"); + } + +/* Should fail with ESTALE since we're in a different user namespace */ +ASSERT_EQ (result,'P' ); + +close (pipefd[0 ]); +free (handle); +} + +TEST (nsfs_user_mnt_namespace_isolation) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +pid_t pid; +int status; +int pipefd[2 ]; +char result; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Create pipe for communication */ +ASSERT_EQ (pipe (pipefd),0 ); + +/* Get handle for current mount namespace */ + ns_fd =open ("/proc/self/ns/mnt" ,O_RDONLY ); +ASSERT_GE (ns_fd,0 ); + + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); close(pipefd[0]); +close (pipefd[1 ]); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +close (ns_fd); + + pid =fork (); +ASSERT_GE (pid,0 ); + +if (pid ==0 ) { +/* Child process */ +close (pipefd[0 ]); + +/* First create new user namespace to drop privileges */ + ret =unshare (CLONE_NEWUSER ); +if (ret <0 ) { +write (pipefd[1 ],"U" , +1 );/* Unable to create user namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Write uid/gid mappings to maintain some capabilities */ +int uid_map_fd =open ("/proc/self/uid_map" ,O_WRONLY ); +int gid_map_fd =open ("/proc/self/gid_map" ,O_WRONLY ); +int setgroups_fd =open ("/proc/self/setgroups" ,O_WRONLY ); + +if (uid_map_fd <0 || gid_map_fd <0 || setgroups_fd <0 ) { +write (pipefd[1 ],"M" ,1 );/* Unable to set mappings */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Disable setgroups to allow gid mapping */ +write (setgroups_fd,"deny" ,4 ); +close (setgroups_fd); + +/* Map current uid/gid to root in the new namespace */ +char mapping[64 ]; +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getuid ()); +write (uid_map_fd, mapping,strlen (mapping)); +close (uid_map_fd); + +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getgid ()); +write (gid_map_fd, mapping,strlen (mapping)); +close (gid_map_fd); + +/* Now create new mount namespace */ + ret =unshare (CLONE_NEWNS ); +if (ret <0 ) { +write (pipefd[1 ],"N" , +1 );/* Unable to create mount namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Try to open parent's mount namespace handle from new user+mnt namespace */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); + +if (fd >=0 ) { +/* Should NOT succeed - we're in a different user namespace */ +write (pipefd[1 ],"S" ,1 );/* Unexpected success */ +close (fd); + }else if (errno ==ESTALE ) { +/* Expected: Stale file handle */ +write (pipefd[1 ],"P" ,1 ); + }else { +/* Other error */ +write (pipefd[1 ],"F" ,1 ); + } + +close (pipefd[1 ]); +exit (0 ); + } + +/* Parent process */ +close (pipefd[1 ]); +ASSERT_EQ (read (pipefd[0 ], &result,1 ),1 ); + +waitpid (pid, &status,0 ); +ASSERT_TRUE (WIFEXITED (status)); +ASSERT_EQ (WEXITSTATUS (status),0 ); + +if (result =='U' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new user namespace"); + } +if (result =='M' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot set uid/gid mappings"); + } +if (result =='N' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new mount namespace"); + } + +/* Should fail with ESTALE since we're in a different user namespace */ +ASSERT_EQ (result,'P' ); + +close (pipefd[0 ]); +free (handle); +} + +TEST (nsfs_user_cgroup_namespace_isolation) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +pid_t pid; +int status; +int pipefd[2 ]; +char result; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Create pipe for communication */ +ASSERT_EQ (pipe (pipefd),0 ); + +/* Get handle for current cgroup namespace */ + ns_fd =open ("/proc/self/ns/cgroup" ,O_RDONLY ); +if (ns_fd <0 ) { + SKIP(free(handle); close(pipefd[0]); close(pipefd[1]); + return, "cgroup namespace not available"); + } + + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); close(pipefd[0]); +close (pipefd[1 ]); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +close (ns_fd); + + pid =fork (); +ASSERT_GE (pid,0 ); + +if (pid ==0 ) { +/* Child process */ +close (pipefd[0 ]); + +/* First create new user namespace to drop privileges */ + ret =unshare (CLONE_NEWUSER ); +if (ret <0 ) { +write (pipefd[1 ],"U" , +1 );/* Unable to create user namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Write uid/gid mappings to maintain some capabilities */ +int uid_map_fd =open ("/proc/self/uid_map" ,O_WRONLY ); +int gid_map_fd =open ("/proc/self/gid_map" ,O_WRONLY ); +int setgroups_fd =open ("/proc/self/setgroups" ,O_WRONLY ); + +if (uid_map_fd <0 || gid_map_fd <0 || setgroups_fd <0 ) { +write (pipefd[1 ],"M" ,1 );/* Unable to set mappings */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Disable setgroups to allow gid mapping */ +write (setgroups_fd,"deny" ,4 ); +close (setgroups_fd); + +/* Map current uid/gid to root in the new namespace */ +char mapping[64 ]; +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getuid ()); +write (uid_map_fd, mapping,strlen (mapping)); +close (uid_map_fd); + +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getgid ()); +write (gid_map_fd, mapping,strlen (mapping)); +close (gid_map_fd); + +/* Now create new cgroup namespace */ + ret =unshare (CLONE_NEWCGROUP ); +if (ret <0 ) { +write (pipefd[1 ],"N" , +1 );/* Unable to create cgroup namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Try to open parent's cgroup namespace handle from new user+cgroup namespace */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); + +if (fd >=0 ) { +/* Should NOT succeed - we're in a different user namespace */ +write (pipefd[1 ],"S" ,1 );/* Unexpected success */ +close (fd); + }else if (errno ==ESTALE ) { +/* Expected: Stale file handle */ +write (pipefd[1 ],"P" ,1 ); + }else { +/* Other error */ +write (pipefd[1 ],"F" ,1 ); + } + +close (pipefd[1 ]); +exit (0 ); + } + +/* Parent process */ +close (pipefd[1 ]); +ASSERT_EQ (read (pipefd[0 ], &result,1 ),1 ); + +waitpid (pid, &status,0 ); +ASSERT_TRUE (WIFEXITED (status)); +ASSERT_EQ (WEXITSTATUS (status),0 ); + +if (result =='U' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new user namespace"); + } +if (result =='M' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot set uid/gid mappings"); + } +if (result =='N' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new cgroup namespace"); + } + +/* Should fail with ESTALE since we're in a different user namespace */ +ASSERT_EQ (result,'P' ); + +close (pipefd[0 ]); +free (handle); +} + +TEST (nsfs_user_pid_namespace_isolation) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +pid_t pid; +int status; +int pipefd[2 ]; +char result; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Create pipe for communication */ +ASSERT_EQ (pipe (pipefd),0 ); + +/* Get handle for current PID namespace */ + ns_fd =open ("/proc/self/ns/pid" ,O_RDONLY ); +ASSERT_GE (ns_fd,0 ); + + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); close(pipefd[0]); +close (pipefd[1 ]); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +close (ns_fd); + + pid =fork (); +ASSERT_GE (pid,0 ); + +if (pid ==0 ) { +/* Child process */ +close (pipefd[0 ]); + +/* First create new user namespace to drop privileges */ + ret =unshare (CLONE_NEWUSER ); +if (ret <0 ) { +write (pipefd[1 ],"U" , +1 );/* Unable to create user namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Write uid/gid mappings to maintain some capabilities */ +int uid_map_fd =open ("/proc/self/uid_map" ,O_WRONLY ); +int gid_map_fd =open ("/proc/self/gid_map" ,O_WRONLY ); +int setgroups_fd =open ("/proc/self/setgroups" ,O_WRONLY ); + +if (uid_map_fd <0 || gid_map_fd <0 || setgroups_fd <0 ) { +write (pipefd[1 ],"M" ,1 );/* Unable to set mappings */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Disable setgroups to allow gid mapping */ +write (setgroups_fd,"deny" ,4 ); +close (setgroups_fd); + +/* Map current uid/gid to root in the new namespace */ +char mapping[64 ]; +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getuid ()); +write (uid_map_fd, mapping,strlen (mapping)); +close (uid_map_fd); + +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getgid ()); +write (gid_map_fd, mapping,strlen (mapping)); +close (gid_map_fd); + +/* Now create new PID namespace - requires fork to take effect */ + ret =unshare (CLONE_NEWPID ); +if (ret <0 ) { +write (pipefd[1 ],"N" , +1 );/* Unable to create PID namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Fork again for PID namespace to take effect */ +pid_t child_pid =fork (); +if (child_pid <0 ) { +write (pipefd[1 ],"N" , +1 );/* Unable to fork in PID namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +if (child_pid ==0 ) { +/* Grandchild in new PID namespace */ +/* Try to open parent's PID namespace handle from new user+pid namespace */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); + +if (fd >=0 ) { +/* Should NOT succeed - we're in a different user namespace */ +write (pipefd[1 ],"S" , +1 );/* Unexpected success */ +close (fd); + }else if (errno ==ESTALE ) { +/* Expected: Stale file handle */ +write (pipefd[1 ],"P" ,1 ); + }else { +/* Other error */ +write (pipefd[1 ],"F" ,1 ); + } + +close (pipefd[1 ]); +exit (0 ); + } + +/* Wait for grandchild */ +waitpid (child_pid,NULL ,0 ); +exit (0 ); + } + +/* Parent process */ +close (pipefd[1 ]); +ASSERT_EQ (read (pipefd[0 ], &result,1 ),1 ); + +waitpid (pid, &status,0 ); +ASSERT_TRUE (WIFEXITED (status)); +ASSERT_EQ (WEXITSTATUS (status),0 ); + +if (result =='U' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new user namespace"); + } +if (result =='M' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot set uid/gid mappings"); + } +if (result =='N' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new PID namespace"); + } + +/* Should fail with ESTALE since we're in a different user namespace */ +ASSERT_EQ (result,'P' ); + +close (pipefd[0 ]); +free (handle); +} + +TEST (nsfs_user_time_namespace_isolation) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +pid_t pid; +int status; +int pipefd[2 ]; +char result; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Create pipe for communication */ +ASSERT_EQ (pipe (pipefd),0 ); + +/* Get handle for current time namespace */ + ns_fd =open ("/proc/self/ns/time" ,O_RDONLY ); +if (ns_fd <0 ) { + SKIP(free(handle); close(pipefd[0]); close(pipefd[1]); + return, "time namespace not available"); + } + + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); close(pipefd[0]); +close (pipefd[1 ]); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +close (ns_fd); + + pid =fork (); +ASSERT_GE (pid,0 ); + +if (pid ==0 ) { +/* Child process */ +close (pipefd[0 ]); + +/* First create new user namespace to drop privileges */ + ret =unshare (CLONE_NEWUSER ); +if (ret <0 ) { +write (pipefd[1 ],"U" , +1 );/* Unable to create user namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Write uid/gid mappings to maintain some capabilities */ +int uid_map_fd =open ("/proc/self/uid_map" ,O_WRONLY ); +int gid_map_fd =open ("/proc/self/gid_map" ,O_WRONLY ); +int setgroups_fd =open ("/proc/self/setgroups" ,O_WRONLY ); + +if (uid_map_fd <0 || gid_map_fd <0 || setgroups_fd <0 ) { +write (pipefd[1 ],"M" ,1 );/* Unable to set mappings */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Disable setgroups to allow gid mapping */ +write (setgroups_fd,"deny" ,4 ); +close (setgroups_fd); + +/* Map current uid/gid to root in the new namespace */ +char mapping[64 ]; +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getuid ()); +write (uid_map_fd, mapping,strlen (mapping)); +close (uid_map_fd); + +snprintf (mapping,sizeof (mapping),"0 %d 1" ,getgid ()); +write (gid_map_fd, mapping,strlen (mapping)); +close (gid_map_fd); + +/* Now create new time namespace - requires fork to take effect */ + ret =unshare (CLONE_NEWTIME ); +if (ret <0 ) { +write (pipefd[1 ],"N" , +1 );/* Unable to create time namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +/* Fork again for time namespace to take effect */ +pid_t child_pid =fork (); +if (child_pid <0 ) { +write (pipefd[1 ],"N" , +1 );/* Unable to fork in time namespace */ +close (pipefd[1 ]); +exit (0 ); + } + +if (child_pid ==0 ) { +/* Grandchild in new time namespace */ +/* Try to open parent's time namespace handle from new user+time namespace */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDONLY ); + +if (fd >=0 ) { +/* Should NOT succeed - we're in a different user namespace */ +write (pipefd[1 ],"S" , +1 );/* Unexpected success */ +close (fd); + }else if (errno ==ESTALE ) { +/* Expected: Stale file handle */ +write (pipefd[1 ],"P" ,1 ); + }else { +/* Other error */ +write (pipefd[1 ],"F" ,1 ); + } + +close (pipefd[1 ]); +exit (0 ); + } + +/* Wait for grandchild */ +waitpid (child_pid,NULL ,0 ); +exit (0 ); + } + +/* Parent process */ +close (pipefd[1 ]); +ASSERT_EQ (read (pipefd[0 ], &result,1 ),1 ); + +waitpid (pid, &status,0 ); +ASSERT_TRUE (WIFEXITED (status)); +ASSERT_EQ (WEXITSTATUS (status),0 ); + +if (result =='U' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new user namespace"); + } +if (result =='M' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot set uid/gid mappings"); + } +if (result =='N' ) { + SKIP(free(handle); close(pipefd[0]); + return, "Cannot create new time namespace"); + } + +/* Should fail with ESTALE since we're in a different user namespace */ +ASSERT_EQ (result,'P' ); + +close (pipefd[0 ]); +free (handle); +} + +TEST (nsfs_open_flags) +{ +struct file_handle *handle; +int mount_id; +int ret; +int fd; +int ns_fd; +char ns_path[256 ]; + + handle =malloc (sizeof (*handle) +MAX_HANDLE_SZ ); +ASSERT_NE (handle,NULL ); + +/* Open a namespace file descriptor */ +snprintf (ns_path,sizeof (ns_path),"/proc/self/ns/net" ); + ns_fd =open (ns_path,O_RDONLY ); +ASSERT_GE (ns_fd,0 ); + +/* Get handle for the namespace */ + handle->handle_bytes =MAX_HANDLE_SZ ; + ret =name_to_handle_at (ns_fd,"" , handle, &mount_id,AT_EMPTY_PATH ); +if (ret <0 && errno ==EOPNOTSUPP ) { + SKIP(free(handle); close(ns_fd); + return, "nsfs doesn't support file handles"); + } +ASSERT_EQ (ret,0 ); +ASSERT_GT (handle->handle_bytes,0 ); + +/* Test invalid flags that should fail */ + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_WRONLY ); +ASSERT_LT (fd,0 ); +ASSERT_EQ (errno,EPERM ); + + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_RDWR ); +ASSERT_LT (fd,0 ); +ASSERT_EQ (errno,EPERM ); + + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_CREAT ); +ASSERT_LT (fd,0 ); +ASSERT_EQ (errno,EINVAL ); + + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_TRUNC ); +ASSERT_LT (fd,0 ); +ASSERT_EQ (errno,EINVAL ); + + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_APPEND ); +ASSERT_LT (fd,0 ); +ASSERT_EQ (errno,EINVAL ); + + fd =open_by_handle_at (FD_NSFS_ROOT , handle,O_DIRECT ); +ASSERT_LT (fd,0 ); +ASSERT_EQ (errno,EINVAL ); + +close (ns_fd); +free (handle); +} + +TEST_HARNESS_MAIN --2.47.3
Re: [PATCH 32/32] selftests/namespaces: add file handle selftests
Quoted 13 lines · Christian Brauner
Quoted 11 lines · Amir Goldstein
Quoted 4 lines · Christian Brauner
Add a bunch of selftests for namespace file handles.
Signed-off-by Christian Brauner <[email protected]>Obviously, I did not go over every single line, but for the general test template and test coverage you may add:
Reviewed-by Amir Goldstein <[email protected]>
Quoted 9 lines
Quoted 2 lines
However, see my comment on file handle support patch. The test matrix is incomplete.
I mean, I'll just drop to non-root in the non-cross ns tests:
/* Drop to unprivileged uid/gid */
ASSERT_EQ(setresgid(65534, 65534, 65534), 0); /* nogroup */ASSERT_EQ(setresuid(65534, 65534, 65534), 0); /* nobody */
That would be good I think.
Quoted 10 lines
Quoted 10 lines
Maybe it would be complete if test is run as root and then as non root, but then I think the test needs some changes for running as root and opening non-self ns.
I am not sure what the standard is wrt running the selftests as root /non-root.
I see that the userns isolation tests do:
/* Map current uid/gid to root in the new namespace */
Quoted 7 lines
Quoted 2 lines
Are you assuming that non root is running this test or am I missing something?
No, I'm not assuming that. I just need a new user namespace and become root in it to assume privilege over it so I can test that decoding doesn't work from an ancestor userns owned namespace.
With dropping to unprivileged uid/gid in parent, I understand it should work. I guess I wasn't sure if dropping to unprivileged uid/gid was required for the test to pass when the test is run as root user, but with the addition of dropping to unprivileged uid/gid - feel free to add:
Thanks,
Amir.
Re: [PATCH 32/32] selftests/namespaces: add file handle selftests
In reply to Christian BraunerReview
Quoted 2 lines · Christian Brauner
(quoted diff) +2
-0+ snprintf(ns_path, sizeof(ns_path), "/proc/self/ns/net"); + ns_fd = open(ns_path, O_RDONLY);
Here and also in TEST(nsfs_uts_handle), ns_path is not modified. Does this mean that "/proc/self/ns/net" can be stored in a static const char array and also that the snprintf() call can be left out? In case I would have missed the reason why the path is copied, how about using asprintf() or strdup() instead of snprintf()?
Thanks,
Bart.
Christian Brauner
maintainerRe: [PATCH 32/32] selftests/namespaces: add file handle selftests
Quoted 9 lines · Bart Van Assche
Review
Quoted 2 lines · Christian Brauner
(quoted diff) +2
-0+ snprintf(ns_path, sizeof(ns_path), "/proc/self/ns/net"); + ns_fd = open(ns_path, O_RDONLY);Here and also in
TEST(nsfs_uts_handle),ns_pathis not modified. Does this mean that "/proc/self/ns/net" can be stored in a static const char array and also that thesnprintf()call can be left out? In case I would have missed the reason why the path is copied, how about usingasprintf()orstrdup()instead ofsnprintf()?
Yep, that can just be a static string. Thanks.
syzbot ci has tested the following series
syzbot ci has tested the following series
[v1] ns: support file handles
https://lore.kernel.org/all/[email protected]
* [PATCH 01/32] pidfs: validate extensible ioctls
* [PATCH 02/32] nsfs: validate extensible ioctls
* [PATCH 03/32] block: use extensible_ioctl_valid()
* [PATCH 04/32] ns: move to_ns_common() to ns_common.h
* [PATCH 05/32] nsfs: add nsfs.h header
* [PATCH 06/32] ns: uniformly initialize ns_common
* [PATCH 07/32] mnt: use ns_common_init()
* [PATCH 08/32] ipc: use ns_common_init()
* [PATCH 09/32] cgroup: use ns_common_init()
* [PATCH 10/32] pid: use ns_common_init()
* [PATCH 11/32] time: use ns_common_init()
* [PATCH 12/32] uts: use ns_common_init()
* [PATCH 13/32] user: use ns_common_init()
* [PATCH 14/32] net: use ns_common_init()
* [PATCH 15/32] ns: remove ns_alloc_inum()
* [PATCH 16/32] nstree: make iterator generic
* [PATCH 17/32] mnt: support iterator
* [PATCH 18/32] cgroup: support iterator
* [PATCH 19/32] ipc: support iterator
* [PATCH 20/32] net: support iterator
* [PATCH 21/32] pid: support iterator
* [PATCH 22/32] time: support iterator
* [PATCH 23/32] userns: support iterator
* [PATCH 24/32] uts: support iterator
* [PATCH 25/32] ns: add to_<type>_ns() to respective headers
* [PATCH 26/32] nsfs: add current_in_namespace()
* [PATCH 27/32] nsfs: support file handles
* [PATCH 28/32] nsfs: support exhaustive file handles
* [PATCH 29/32] nsfs: add missing id retrieval support
* [PATCH 30/32] tools: update nsfs.h uapi header
* [PATCH 31/32] selftests/namespaces: add identifier selftests
* [PATCH 32/32] selftests/namespaces: add file handle selftests
and found the following issue:
WARNING in copy_net_ns
Full report is available here:
https://ci.syzbot.org/series/bc3dfd83-98cc-488c-b046-f849c79a6a41
***
WARNING in copy_net_ns
tree: net-next URL: https://kernel.googlesource.com/pub/scm/linux/kernel/git/netdev/net-next.git base: deb105f49879dd50d595f7f55207d6e74dec34e6 arch: amd64 compiler: Debian clang version 20.1.8 (++20250708063551+0c9f909b7976-1~exp1~20250708183702.136), Debian LLD 20.1.8 config: https://ci.syzbot.org/builds/a560fd28-b788-4442-a7c8-10c6240b4dbf/config syz repro: https://ci.syzbot.org/findings/18e91b10-567e-4cae-a279-8a5f2f2cde80/syz_repro
------------[ cut here ]------------ ida_free called for id=1326 which is not allocated. WARNING: CPU: 0 PID: 6146 at lib/idr.c:592 ida_free+0x280/0x310 lib/idr.c:592 Modules linked in: CPU: 0 UID: 0 PID: 6146 Comm: syz.1.60 Not tainted syzkaller #0 PREEMPT(full) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014 RIP: 0010:ida_free+0x280/0x310 lib/idr.c:592 Code: 00 00 00 00 fc ff df 48 8b 5c 24 10 48 8b 7c 24 40 48 89 de e8 d1 8a 0c 00 90 48 c7 c7 80 ee ba 8c 44 89 fe e8 11 87 12 f6 90 <0f> 0b 90 90 eb 34 e8 95 02 4f f6 49 bd 00 00 00 00 00 fc ff df eb RSP: 0018:ffffc9000302fba0 EFLAGS: 00010246 RAX: c838d58ce4bb0000 RBX: 0000000000000a06 RCX: ffff88801eac0000 RDX: 0000000000000000 RSI: 0000000000000001 RDI: 0000000000000002 RBP: ffffc9000302fca0 R08: ffff88804b024293 R09: 1ffff11009604852 R10: dffffc0000000000 R11: ffffed1009604853 R12: 1ffff92000605f78 R13: dffffc0000000000 R14: ffff888026c1fd00 R15: 000000000000052e FS: 00007f6d7aab16c0(0000) GS:ffff8880b8613000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000200000004000 CR3: 000000002726e000 CR4: 00000000000006f0 Call Trace: <TASK> copy_net_ns+0x37a/0x510 net/core/net_namespace.c:593 create_new_namespaces+0x3f3/0x720 kernel/nsproxy.c:110 unshare_nsproxy_namespaces+0x11c/0x170 kernel/nsproxy.c:218 ksys_unshare+0x4c8/0x8c0 kernel/fork.c:3127 __do_sys_unshare kernel/fork.c:3198 [inline] __se_sys_unshare kernel/fork.c:3196 [inline] __x64_sys_unshare+0x38/0x50 kernel/fork.c:3196 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0xfa/0x3b0 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7f6d79b8eba9 Code: ff ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 40 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 a8 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007f6d7aab1038 EFLAGS: 00000246 ORIG_RAX: 0000000000000110 RAX: ffffffffffffffda RBX: 00007f6d79dd5fa0 RCX: 00007f6d79b8eba9 RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000062040200 RBP: 00007f6d79c11e19 R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000 R13: 00007f6d79dd6038 R14: 00007f6d79dd5fa0 R15: 00007ffd5ab830f8 </TASK>
***
If these findings have caused you to resend the series or submit a separate fix, please add the following tag to your commit message:
---
This report is generated by a bot. It may contain errors. syzbot ci engineers can be reached at [email protected].