From 2c24c012bd1f4d92aa38861ff171525b8de1f060 Mon Sep 17 00:00:00 2001 From: nekral-guest Date: Fri, 9 Dec 2011 22:13:02 +0000 Subject: [PATCH] * lib/prototypes.h, lib/Makefile.am, po/POTFILES.in, libmisc/copydir.c, lib/selinux.c: Move set_selinux_file_context() and reset_selinux_file_context() from libmisc/copydir.c to lib/selinux.c. * lib/commonio.c: Use set_selinux_file_context() and reset_selinux_file_context() instead of using the existing database SELinux context to set the context for the newly created files. --- ChangeLog | 11 +++++ lib/Makefile.am | 1 + lib/commonio.c | 58 +++++--------------------- lib/prototypes.h | 4 ++ lib/selinux.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++ libmisc/copydir.c | 64 ----------------------------- po/POTFILES.in | 1 + 7 files changed, 130 insertions(+), 111 deletions(-) create mode 100644 lib/selinux.c diff --git a/ChangeLog b/ChangeLog index 2c76703c..dc4a739a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2011-12-09 Peter Vrabec + + * lib/prototypes.h, lib/Makefile.am, po/POTFILES.in, + libmisc/copydir.c, lib/selinux.c: Move set_selinux_file_context() + and reset_selinux_file_context() from libmisc/copydir.c to + lib/selinux.c. + * lib/commonio.c: Use set_selinux_file_context() and + reset_selinux_file_context() instead of using the existing + database SELinux context to set the context for the newly created + files. + 2011-12-09 Nicolas François * src/vipw.c: Do not use a hardcoded program name in the usage diff --git a/lib/Makefile.am b/lib/Makefile.am index 8a1df3bd..c448dd30 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -39,6 +39,7 @@ libshadow_la_SOURCES = \ pwio.c \ pwio.h \ pwmem.c \ + selinux.c \ semanage.c \ sgetgrent.c \ sgetpwent.c \ diff --git a/lib/commonio.c b/lib/commonio.c index dbc0990a..11e46cd0 100644 --- a/lib/commonio.c +++ b/lib/commonio.c @@ -45,9 +45,6 @@ #include #include #include "nscd.h" -#ifdef WITH_SELINUX -#include -#endif /* WITH_SELINUX */ #ifdef WITH_TCB #include #endif /* WITH_TCB */ @@ -652,15 +649,6 @@ int commonio_open (struct commonio_db *db, int mode) /* Do not inherit fd in spawned processes (e.g. nscd) */ fcntl (fileno (db->fp), F_SETFD, FD_CLOEXEC); -#ifdef WITH_SELINUX - db->scontext = NULL; - if ((is_selinux_enabled () > 0) && (!db->readonly)) { - if (fgetfilecon (fileno (db->fp), &db->scontext) < 0) { - goto cleanup_errno; - } - } -#endif /* WITH_SELINUX */ - buflen = BUFLEN; buf = (char *) malloc (buflen); if (NULL == buf) { @@ -745,12 +733,6 @@ int commonio_open (struct commonio_db *db, int mode) cleanup_errno: saved_errno = errno; free_linked_list (db); -#ifdef WITH_SELINUX - if (db->scontext != NULL) { - freecon (db->scontext); - db->scontext = NULL; - } -#endif /* WITH_SELINUX */ fclose (db->fp); db->fp = NULL; errno = saved_errno; @@ -932,10 +914,6 @@ int commonio_close (struct commonio_db *db) int errors = 0; struct stat sb; -#ifdef WITH_SELINUX - /*@null@*/security_context_t old_context = NULL; -#endif /* WITH_SELINUX */ - if (!db->isopen) { errno = EINVAL; return 0; @@ -959,23 +937,17 @@ int commonio_close (struct commonio_db *db) db->fp = NULL; goto fail; } -#ifdef WITH_SELINUX - if (db->scontext != NULL) { - if (getfscreatecon (&old_context) < 0) { - errors++; - goto fail; - } - if (setfscreatecon (db->scontext) < 0) { - errors++; - goto fail; - } - } -#endif /* WITH_SELINUX */ + /* * Create backup file. */ snprintf (buf, sizeof buf, "%s-", db->filename); +#ifdef WITH_SELINUX + if (set_selinux_file_context (buf) != 0) { + errors++; + } +#endif if (create_backup (buf, db->fp) != 0) { errors++; } @@ -984,6 +956,11 @@ int commonio_close (struct commonio_db *db) errors++; } +#ifdef WITH_SELINUX + if (reset_selinux_file_context () != 0) { + errors++; + } +#endif if (errors != 0) { db->fp = NULL; goto fail; @@ -1040,19 +1017,6 @@ int commonio_close (struct commonio_db *db) errors++; success: -#ifdef WITH_SELINUX - if (db->scontext != NULL) { - if (NULL != old_context) { - if (setfscreatecon (old_context) < 0) { - errors++; - } - freecon (old_context); - old_context = NULL; - } - freecon (db->scontext); - db->scontext = NULL; - } -#endif /* WITH_SELINUX */ free_linked_list (db); return errors == 0; } diff --git a/lib/prototypes.h b/lib/prototypes.h index 85758b8d..f001a331 100644 --- a/lib/prototypes.h +++ b/lib/prototypes.h @@ -297,6 +297,10 @@ extern void process_root_flag (const char* short_opt, int argc, char **argv); /* salt.c */ extern /*@observer@*/const char *crypt_make_salt (/*@null@*//*@observer@*/const char *meth, /*@null@*/void *arg); +/* selinux.c */ +extern int set_selinux_file_context (const char *dst_name); +extern int reset_selinux_file_context (void); + /* semanage.c */ extern int set_seuser(const char *login_name, const char *seuser_name); extern int del_seuser(const char *login_name); diff --git a/lib/selinux.c b/lib/selinux.c new file mode 100644 index 00000000..7c29f79f --- /dev/null +++ b/lib/selinux.c @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2011 , Peter Vrabec + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the copyright holders or contributors may not be used to + * endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +#ifdef WITH_SELINUX + +#include "defines.h" + +#include + + +static bool selinux_checked = false; +static bool selinux_enabled; + +/* + * set_selinux_file_context - Set the security context before any file or + * directory creation. + * + * set_selinux_file_context () should be called before any creation + * of file, symlink, directory, ... + * + * Callers may have to Reset SELinux to create files with default + * contexts with reset_selinux_file_context + */ +int set_selinux_file_context (const char *dst_name) +{ + /*@null@*/security_context_t scontext = NULL; + + if (!selinux_checked) { + selinux_enabled = is_selinux_enabled () > 0; + selinux_checked = true; + } + + if (selinux_enabled) { + /* Get the default security context for this file */ + if (matchpathcon (dst_name, 0, &scontext) < 0) { + if (security_getenforce () != 0) { + return 1; + } + } + /* Set the security context for the next created file */ + if (setfscreatecon (scontext) < 0) { + if (security_getenforce () != 0) { + return 1; + } + } + freecon (scontext); + } + return 0; +} + +/* + * reset_selinux_file_context - Reset the security context to the default + * policy behavior + * + * reset_selinux_file_context () should be called after the context + * was changed with set_selinux_file_context () + */ +int reset_selinux_file_context (void) +{ + if (!selinux_checked) { + selinux_enabled = is_selinux_enabled () > 0; + selinux_checked = true; + } + if (selinux_enabled) { + if (setfscreatecon (NULL) != 0) { + return 1; + } + } + return 0; +} + +#else /* !WITH_SELINUX */ +extern int errno; /* warning: ANSI C forbids an empty source file */ +#endif /* !WITH_SELINUX */ diff --git a/libmisc/copydir.c b/libmisc/copydir.c index 93f2b4f2..7cb5f568 100644 --- a/libmisc/copydir.c +++ b/libmisc/copydir.c @@ -55,10 +55,6 @@ #include #endif /* WITH_ATTR */ -#ifdef WITH_SELINUX -static bool selinux_checked = false; -static bool selinux_enabled; -#endif /* WITH_SELINUX */ static /*@null@*/const char *src_orig; static /*@null@*/const char *dst_orig; @@ -112,66 +108,6 @@ static int fchown_if_needed (int fdst, const struct stat *statp, uid_t old_uid, uid_t new_uid, gid_t old_gid, gid_t new_gid); -#ifdef WITH_SELINUX -/* - * set_selinux_file_context - Set the security context before any file or - * directory creation. - * - * set_selinux_file_context () should be called before any creation - * of file, symlink, directory, ... - * - * Callers may have to Reset SELinux to create files with default - * contexts with reset_selinux_file_context - */ -int set_selinux_file_context (const char *dst_name) -{ - /*@null@*/security_context_t scontext = NULL; - - if (!selinux_checked) { - selinux_enabled = is_selinux_enabled () > 0; - selinux_checked = true; - } - - if (selinux_enabled) { - /* Get the default security context for this file */ - if (matchpathcon (dst_name, 0, &scontext) < 0) { - if (security_getenforce () != 0) { - return 1; - } - } - /* Set the security context for the next created file */ - if (setfscreatecon (scontext) < 0) { - if (security_getenforce () != 0) { - return 1; - } - } - freecon (scontext); - } - return 0; -} - -/* - * reset_selinux_file_context - Reset the security context to the default - * policy behavior - * - * reset_selinux_file_context () should be called after the context - * was changed with set_selinux_file_context () - */ -int reset_selinux_file_context (void) -{ - if (!selinux_checked) { - selinux_enabled = is_selinux_enabled () > 0; - selinux_checked = true; - } - if (selinux_enabled) { - if (setfscreatecon (NULL) != 0) { - return 1; - } - } - return 0; -} -#endif /* WITH_SELINUX */ - #if defined(WITH_ACL) || defined(WITH_ATTR) /* * error_acl - format the error messages for the ACL and EQ libraries. diff --git a/po/POTFILES.in b/po/POTFILES.in index eca476ae..1758f49e 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -17,6 +17,7 @@ lib/port.c lib/pwauth.c lib/pwio.c lib/pwmem.c +lib/selinux.c lib/semanage.c lib/sgetgrent.c lib/sgetpwent.c