]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
maint: fix const correctness warnings with security_context_t
authorPádraig Brady <P@draigBrady.com>
Tue, 1 Jul 2014 01:44:10 +0000 (02:44 +0100)
committerPádraig Brady <P@draigBrady.com>
Tue, 1 Jul 2014 14:45:53 +0000 (15:45 +0100)
The security_context_t type was always an artificial separation
from a standard char* string, and various libselinux using code
assumed both were synonymous.  In addition, prior to libselinux 2.3
function declarations were incorrect wrt constness of this type.
Here we replace security_context_t with char*, and also
provide a wrapper function to cater for the const issue on
older libselinux.

* src/system.h (se_const): A new function to avoid and identify
the const issue on older libselinux.
* src/copy.c: s/security_context_t/char */.
* src/cp.c: Likewise.
* src/id.c: Likewise.
* src/install.c: Likewise.
* src/ls.c: Likewise.
* src/mkdir.c: Likewise.
* src/mkfifo.c: Likewise.
* src/mknod.c: Likewise.
* src/runcon.c: Likewise.
* src/selinux.c: Likewise.
* tests/cp/no-ctx.sh: Likewise.
* src/chcon.c: Likesize.

13 files changed:
src/chcon.c
src/copy.c
src/cp.c
src/id.c
src/install.c
src/ls.c
src/mkdir.c
src/mkfifo.c
src/mknod.c
src/runcon.c
src/selinux.c
src/system.h
tests/cp/no-ctx.sh

index 6940cf54eb63863deee383a258134bc1f954ead3..e253a4639009b6a1282e8396dacd82ffd691bd02 100644 (file)
@@ -91,7 +91,7 @@ static struct option const long_options[] =
    setting any portions selected via the global variables, specified_user,
    specified_role, etc.  */
 static int
-compute_context_from_mask (security_context_t context, context_t *ret)
+compute_context_from_mask (char const *context, context_t *ret)
 {
   bool ok = true;
   context_t new_context = context_new (context);
@@ -140,9 +140,9 @@ compute_context_from_mask (security_context_t context, context_t *ret)
 static int
 change_file_context (int fd, char const *file)
 {
-  security_context_t file_context = NULL;
+  char *file_context = NULL;
   context_t context IF_LINT (= NULL);
-  security_context_t context_string;
+  char const * context_string;
   int errors = 0;
 
   if (specified_context == NULL)
@@ -181,8 +181,8 @@ change_file_context (int fd, char const *file)
   if (file_context == NULL || ! STREQ (context_string, file_context))
     {
       int fail = (affect_symlink_referent
-                  ?  setfileconat (fd, file, context_string)
-                  : lsetfileconat (fd, file, context_string));
+                  ?  setfileconat (fd, file, se_const (context_string))
+                  : lsetfileconat (fd, file, se_const (context_string)));
 
       if (fail)
         {
@@ -409,8 +409,6 @@ one takes effect.\n\
 int
 main (int argc, char **argv)
 {
-  security_context_t ref_context = NULL;
-
   /* Bit flags that control how fts works.  */
   int bit_flags = FTS_PHYSICAL;
 
@@ -542,6 +540,8 @@ main (int argc, char **argv)
 
   if (reference_file)
     {
+      char *ref_context = NULL;
+
       if (getfilecon (reference_file, &ref_context) < 0)
         error (EXIT_FAILURE, errno, _("failed to get security context of %s"),
                quote (reference_file));
@@ -556,7 +556,7 @@ main (int argc, char **argv)
   else
     {
       specified_context = argv[optind++];
-      if (security_check_context (specified_context) < 0)
+      if (security_check_context (se_const (specified_context)) < 0)
         error (EXIT_FAILURE, errno, _("invalid context: %s"),
                quotearg_colon (specified_context));
     }
index eee918a813b03e22e88a0b63b67720aad9e5eaf8..26d5bdd2929b1dfac7416ff1ddb67761cdeeeac5 100644 (file)
@@ -779,7 +779,7 @@ set_process_security_ctx (char const *src_name, char const *dst_name,
       /* Set the default context for the process to match the source.  */
       bool all_errors = !x->data_copy_required || x->require_preserve_context;
       bool some_errors = !all_errors && !x->reduce_diagnostics;
-      security_context_t con;
+      char *con;
 
       if (0 <= lgetfilecon (src_name, &con))
         {
index a2541161ea6bf6ed7e53362594570d8bb98e52f4..99cafa7f56e7ed3b8383391fcec0ad33e56bcef2 100644 (file)
--- a/src/cp.c
+++ b/src/cp.c
@@ -926,7 +926,7 @@ main (int argc, char **argv)
   bool copy_contents = false;
   char *target_directory = NULL;
   bool no_target_directory = false;
-  security_context_t scontext = NULL;
+  char const *scontext = NULL;
 
   initialize_main (&argc, &argv);
   set_program_name (argv[0]);
@@ -1196,7 +1196,7 @@ main (int argc, char **argv)
        if (scontext)
          restorecon (dst_path, 0, true);
    */
-  if (scontext && setfscreatecon (scontext) < 0)
+  if (scontext && setfscreatecon (se_const (scontext)) < 0)
     error (EXIT_FAILURE, errno,
            _("failed to set default file creation context to %s"),
            quote (scontext));
index f46bb41b59b5ed52804f46914e509f2f2fbb0ff7..ccd1e879af5073dd9f7b00898990f48e2f1a6ace 100644 (file)
--- a/src/id.c
+++ b/src/id.c
@@ -58,7 +58,7 @@ static bool ok = true;
 
 /* The SELinux context.  Start with a known invalid value so print_full_info
    knows when 'context' has not been set to a meaningful value.  */
-static security_context_t context = NULL;
+static char *context = NULL;
 
 static struct option const longopts[] =
 {
@@ -220,7 +220,7 @@ main (int argc, char **argv)
       /* Report failure only if --context (-Z) was explicitly requested.  */
       if ((selinux_enabled && getcon (&context) && just_context)
           || (smack_enabled
-              && smack_new_label_from_self ((char **) &context) < 0
+              && smack_new_label_from_self (&context) < 0
               && just_context))
         error (EXIT_FAILURE, 0, _("can't get process context"));
     }
index e7de260c66280cb79d863490a197e3a8f45fb631..c3424227beda7db2bf1666784a42ed4c83208d10 100644 (file)
@@ -218,8 +218,8 @@ need_copy (const char *src_name, const char *dest_name,
   /* compare SELinux context if preserving */
   if (selinux_enabled && x->preserve_security_context)
     {
-      security_context_t file_scontext = NULL;
-      security_context_t to_scontext = NULL;
+      char *file_scontext = NULL;
+      char *to_scontext = NULL;
       bool scontext_match;
 
       if (getfilecon (src_name, &file_scontext) == -1)
@@ -312,7 +312,7 @@ static void
 setdefaultfilecon (char const *file)
 {
   struct stat st;
-  security_context_t scontext = NULL;
+  char *scontext = NULL;
   static bool first_call = true;
 
   if (selinux_enabled != 1)
@@ -786,7 +786,7 @@ main (int argc, char **argv)
   int n_files;
   char **file;
   bool strip_program_specified = false;
-  security_context_t scontext = NULL;
+  char const *scontext = NULL;
   /* set iff kernel has extra selinux system calls */
   selinux_enabled = (0 < is_selinux_enabled ());
 
@@ -943,7 +943,7 @@ main (int argc, char **argv)
     error (EXIT_FAILURE, 0,
            _("cannot set target context and preserve it"));
 
-  if (scontext && setfscreatecon (scontext) < 0)
+  if (scontext && setfscreatecon (se_const (scontext)) < 0)
     error (EXIT_FAILURE, errno,
            _("failed to set default file creation context to %s"),
            quote (scontext));
index 54c6705f1a0d817cf12e82834f766dafcc1df8f9..cd5996eb979f9e9319089e8c065b1276a1fbece8 100644 (file)
--- a/src/ls.c
+++ b/src/ls.c
@@ -208,7 +208,7 @@ struct fileinfo
     mode_t linkmode;
 
     /* security context.  */
-    security_context_t scontext;
+    char *scontext;
 
     bool stat_ok;
 
index 04367ebf6db4b4e88e2bb2a46534741df4b8ebab..eb9693cc9ae36b87172fa6b339160a9c50d6fa59 100644 (file)
@@ -200,7 +200,7 @@ main (int argc, char **argv)
 {
   const char *specified_mode = NULL;
   int optc;
-  security_context_t scontext = NULL;
+  char const *scontext = NULL;
   struct mkdir_options options;
 
   options.make_ancestor_function = NULL;
@@ -272,7 +272,7 @@ main (int argc, char **argv)
       if (is_smack_enabled ())
         ret = smack_set_label_for_self (scontext);
       else
-        ret = setfscreatecon (scontext);
+        ret = setfscreatecon (se_const (scontext));
 
       if (ret < 0)
         error (EXIT_FAILURE, errno,
index 5a52d6a22227d2cb4db38ee05599f20f78713f14..415ae562fd4228df8b5812bad0ed24f14df603af 100644 (file)
@@ -79,7 +79,7 @@ main (int argc, char **argv)
   char const *specified_mode = NULL;
   int exit_status = EXIT_SUCCESS;
   int optc;
-  security_context_t scontext = NULL;
+  char const *scontext = NULL;
   bool set_security_context = false;
 
   initialize_main (&argc, &argv);
@@ -136,7 +136,7 @@ main (int argc, char **argv)
       if (is_smack_enabled ())
         ret = smack_set_label_for_self (scontext);
       else
-        ret = setfscreatecon (scontext);
+        ret = setfscreatecon (se_const (scontext));
 
       if (ret < 0)
         error (EXIT_FAILURE, errno,
index 6e6709492902245a34d52e071c889644ebc3c03e..8f547e9c6193acb71afc14700fc5bfab5d058fff 100644 (file)
@@ -96,7 +96,7 @@ main (int argc, char **argv)
   int optc;
   int expected_operands;
   mode_t node_type;
-  security_context_t scontext = NULL;
+  char const *scontext = NULL;
   bool set_security_context = false;
 
   initialize_main (&argc, &argv);
@@ -192,7 +192,7 @@ main (int argc, char **argv)
       if (is_smack_enabled ())
         ret = smack_set_label_for_self (scontext);
       else
-        ret = setfscreatecon (scontext);
+        ret = setfscreatecon (se_const (scontext));
 
       if (ret < 0)
         error (EXIT_FAILURE, errno,
index 78ba79b74456be46706d60c82944c9c028d4448d..14ccc3d586d5196254cfa8ad7deaab27f47a5ffe 100644 (file)
@@ -115,9 +115,9 @@ main (int argc, char **argv)
   char *user = NULL;
   char *type = NULL;
   char *context = NULL;
-  security_context_t cur_context = NULL;
-  security_context_t file_context = NULL;
-  security_context_t new_context = NULL;
+  char *cur_context = NULL;
+  char *file_context = NULL;
+  char *new_context = NULL;
   bool compute_trans = false;
 
   context_t con;
index 016db162604c2b618e3ae19377698265bc3a7120..ae454f8a344f776374f59ef1049edc82b6fbb406 100644 (file)
@@ -72,10 +72,10 @@ mode_to_security_class (mode_t m)
 */
 
 static int
-computecon (char const *path, mode_t mode, security_context_t * con)
+computecon (char const *path, mode_t mode, char **con)
 {
-  security_context_t scon = NULL;
-  security_context_t tcon = NULL;
+  char *scon = NULL;
+  char *tcon = NULL;
   security_class_t tclass;
   int rc = -1;
 
@@ -111,7 +111,8 @@ int
 defaultcon (char const *path, mode_t mode)
 {
   int rc = -1;
-  security_context_t scon = NULL, tcon = NULL;
+  char *scon = NULL;
+  char *tcon = NULL;
   context_t scontext = 0, tcontext = 0;
   const char *contype;
   char *constr;
@@ -182,7 +183,8 @@ restorecon_private (char const *path, bool local)
 {
   int rc = -1;
   struct stat sb;
-  security_context_t scon = NULL, tcon = NULL;
+  char *scon = NULL;
+  char *tcon = NULL;
   context_t scontext = 0, tcontext = 0;
   const char *contype;
   char *constr;
index f295ad1c2e4287735e92c7a9ff7b2b4be275efcd..162446c11aa50698876afe4008b6549d5a962cc7 100644 (file)
@@ -657,3 +657,9 @@ stzncpy (char *restrict dest, char const *restrict src, size_t len)
 #ifndef ARRAY_CARDINALITY
 # define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
 #endif
+
+/* Avoid const warnings by casting to more portable type.
+   This is to cater for the incorrect const function declarations
+   in selinux.h before libselinux-2.3 (May 2014).
+   When version >= 2.3 is ubiquitous remove this function.  */
+static inline char * se_const (char const * sctx) { return (char *) sctx; }
index 059c84f80449dbaf5ed65a2faccb74e35c898057..8b1328a478213ddeabd1a31db3413faa03e6905e 100755 (executable)
@@ -30,7 +30,7 @@ cat > k.c <<'EOF' || framework_failure_
 #include <selinux/selinux.h>
 #include <errno.h>
 
-int getfilecon (const char *path, security_context_t *con)
+int getfilecon (const char *path, char **con)
 {
   /* Leave a marker so we can identify if the function was intercepted.  */
   fclose(fopen("preloaded", "w"));
@@ -39,7 +39,7 @@ int getfilecon (const char *path, security_context_t *con)
   return -1;
 }
 
-int lgetfilecon (const char *path, security_context_t *con)
+int lgetfilecon (const char *path, char **con)
 { return getfilecon (path, con); }
 EOF