chroot and user namespaces
When playing around with libcamera [1] and br2-readonly-rootfs-overlay [2] I found something.. well.. unexpected. At least at first glance.
What happened was that I encountered this error:
1 $ libcamera-still
2Preview window unavailable
3[0:02:54.785102683] [517] INFO Camera camera_manager.cpp:299 libcamera v0.0.0+67319-2023.02-22-gd530afad-dirty (2024-02-20T16:56:34+01:00)
4[0:02:54.885731084] [518] ERROR Process process.cpp:312 Failed to unshare execution context: Operation not permitted
Failed to unshare execution context: Operation not permitted... what?
I know that libcamera executes proprietary IPAs (Image Processing Algorithms) as black boxes, and that the execution is isolated in their own namespace. But.. not permitted..?
Lets look into the code for libcamera (src/libcamera/process.cpp) [3]:
1int Process::isolate()
2{
3 int ret = unshare(CLONE_NEWUSER | CLONE_NEWNET);
4 if (ret) {
5 ret = -errno;
6 LOG(Process, Error) << "Failed to unshare execution context: "
7 << strerror(-ret);
8 return ret;
9 }
10
11 return 0;
12}
Libcamera does indeed create a user and network namespace for the execution. In Linux, new namespaces is created with unshare(2).
The unshare(2) library call is used to dissassociate parts of its execution context by creating new namespaces. As you can see in the manpage [4], there are a few scenarios that can give us -EPERM as return value:
EPERM The calling process did not have the required privileges for this operation. EPERM CLONE_NEWUSER was specified in flags, but either the effective user ID or the effective group ID of the caller does not have a mapping in the parent namespace (see user_namespaces(7)). EPERM (since Linux 3.9) CLONE_NEWUSER was specified in flags and the caller is in a chroot environment (i.e., the caller's root directory does not match the root directory of the mount namespace in which it resides).
The last one caught my eyes. I was not aware of this, even if it makes very much sense to have it this way.
Unfortunately, the last step of the init-script in the initramfs for br2-readonly-rootfs-overlay is to do chroot(1) to the actual root filesystem. I did initially choose to use chroot because it works with both an initrd and initramfs root filesystem, and the code was intended to be generic.
pivot_root and switch_root can (and should) be used with advantage, but it requires you to now which type of filesystem you are working with.
As the br2-readonly-rootfs-overlay is now only designed to be used with initramfs, I switched to use switch_root and the problem was solved.
For the sake of fun, lets follow the code down to where the error occour.
Code digging
When the application makes a call to unshare(2), which simply is a wrapper in the libc implementation for the system call down to the kernel, it ends up with a syscall(2).
The system call on the kernel side is defined as follow:
1SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
2{
3 return ksys_unshare(unshare_flags);
4}
It only calls ksys_unshare() which interpret the unshare_flags and "unshares" namespaces depending on flags. The flag for unsharing a user namespace is CLONE_NEWNS.
1int ksys_unshare(unsigned long unshare_flags)
2{
3 struct fs_struct *fs, *new_fs = NULL;
4 struct files_struct *new_fd = NULL;
5 struct cred *new_cred = NULL;
6 struct nsproxy *new_nsproxy = NULL;
7 int do_sysvsem = 0;
8 int err;
9
10 /*
11 * If unsharing a user namespace must also unshare the thread group
12 * and unshare the filesystem root and working directories.
13 */
14 if (unshare_flags & CLONE_NEWUSER)
15 unshare_flags |= CLONE_THREAD | CLONE_FS;
16 /*
17 * If unsharing vm, must also unshare signal handlers.
18 */
19 if (unshare_flags & CLONE_VM)
20 unshare_flags |= CLONE_SIGHAND;
21 /*
22 * If unsharing a signal handlers, must also unshare the signal queues.
23 */
24 if (unshare_flags & CLONE_SIGHAND)
25 unshare_flags |= CLONE_THREAD;
26 /*
27 * If unsharing namespace, must also unshare filesystem information.
28 */
29 if (unshare_flags & CLONE_NEWNS)
30 unshare_flags |= CLONE_FS;
31
32 err = check_unshare_flags(unshare_flags);
33 if (err)
34 goto bad_unshare_out;
35 /*
36 * CLONE_NEWIPC must also detach from the undolist: after switching
37 * to a new ipc namespace, the semaphore arrays from the old
38 * namespace are unreachable.
39 */
40 if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM))
41 do_sysvsem = 1;
42 err = unshare_fs(unshare_flags, &new_fs);
43 if (err)
44 goto bad_unshare_out;
45 err = unshare_fd(unshare_flags, NR_OPEN_MAX, &new_fd);
46 if (err)
47 goto bad_unshare_cleanup_fs;
48 err = unshare_userns(unshare_flags, &new_cred);
49 ....
unshare_userns() prepares credentials and then creates the user namespace by calling create_user_ns():
1int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
2{
3 struct cred *cred;
4 int err = -ENOMEM;
5
6 if (!(unshare_flags & CLONE_NEWUSER))
7 return 0;
8
9 cred = prepare_creds();
10 if (cred) {
11 err = create_user_ns(cred);
12 if (err)
13 put_cred(cred);
14 else
15 *new_cred = cred;
16 }
17
18 return err;
19}
Before create_user_ns() creates the actual user namespace, it makes various sanity checks, one of those are if(current_chrooted())
1int create_user_ns(struct cred *new)
2{
3 struct user_namespace *ns, *parent_ns = new->user_ns;
4 kuid_t owner = new->euid;
5 kgid_t group = new->egid;
6 struct ucounts *ucounts;
7 int ret, i;
8
9 ret = -ENOSPC;
10 if (parent_ns->level > 32)
11 goto fail;
12
13 ucounts = inc_user_namespaces(parent_ns, owner);
14 if (!ucounts)
15 goto fail;
16
17 /*
18 * Verify that we can not violate the policy of which files
19 * may be accessed that is specified by the root directory,
20 * by verifying that the root directory is at the root of the
21 * mount namespace which allows all files to be accessed.
22 */
23 ret = -EPERM;
24 if (current_chrooted())
25 goto fail_dec;
26
27 ....
This is how we check if the if the current process is chrooted or not:
1bool current_chrooted(void)
2{
3 /* Does the current process have a non-standard root */
4 struct path ns_root;
5 struct path fs_root;
6 bool chrooted;
7
8 /* Find the namespace root */
9 ns_root.mnt = ¤t->nsproxy->mnt_ns->root->mnt;
10 ns_root.dentry = ns_root.mnt->mnt_root;
11 path_get(&ns_root);
12 while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
13 ;
14
15 get_fs_root(current->fs, &fs_root);
16
17 chrooted = !path_equal(&fs_root, &ns_root);
18
19 path_put(&fs_root);
20 path_put(&ns_root);
21
22 return chrooted;
23}
Summary
Do not create user namespaces for chroot:ed processes. It does not work for good reasons.