Skip to contentSkip to conversation

restorecond: add noreturn attribute to exitApp()

PATCH In review Nicolas Iooss 2 participants 7 messages Mar 5, 2017 – Mar 7, 2017 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/6 restorecond: add noreturn attribute to exitApp()

This makes it possible for static analyzers such as clang's one to understand that strings_list_add() cannot dereference a NULL pointer in the following code:

    if (!newptr)
        exitApp("Out of Memory");
    newptr->string = strdup(string);
---
 restorecond/restorecond.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
restorecond/restorecond.h +1 -1
index a6be584c84a5..db7e50f72d2d 100644
@@ -33,7 +33,7 @@ extern int run_as_user;
 extern int start(void);
 extern int server(int, const char *watch_file);
 
-extern void exitApp(const char *msg);
+extern void exitApp(const char *msg) __attribute__((__noreturn__));
 extern void read_config(int fd,	const char *watch_file);
 
 extern int watch(int fd, const char *watch_file);
Signature · 1 line
2.11.1
#1 lore raw

PATCH 2/6 checkpolicy: add noreturn attribute to usage()

While at it, make usage() static and mark its argument as const.

---
 checkpolicy/checkmodule.c | 2 +-
 checkpolicy/checkpolicy.c | 2 +-
 checkpolicy/test/dismod.c | 2 +-
 checkpolicy/test/dispol.c | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)
checkpolicy/checkmodule.c +1 -1
index 53cc5a04c9a4..46ce258fb832 100644
@@ -124,7 +124,7 @@ static int write_binary_policy(policydb_t * p, FILE *outfp)
 	return policydb_write(p, &pf);
 }
 
-static void usage(char *progname)
+static __attribute__((__noreturn__)) void usage(const char *progname)
 {
 	printf("usage:  %s [-h] [-V] [-b] [-C] [-U handle_unknown] [-m] [-M] [-o FILE] [INPUT]\n", progname);
 	printf("Build base and policy modules.\n");
checkpolicy/checkpolicy.c +1 -1
index c82f6f409271..b98bfcd1f169 100644
@@ -102,7 +102,7 @@ static const char *binfile = "policy";
 
 unsigned int policyvers = POLICYDB_VERSION_MAX;
 
-void usage(char *progname)
+static __attribute__((__noreturn__)) void usage(const char *progname)
 {
 	printf
 	    ("usage:  %s [-b] [-C] [-d] [-U handle_unknown (allow,deny,reject)] [-M]"
checkpolicy/test/dismod.c +1 -1
index 08b039dc1de0..aac13e17fed3 100644
@@ -65,7 +65,7 @@ static const char *symbol_labels[9] = {
 	"levels ", "cats   ", "attribs"
 };
 
-void usage(const char *progname)
+static __attribute__((__noreturn__)) void usage(const char *progname)
 {
 	printf("usage:  %s binary_pol_file\n\n", progname);
 	exit(1);
checkpolicy/test/dispol.c +1 -1
index d91fafdb0e1c..ee0f5f643482 100644
@@ -36,7 +36,7 @@
 
 static policydb_t policydb;
 
-void usage(const char *progname)
+static __attribute__((__noreturn__)) void usage(const char *progname)
 {
 	printf("usage:  %s binary_pol_file\n\n", progname);
 	exit(1);
Signature · 1 line
2.11.1
#2 lore raw

PATCH 3/6 secilc: add noreturn attribute to usage()

In reply to Nicolas Iooss

While at it, make usage() static and mark its argument as const.

---
 secilc/secil2conf.c | 2 +-
 secilc/secilc.c     | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
secilc/secil2conf.c +1 -1
index ff564f08ab8a..5d8fe870394d 100644
@@ -41,7 +41,7 @@
 #endif
 #include <sepol/policydb.h>
 
-void usage(char *prog)
+static __attribute__((__noreturn__)) void usage(const char *prog)
 {
 	printf("Usage: %s [OPTION]... FILE...\n", prog);
 	printf("\n");
secilc/secilc.c +1 -1
index dad1e904eb61..f4ecbeec73fb 100644
@@ -41,7 +41,7 @@
 #endif
 #include <sepol/policydb.h>
 
-void usage(char *prog)
+static __attribute__((__noreturn__)) void usage(const char *prog)
 {
 	printf("Usage: %s [OPTION]... FILE...\n", prog);
 	printf("\n");
Signature · 1 line
2.11.1
#3 lore raw

PATCH 4/6 mcstrans: add noreturn attribute to usage()

In reply to Nicolas Iooss
---
 mcstrans/utils/transcon.c   | 2 +-
 mcstrans/utils/untranscon.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
mcstrans/utils/transcon.c +1 -1
index 5189f0fa8712..f4ded53e9609 100644
@@ -7,7 +7,7 @@
 #include <selinux/selinux.h>
 #include "mcstrans.h"
 
-void usage(const char *progname) 
+static __attribute__((__noreturn__)) void usage(const char *progname)
 {
 	fprintf(stderr, "usage:  %s context\n", progname);
 	exit(1);
mcstrans/utils/untranscon.c +1 -1
index f47b6ea81b14..85cea294e88d 100644
@@ -7,7 +7,7 @@
 #include <selinux/selinux.h>
 #include "mcstrans.h"
 
-void usage(const char *progname) 
+static __attribute__((__noreturn__)) void usage(const char *progname)
 {
 	fprintf(stderr, "usage:  %s context\n", progname);
 	exit(1);
Signature · 1 line
2.11.1
#4 lore raw

PATCH 5/6 semodule-utils: add noreturn attribute to usage()

In reply to Nicolas Iooss
---
 semodule-utils/semodule_deps/semodule_deps.c         | 2 +-
 semodule-utils/semodule_expand/semodule_expand.c     | 2 +-
 semodule-utils/semodule_link/semodule_link.c         | 2 +-
 semodule-utils/semodule_package/semodule_package.c   | 2 +-
 semodule-utils/semodule_package/semodule_unpackage.c | 2 +-
 5 files changed, 5 insertions(+), 5 deletions(-)
semodule-utils/semodule_deps/semodule_deps.c +1 -1
index 8048ee02d3ca..7a7ff2fc9f80 100644
@@ -42,7 +42,7 @@ extern int optind;
  */
 #define BASE_NAME ((char *)"BASE")
 
-static void usage(char *program_name)
+static __attribute__((__noreturn__)) void usage(const char *program_name)
 {
 	printf("usage: %s [-v -g -b] basemodpkg modpkg1 [modpkg2 ... ]\n",
 	       program_name);
semodule-utils/semodule_expand/semodule_expand.c +1 -1
index aeb41b283de3..895cdd788ea2 100644
@@ -28,7 +28,7 @@ int policyvers = 0;
 
 #define EXPANDPOLICY_VERSION "1.0"
 
-static void usage(char *program_name)
+static __attribute__((__noreturn__)) void usage(const char *program_name)
 {
 	printf("usage: %s [-V -a -c [version]] basemodpkg outputfile\n",
 	       program_name);
semodule-utils/semodule_link/semodule_link.c +1 -1
index 1234735637e4..38a6843cd6ce 100644
@@ -25,7 +25,7 @@ char *progname;
 extern char *optarg;
 extern int optind;
 
-static void usage(char *program_name)
+static __attribute__((__noreturn__)) void usage(const char *program_name)
 {
 	printf("usage: %s [-Vv] [-o outfile] basemodpkg modpkg1 [modpkg2]...\n",
 	       program_name);
semodule-utils/semodule_package/semodule_package.c +1 -1
index a25daf59f9e7..3515234e36de 100644
@@ -22,7 +22,7 @@
 char *progname = NULL;
 extern char *optarg;
 
-static void usage(const char *prog)
+static __attribute__((__noreturn__)) void usage(const char *prog)
 {
 	printf("usage: %s -o <output file> -m <module> [-f <file contexts>]\n",
 	       prog);
semodule-utils/semodule_package/semodule_unpackage.c +1 -1
index 5117b39e9ffe..c9124c0f5ac1 100644
@@ -14,7 +14,7 @@
 char *progname = NULL;
 extern char *optarg;
 
-static void usage(void)
+static __attribute__((__noreturn__)) void usage(void)
 {
 	printf("usage: %s ppfile modfile [fcfile]\n", progname);
 	exit(1);
Signature · 1 line
2.11.1
#5 lore raw

PATCH 6/6 policycoreutils: add noreturn attribute to usage()

In reply to Nicolas Iooss
---
 policycoreutils/hll/pp/pp.c                 | 2 +-
 policycoreutils/load_policy/load_policy.c   | 2 +-
 policycoreutils/secon/secon.c               | 2 +-
 policycoreutils/setfiles/restorecon_xattr.c | 2 +-
 policycoreutils/setfiles/setfiles.c         | 2 +-
 policycoreutils/setsebool/setsebool.c       | 2 +-
 6 files changed, 6 insertions(+), 6 deletions(-)
policycoreutils/hll/pp/pp.c +1 -1
index e4fa4c4b467c..b97a9b34816a 100644
@@ -46,7 +46,7 @@ static void log_err(const char *fmt, ...)
 	}
 }
 
-static void usage(int err)
+static __attribute__((__noreturn__)) void usage(int err)
 {
 	fprintf(stderr, "Usage: %s [OPTIONS] [IN_FILE [OUT_FILE]]\n", progname);
 	fprintf(stderr, "\n");
policycoreutils/load_policy/load_policy.c +1 -1
index 47d9b0f6ac3e..7c2c2a7836d8 100644
@@ -17,7 +17,7 @@
 #define PACKAGE "policycoreutils"	/* the name of this package lang translation */
 #endif
 
-void usage(char *progname)
+static __attribute__((__noreturn__)) void usage(const char *progname)
 {
 	fprintf(stderr, _("usage:  %s [-qi]\n"), progname);
 	exit(1);
policycoreutils/secon/secon.c +1 -1
index 134f4ee30722..c29d9fb67fa5 100644
@@ -73,7 +73,7 @@ struct {
 		FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
 		    FALSE, FALSE, FALSE, OPTS_FROM_ARG, {0} } };
 
-static void usage(const char *name, int exit_code)
+static __attribute__((__noreturn__)) void usage(const char *name, int exit_code)
 {
 	fprintf(exit_code ? stderr : stdout,
 		"  Usage: %s [-%s] [ context | - ]\n"
policycoreutils/setfiles/restorecon_xattr.c +1 -1
index 1c39efe04e95..91c087faee93 100644
@@ -14,7 +14,7 @@
 
 #include "restore.h"
 
-static void usage(const char *progname)
+static __attribute__((__noreturn__)) void usage(const char *progname)
 {
 	fprintf(stderr,
 		"\nusage: %s [-vnrmdD] [-e directory] [-f specfile] pathname\n"
policycoreutils/setfiles/setfiles.c +1 -1
index e3df212555a3..6f69c90c6734 100644
@@ -39,7 +39,7 @@ static int iamrestorecon;
 static int ctx_validate; /* Validate contexts */
 static const char *altpath; /* Alternate path to file_contexts */
 
-void usage(const char *const name)
+static __attribute__((__noreturn__)) void usage(const char *const name)
 {
 	if (iamrestorecon) {
 		fprintf(stderr,
policycoreutils/setsebool/setsebool.c +1 -1
index b101f0871c1d..53d3566cffbd 100644
@@ -23,7 +23,7 @@ int verbose = 0;
 
 int setbool(char **list, size_t start, size_t end);
 
-void usage(void)
+static __attribute__((__noreturn__)) void usage(void)
 {
 	fputs
 	    ("\nUsage:  setsebool [ -NPV ] boolean value | bool1=val1 bool2=val2...\n\n",
Signature · 1 line
2.11.1
#6 lore raw
In reply to Nicolas Iooss
Quoted 9 lines · Nicolas Iooss

This makes it possible for static analyzers such as clang's one to understand that strings_list_add() cannot dereference a NULL pointer in the following code:

    if (!newptr)
        exitApp("Out of Memory");
    newptr->string = strdup(string);

I applied these six patches.

Thanks,
Jim

Quoted 18 lines
---
 restorecond/restorecond.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
restorecond/restorecond.h +1 -1
index a6be584c84a5..db7e50f72d2d 100644
@@ -33,7 +33,7 @@ extern int run_as_user;
 extern int start(void);
 extern int server(int, const char *watch_file);
 
-extern void exitApp(const char *msg);
+extern void exitApp(const char *msg) __attribute__((__noreturn__));
 extern void read_config(int fd,	const char *watch_file);
 
 extern int watch(int fd, const char *watch_file);
Signature · 2 lines
James Carter <[email protected]>
National Security Agency