forked from SELinuxProject/selinux
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libselinux: add selinux_unshare() to unshare the SELinux namespace
Signed-off-by: Stephen Smalley <stephen.smalley.work@gmail.com>
- Loading branch information
1 parent
b411742
commit 30d7363
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#ifndef _GNU_SOURCE | ||
#define _GNU_SOURCE | ||
#endif | ||
|
||
#include <sched.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <fcntl.h> | ||
#include <unistd.h> | ||
#include <errno.h> | ||
#include <sys/mount.h> | ||
|
||
#include "selinux_internal.h" | ||
#include "policy.h" | ||
|
||
/* | ||
* Precondition: caller must have already done unshare(CLONE_NEWNS) or | ||
* been created via clone(CLONE_NEWNS) so that the selinuxfs mount | ||
* changes do not affect anyone else. | ||
*/ | ||
int selinux_unshare(void) | ||
{ | ||
int ret, fd; | ||
char buf[3] = "1\n"; | ||
|
||
/* unmount any previously mounted selinuxfs since it might be read-only */ | ||
umount2(SELINUXMNT, MNT_DETACH); | ||
|
||
/* mount selinuxfs read-write for the unshare */ | ||
ret = mount(SELINUXFS, SELINUXMNT, SELINUXFS, MS_NOEXEC|MS_NOSUID|MS_NODEV, 0); | ||
if (ret < 0) | ||
return -1; | ||
|
||
/* unshare SELinux namespace */ | ||
fd = open(SELINUXMNT "/unshare", O_WRONLY); | ||
if (fd < 0) | ||
return -1; | ||
|
||
ret = write(fd, buf, sizeof(buf)); | ||
if (ret != sizeof(buf)) { | ||
int sv_errno = errno; | ||
close(fd); | ||
errno = sv_errno; | ||
return -1; | ||
} | ||
close(fd); | ||
|
||
/* mount new selinuxfs instance for this namespace */ | ||
umount2(SELINUXMNT, MNT_DETACH); | ||
ret = mount(SELINUXFS, SELINUXMNT, SELINUXFS, MS_NOEXEC|MS_NOSUID|MS_NODEV, 0); | ||
if (ret < 0) | ||
return -1; | ||
return 0; | ||
} |