From e096e36e50b4b66638ebc4d3c09c2ee35f538dfa Mon Sep 17 00:00:00 2001 From: Stephen Smalley Date: Mon, 11 Jun 2012 13:37:39 -0400 Subject: Set the SELinux security label on new directories. Automatically set the SELinux security label on directories created by init.rc. This avoids the need to separately call restorecon on each such directory from the init.rc file. Also restorecon /dev and /dev/socket after initial policy load so that they are labeled correctly before any other dev nodes or sockets are created. Change-Id: If6af6c4887cdead949737cebdd673957e9273ead Signed-off-by: Stephen Smalley --- init/util.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) (limited to 'init/util.c') diff --git a/init/util.c b/init/util.c index 3a4b10b..8922de5 100755 --- a/init/util.c +++ b/init/util.c @@ -290,12 +290,12 @@ int mkdir_recursive(const char *pathname, mode_t mode) memcpy(buf, pathname, width); buf[width] = 0; if (stat(buf, &info) != 0) { - ret = mkdir(buf, mode); + ret = make_dir(buf, mode); if (ret && errno != EEXIST) return ret; } } - ret = mkdir(pathname, mode); + ret = make_dir(pathname, mode); if (ret && errno != EEXIST) return ret; return 0; @@ -451,3 +451,52 @@ void import_kernel_cmdline(int in_qemu, ptr = x; } } + +int make_dir(const char *path, mode_t mode) +{ + int rc; + +#ifdef HAVE_SELINUX + char *secontext = NULL; + + if (sehandle) { + selabel_lookup(sehandle, &secontext, path, mode); + setfscreatecon(secontext); + } +#endif + + rc = mkdir(path, mode); + +#ifdef HAVE_SELINUX + if (secontext) { + int save_errno = errno; + freecon(secontext); + setfscreatecon(NULL); + errno = save_errno; + } +#endif + return rc; +} + +int restorecon(const char *pathname) +{ +#ifdef HAVE_SELINUX + char *secontext = NULL; + struct stat sb; + int i; + + if (is_selinux_enabled() <= 0 || !sehandle) + return 0; + + if (lstat(pathname, &sb) < 0) + return -errno; + if (selabel_lookup(sehandle, &secontext, pathname, sb.st_mode) < 0) + return -errno; + if (lsetfilecon(pathname, secontext) < 0) { + freecon(secontext); + return -errno; + } + freecon(secontext); +#endif + return 0; +} -- cgit v1.1