Bug 216215 - clone and unshare say CAP_SYS_ADMIN is required to create new namespaces
Summary: clone and unshare say CAP_SYS_ADMIN is required to create new namespaces
Status: RESOLVED DOCUMENTED
Alias: None
Product: Documentation
Classification: Unclassified
Component: man-pages (show other bugs)
Hardware: All Linux
: P1 normal
Assignee: documentation_man-pages@kernel-bugs.osdl.org
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-07-07 18:50 UTC by pxeger
Modified: 2022-09-06 12:31 UTC (History)
1 user (show)

See Also:
Kernel Version:
Subsystem:
Regression: No
Bisected commit-id:


Attachments

Description pxeger 2022-07-07 18:50:06 UTC
According to clone(2) and unshare(2), the various CLONE_NEW* flags for creating new namespaces require CAP_SYS_ADMIN. But this is not the case, and never has been (as best I can tell from some git log grepping in the kernel).
Comment 1 Alejandro Colomar 2022-07-10 22:19:23 UTC
$ cat unshare.c 
#define _GNU_SOURCE
#include <err.h>
#include <sched.h>
#include <stdlib.h>

int main(void)
{
	if (unshare(CLONE_NEWPID) == -1)
		err(EXIT_FAILURE, "unshare(2)");
	exit(EXIT_SUCCESS);
}

$ cc -Wall -Wextra unshare.c
$ sudo setcap 'cap_sys_admin=' a.out 
$ ./a.out 
a.out: unshare(2): Operation not permitted
$ sudo setcap 'cap_sys_admin=eip' a.out 
$ ./a.out 
$ 


CAP_SYS_ADMIN is required, as the example above demonstrates.
Comment 2 pxeger 2022-07-11 08:01:24 UTC
Ah, I understand the confusion I was having now: all namespaces, *except user namespaces*, require CAP_SYS_ADMIN. But creating a new user namespace automatically confers a full set of capabilities. So, when using clone(2) with CLONE_NEWUSER and some other CLONE_NEW* flags for other namespaces, at the same time, you don't need CAP_SYS_ADMIN in the parent, because it's given to the child during the clone call.

Is this worth mentioning somewhere?
Comment 3 Alejandro Colomar 2022-07-19 15:04:36 UTC
Maybe we could add that to NOTES in unshare(2).
Would you mind sending a patch?

Thanks,

Alex

Note You need to log in before you can comment on or make changes to this bug.