Skip to contentSkip to conversation

checkpolicy: use correct unsigned format specifiers

PATCH In review Christian Göttsche 2 participants 14 messages Nov 12, 2021 – Nov 18, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/12 checkpolicy: use correct unsigned format specifiers

The two variables policydb_lineno and source_lineno are both of the type unsigned long; use the appropriate format specifier.

Found by Cppcheck

---
 checkpolicy/policy_scan.l | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
checkpolicy/policy_scan.l +4 -4
index 129a8a2a..ef9f1899 100644
@@ -308,11 +308,11 @@ GLBLUB				{ return(GLBLUB); }
 int yyerror(const char *msg)
 {
 	if (source_file[0])
-		fprintf(stderr, "%s:%ld:",
+		fprintf(stderr, "%s:%lu:",
 			source_file, source_lineno);
 	else
 		fprintf(stderr, "(unknown source)::");
-	fprintf(stderr, "ERROR '%s' at token '%s' on line %ld:\n%s\n%s\n",
+	fprintf(stderr, "ERROR '%s' at token '%s' on line %lu:\n%s\n%s\n",
 			msg,
 			yytext,
 			policydb_lineno,
@@ -327,11 +327,11 @@ int yywarn(const char *msg)
 		return yyerror(msg);
 
 	if (source_file[0])
-		fprintf(stderr, "%s:%ld:",
+		fprintf(stderr, "%s:%lu:",
 			source_file, source_lineno);
 	else
 		fprintf(stderr, "(unknown source)::");
-	fprintf(stderr, "WARNING '%s' at token '%s' on line %ld:\n%s\n%s\n",
+	fprintf(stderr, "WARNING '%s' at token '%s' on line %lu:\n%s\n%s\n",
 			msg,
 			yytext,
 			policydb_lineno,
Signature · 1 line
2.33.1
#1 lore raw

PATCH 2/12 libsepol: use string literals as format strings

Use string literals as format strings so that compilers can validate the count and types of the inherent arguments.

kernel_to_cil.c: In function ‘class_constraint_rules_to_strs’:
kernel_to_cil.c:301:17: error: format not a string literal, argument types not checked [-Werror=format-nonliteral]
301 | rc = strs_create_and_add(strs, format_str, 3, classkey, perms+1, expr);
| ^~
kernel_to_cil.c: In function ‘class_validatetrans_rules_to_strs’:
kernel_to_cil.c:341:17: error: format not a string literal, argument types not checked [-Werror=format-nonliteral]
341 | rc = strs_create_and_add(strs, format_str, 2, classkey, expr);
| ^~
kernel_to_cil.c: In function ‘cats_ebitmap_to_str’:
kernel_to_cil.c:1068:40: error: format not a string literal, argument types not checked [-Werror=format-nonliteral]
1068 | val_to_name[start], val_to_name[i]);
| ^~~~~~~~~~~

kernel_to_conf.c: In function ‘class_constraint_rules_to_strs’:
kernel_to_conf.c:301:42: error: format not a string literal, argument types not checked [-Werror=format-nonliteral]
301 | flavor, classkey, perms+1, expr);
| ^~~~~~
kernel_to_conf.c: In function ‘cats_ebitmap_to_str’:
kernel_to_conf.c:1059:40: error: format not a string literal, argument types not checked [-Werror=format-nonliteral]
1059 | val_to_name[start], sep, val_to_name[i]);
| ^~~~~~~~~~~
kernel_to_conf.c:1062:25: error: format not a string literal, argument types not checked [-Werror=format-nonliteral]
1062 | len = snprintf(p, remaining, fmt, val_to_name[start]);
| ^~~

module_to_cil.c: In function ‘cond_expr_to_cil’:
module_to_cil.c:1340:25: error: format not a string literal, argument types not checked [-Werror=format-nonliteral]
1340 | rlen = snprintf(new_val, len, fmt_str, op, val1, val2);
| ^~~~
module_to_cil.c: In function ‘constraint_expr_to_string’:
module_to_cil.c:1881:25: error: format not a string literal, argument types not checked [-Werror=format-nonliteral]
1881 | rlen = snprintf(new_val, len, fmt_str, op, val1, val2);
| ^~~~

---
 libsepol/src/kernel_to_cil.c  | 29 +++++++++++++++++------------
 libsepol/src/kernel_to_conf.c | 23 +++++++++++++----------
 libsepol/src/module_to_cil.c  | 22 ++++++++--------------
 3 files changed, 38 insertions(+), 36 deletions(-)
libsepol/src/kernel_to_cil.c +17 -12
index 305567a5..b81cdb22 100644
@@ -278,7 +278,7 @@ static int class_constraint_rules_to_strs(struct policydb *pdb, char *classkey,
 	char *expr = NULL;
 	int is_mls;
 	char *perms;
-	const char *format_str;
+	const char *key_word;
 	struct strs *strs;
 
 	for (curr = constraint_rules; curr != NULL; curr = curr->next) {
@@ -291,14 +291,14 @@ static int class_constraint_rules_to_strs(struct policydb *pdb, char *classkey,
 		perms = sepol_av_to_string(pdb, class->s.value, curr->permissions);
 
 		if (is_mls) {
-			format_str = "(mlsconstrain (%s (%s)) %s)";
+			key_word = "mlsconstrain";
 			strs = mls_list;
 		} else {
-			format_str = "(constrain (%s (%s)) %s)";
+			key_word = "constrain";
 			strs = non_mls_list;
 		}
 
-		rc = strs_create_and_add(strs, format_str, 3, classkey, perms+1, expr);
+		rc = strs_create_and_add(strs, "(%s (%s (%s)) %s)", 4, key_word, classkey, perms+1, expr);
 		free(expr);
 		if (rc != 0) {
 			goto exit;
@@ -319,7 +319,7 @@ static int class_validatetrans_rules_to_strs(struct policydb *pdb, char *classke
 	struct constraint_node *curr;
 	char *expr = NULL;
 	int is_mls;
-	const char *format_str;
+	const char *key_word;
 	struct strs *strs;
 	int rc = 0;
 
@@ -331,14 +331,14 @@ static int class_validatetrans_rules_to_strs(struct policydb *pdb, char *classke
 		}
 
 		if (is_mls) {
-			format_str = "(mlsvalidatetrans %s %s)";
+			key_word = "mlsvalidatetrans";
 			strs = mls_list;
 		} else {
-			format_str = "(validatetrans %s %s)";
+			key_word = "validatetrans";
 			strs = non_mls_list;
 		}
 
-		rc = strs_create_and_add(strs, format_str, 2, classkey, expr);
+		rc = strs_create_and_add(strs, "(%s %s %s)", 3, key_word, classkey, expr);
 		free(expr);
 		if (rc != 0) {
 			goto exit;
@@ -1035,7 +1035,6 @@ static char *cats_ebitmap_to_str(struct ebitmap *cats, char **val_to_name)
 	struct ebitmap_node *node;
 	uint32_t i, start, range;
 	char *catsbuf = NULL, *p;
-	const char *fmt;
 	int len, remaining;
 
 	remaining = (int)cats_ebitmap_len(cats, val_to_name);
@@ -1063,9 +1062,15 @@ static char *cats_ebitmap_to_str(struct ebitmap *cats, char **val_to_name)
 			continue;
 
 		if (range > 1) {
-			fmt = (range == 2) ? "%s %s " : "(range %s %s) ";
-			len = snprintf(p, remaining, fmt,
-				       val_to_name[start], val_to_name[i]);
+			if (range == 2) {
+				len = snprintf(p, remaining, "%s %s ",
+					       val_to_name[start],
+					       val_to_name[i]);
+			} else {
+				len = snprintf(p, remaining, "(range %s %s) ",
+					       val_to_name[start],
+					       val_to_name[i]);
+			}
 		} else {
 			len = snprintf(p, remaining, "%s ", val_to_name[start]);
 		}
libsepol/src/kernel_to_conf.c +13 -10
index eb72e4ac..460209c8 100644
@@ -271,7 +271,7 @@ static int class_constraint_rules_to_strs(struct policydb *pdb, char *classkey,
 {
 	struct constraint_node *curr;
 	struct strs *strs;
-	const char *format_str, *flavor;
+	const char *flavor, *perm_prefix, *perm_suffix;
 	char *perms, *expr;
 	int is_mls;
 	int rc = 0;
@@ -285,9 +285,11 @@ static int class_constraint_rules_to_strs(struct policydb *pdb, char *classkey,
 
 		perms = sepol_av_to_string(pdb, class->s.value, curr->permissions);
 		if (strchr(perms, ' ')) {
-			format_str = "%s %s { %s } %s;";
+			perm_prefix = "{ ";
+			perm_suffix = " }";
 		} else {
-			format_str = "%s %s %s %s";
+			perm_prefix = "";
+			perm_suffix = "";
 		}
 		if (is_mls) {
 			flavor = "mlsconstrain";
@@ -297,8 +299,10 @@ static int class_constraint_rules_to_strs(struct policydb *pdb, char *classkey,
 			strs = non_mls_list;
 		}
 
-		rc = strs_create_and_add(strs, format_str, 4,
-					 flavor, classkey, perms+1, expr);
+		rc = strs_create_and_add(strs, "%s %s %s%s%s %s;", 6,
+					 flavor, classkey,
+					 perm_prefix, perms+1, perm_suffix,
+					 expr);
 		free(expr);
 		if (rc != 0) {
 			goto exit;
@@ -1026,7 +1030,6 @@ static char *cats_ebitmap_to_str(struct ebitmap *cats, char **val_to_name)
 	struct ebitmap_node *node;
 	uint32_t i, start, range, first;
 	char *catsbuf = NULL, *p;
-	const char *fmt;
 	char sep;
 	int len, remaining;
 
@@ -1054,12 +1057,12 @@ static char *cats_ebitmap_to_str(struct ebitmap *cats, char **val_to_name)
 
 		if (range > 1) {
 			sep = (range == 2) ? ',' : '.';
-			fmt = first ? "%s%c%s" : ",%s%c%s";
-			len = snprintf(p, remaining, fmt,
+			len = snprintf(p, remaining, "%s%s%c%s",
+				       first ? "" : ",",
 				       val_to_name[start], sep, val_to_name[i]);
 		} else {
-			fmt = first ? "%s" : ",%s";
-			len = snprintf(p, remaining, fmt, val_to_name[start]);
+			len = snprintf(p, remaining, "%s%s", first ? "" : ",",
+				       val_to_name[start]);
 
 		}
 		if (len < 0 || len >= remaining) {
libsepol/src/module_to_cil.c +8 -14
index 16e4004e..b231d7f8 100644
@@ -1259,7 +1259,7 @@ static int cond_expr_to_cil(int indent, struct policydb *pdb, struct cond_expr *
 	char *val2 = NULL;
 	unsigned int num_params;
 	const char *op;
-	const char *fmt_str;
+	const char *sep;
 	const char *type;
 
 	rc = stack_init(&stack);
@@ -1308,11 +1308,11 @@ static int cond_expr_to_cil(int indent, struct policydb *pdb, struct cond_expr *
 					rc = -1;
 					goto exit;
 				}
-				fmt_str = "(%s %s)";
+				sep = "";
 			} else {
 				val2 = stack_pop(stack);
 				val1 = stack_pop(stack);
-				fmt_str = "(%s %s %s)";
+				sep = " ";
 			}
 
 			if (val1 == NULL || val2 == NULL) {
@@ -1334,10 +1334,7 @@ static int cond_expr_to_cil(int indent, struct policydb *pdb, struct cond_expr *
 				goto exit;
 			}
 
-			// although we always supply val2 and there isn't always a 2nd
-			// value, it should only be used when there are actually two values
-			// in the format strings
-			rlen = snprintf(new_val, len, fmt_str, op, val1, val2);
+			rlen = snprintf(new_val, len, "(%s %s%s%s)", op, val1, sep, val2);
 			if (rlen < 0 || rlen >= len) {
 				log_err("Failed to generate conditional expression");
 				rc = -1;
@@ -1711,7 +1708,7 @@ static int constraint_expr_to_string(struct policydb *pdb, struct constraint_exp
 	char *val2 = NULL;
 	uint32_t num_params;
 	const char *op;
-	const char *fmt_str;
+	const char *sep;
 	const char *attr1;
 	const char *attr2;
 	char *names = NULL;
@@ -1849,11 +1846,11 @@ static int constraint_expr_to_string(struct policydb *pdb, struct constraint_exp
 					rc = -1;
 					goto exit;
 				}
-				fmt_str = "(%s %s)";
+				sep = "";
 			} else {
 				val2 = stack_pop(stack);
 				val1 = stack_pop(stack);
-				fmt_str = "(%s %s %s)";
+				sep = " ";
 			}
 
 			if (val1 == NULL || val2 == NULL) {
@@ -1875,10 +1872,7 @@ static int constraint_expr_to_string(struct policydb *pdb, struct constraint_exp
 				goto exit;
 			}
 
-			// although we always supply val2 and there isn't always a 2nd
-			// value, it should only be used when there are actually two values
-			// in the format strings
-			rlen = snprintf(new_val, len, fmt_str, op, val1, val2);
+			rlen = snprintf(new_val, len, "(%s %s%s%s)", op, val1, sep, val2);
 			if (rlen < 0 || rlen >= len) {
 				log_err("Failed to generate constraint expression");
 				rc = -1;
Signature · 1 line
2.33.1
#2 lore raw

PATCH 3/12 policycoreutils: use string literal as format strings

In reply to Christian Göttsche

Use string literal as format string so that compilers can validate the count and types of the inherent arguments.

sestatus.c: In function ‘printf_tab’:
sestatus.c:175:16: error: format not a string literal, argument types not checked [-Werror=format-nonliteral]
175 | printf(buf, outp);
| ^~~

---
 policycoreutils/sestatus/sestatus.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
policycoreutils/sestatus/sestatus.c +1 -3
index ceee0d52..538c73bc 100644
@@ -170,9 +170,7 @@ void load_checks(char *pc[], int *npc, char *fc[], int *nfc)
 
 void printf_tab(const char *outp)
 {
-	char buf[20];
-	snprintf(buf, sizeof(buf), "%%-%us", COL);
-	printf(buf, outp);
+	printf("%-*s", COL, outp);
 
 }
 
Signature · 1 line
2.33.1
#3 lore raw

PATCH 4/12 Enable extra global compiler warnings

In reply to Christian Göttsche

Enable some extra compiler warnings in the global Makefile. These are supported by GCC and Clang. The global Makefile is almost solely used by developers and the CI, so the interaction with the preexisting -Werror flag is desirable.

---
 Makefile | 4 ++++
 1 file changed, 4 insertions(+)
Makefile +4 -0
index 298cd2b7..215e313e 100644
@@ -9,8 +9,12 @@ ifeq ($(DEBUG),1)
 	export LDFLAGS = -g
 else
 	export CFLAGS ?= -O2 -Werror -Wall -Wextra \
+		-Wfloat-equal \
+		-Wformat=2 \
+		-Winit-self \
 		-Wmissing-format-attribute \
 		-Wmissing-noreturn \
+		-Wnull-dereference \
 		-Wpointer-arith \
 		-Wshadow \
 		-Wstrict-prototypes \
Signature · 1 line
2.33.1
#4 lore raw

PATCH 5/12 checkpolicy: ignore possible string truncation

In reply to Christian Göttsche

The source code line content, saved to improve error reporting, might get truncated, as the current Bison source buffer is 8192 bytes long and only 254 bytes (plus NUL-terminator) are reserved.
As the saved string is only used for improving error reports and source lines longer than 254 character are quite uncommon, simply silence the GCC warning.

In file included from /usr/include/string.h:519,
from lex.yy.c:20:
In function ‘strncpy’,
inlined from ‘yylex’ at policy_scan.l:63:7:
/usr/include/x86_64-linux-gnu/bits/string_fortified.h:91:10: warning: ‘__builtin_strncpy’ output may be truncated copying 255 bytes from a string of length 8190 [-Wstringop-truncation]
91 | return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---
 checkpolicy/policy_scan.l | 7 +++++++
 1 file changed, 7 insertions(+)
checkpolicy/policy_scan.l +7 -0
index ef9f1899..9fefea7b 100644
@@ -60,7 +60,14 @@ hexval	[0-9A-Fa-f]
 
 %%
 \n.*				{
+#if defined(__GNUC__) && __GNUC__ >= 8
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wstringop-truncation"
+#endif
 				  strncpy(linebuf[lno], yytext+1, 255);
+#if defined(__GNUC__) && __GNUC__ >= 8
+#pragma GCC diagnostic pop
+#endif
 				  linebuf[lno][254] = 0;
 				  lno = 1 - lno;
 				  policydb_lineno++;
Signature · 1 line
2.33.1
#5 lore raw

PATCH 6/12 policycoreutils: mark local functions static

In reply to Christian Göttsche

setfiles.c:50:6: warning: no previous prototype for ‘set_rootpath’ [-Wmissing-prototypes]
50 | void set_rootpath(const char *arg)
| ^~~~~~~~~~~~
setfiles.c:67:5: warning: no previous prototype for ‘canoncon’ [-Wmissing-prototypes]
67 | int canoncon(char **contextp)
| ^~~~~~~~

newrole.c:185:5: warning: no previous prototype for ‘authenticate_via_pam’ [-Wmissing-prototypes]
185 | int authenticate_via_pam(const char *ttyn, pam_handle_t * pam_handle)
| ^~~~~~~~~~~~~~~~~~~~

run_init.c:241:5: warning: no previous prototype for ‘authenticate_user’ [-Wmissing-prototypes]
241 | int authenticate_user(void)
| ^~~~~~~~~~~~~~~~~
run_init.c:306:5: warning: no previous prototype for ‘get_init_context’ [-Wmissing-prototypes]
306 | int get_init_context(char **context)
| ^~~~~~~~~~~~~~~~

sestatus.c:38:5: warning: no previous prototype for ‘cmp_cmdline’ [-Wmissing-prototypes]
38 | int cmp_cmdline(const char *command, int pid)
| ^~~~~~~~~~~
sestatus.c:62:5: warning: no previous prototype for ‘pidof’ [-Wmissing-prototypes]
62 | int pidof(const char *command)
| ^~~~~
sestatus.c:95:6: warning: no previous prototype for ‘load_checks’ [-Wmissing-prototypes]
95 | void load_checks(char *pc[], int *npc, char *fc[], int *nfc)
| ^~~~~~~~~~~
sestatus.c:171:6: warning: no previous prototype for ‘printf_tab’ [-Wmissing-prototypes]
171 | void printf_tab(const char *outp)
| ^~~~~~~~~~

---
 policycoreutils/newrole/newrole.c   |  4 ++--
 policycoreutils/run_init/run_init.c | 12 ++++--------
 policycoreutils/sestatus/sestatus.c |  8 ++++----
 policycoreutils/setfiles/setfiles.c |  4 ++--
 4 files changed, 12 insertions(+), 16 deletions(-)
policycoreutils/newrole/newrole.c +2 -2
index 31b51c5a..9d68b6ab 100644
@@ -182,7 +182,7 @@ const char *service_name = "newrole";
  * program.  This is the only function in this program that makes PAM
  * calls.
  */
-int authenticate_via_pam(const char *ttyn, pam_handle_t * pam_handle)
+static int authenticate_via_pam(const char *ttyn, pam_handle_t * pam_handle)
 {
 
 	int result = 0;		/* set to 0 (not authenticated) by default */
@@ -348,7 +348,7 @@ static int read_pam_config(void)
  * This function uses the shadow passwd file to thenticate the user running
  * this program.
  */
-int authenticate_via_shadow_passwd(const char *uname)
+static int authenticate_via_shadow_passwd(const char *uname)
 {
 	struct spwd *p_shadow_line;
 	char *unencrypted_password_s;
policycoreutils/run_init/run_init.c +4 -8
index 545490a2..ce499781 100644
@@ -86,8 +86,6 @@
 				  /* The file containing the context to run 
 				   * the scripts under.                     */
 
-int authenticate_via_pam(const struct passwd *);
-
 /* authenticate_via_pam()
  *
  * in:     p_passwd_line - struct containing data from our user's line in 
@@ -104,7 +102,7 @@ int authenticate_via_pam(const struct passwd *);
  *
  */
 
-int authenticate_via_pam(const struct passwd *p_passwd_line)
+static int authenticate_via_pam(const struct passwd *p_passwd_line)
 {
 
 	int result = 0;		/* our result, set to 0 (not authenticated) by default */
@@ -169,8 +167,6 @@ int authenticate_via_pam(const struct passwd *p_passwd_line)
 
 #define PASSWORD_PROMPT _("Password:")	/* prompt for getpass() */
 
-int authenticate_via_shadow_passwd(const struct passwd *);
-
 /* authenticate_via_shadow_passwd()
  *
  * in:     p_passwd_line - struct containing data from our user's line in 
@@ -187,7 +183,7 @@ int authenticate_via_shadow_passwd(const struct passwd *);
  *
  */
 
-int authenticate_via_shadow_passwd(const struct passwd *p_passwd_line)
+static int authenticate_via_shadow_passwd(const struct passwd *p_passwd_line)
 {
 
 	struct spwd *p_shadow_line;	/* struct derived from shadow passwd file line */
@@ -238,7 +234,7 @@ int authenticate_via_shadow_passwd(const struct passwd *p_passwd_line)
  * return:	0 When success
  *		-1 When failure
  */
-int authenticate_user(void)
+static int authenticate_user(void)
 {
 
 #define INITLEN 255
@@ -303,7 +299,7 @@ int authenticate_user(void)
  * out:		The CONTEXT associated with the context.
  * return:	0 on success, -1 on failure.
  */
-int get_init_context(char **context)
+static int get_init_context(char **context)
 {
 
 	FILE *fp;
policycoreutils/sestatus/sestatus.c +4 -4
index 538c73bc..7dcc9944 100644
@@ -35,7 +35,7 @@ static unsigned int COL = 32;
 
 extern char *selinux_mnt;
 
-int cmp_cmdline(const char *command, int pid)
+static int cmp_cmdline(const char *command, int pid)
 {
 
 	char buf[BUFSIZE];
@@ -59,7 +59,7 @@ int cmp_cmdline(const char *command, int pid)
 		return 0;
 }
 
-int pidof(const char *command)
+static int pidof(const char *command)
 {
 /* inspired by killall5.c from psmisc */
 	char stackpath[PATH_MAX + 1], *p;
@@ -92,7 +92,7 @@ int pidof(const char *command)
 	return ret;
 }
 
-void load_checks(char *pc[], int *npc, char *fc[], int *nfc)
+static void load_checks(char *pc[], int *npc, char *fc[], int *nfc)
 {
 
 	FILE *fp = fopen(CONF, "r");
@@ -168,7 +168,7 @@ void load_checks(char *pc[], int *npc, char *fc[], int *nfc)
 	return;
 }
 
-void printf_tab(const char *outp)
+static void printf_tab(const char *outp)
 {
 	printf("%-*s", COL, outp);
 
policycoreutils/setfiles/setfiles.c +2 -2
index f018d161..fedb93a1 100644
@@ -47,7 +47,7 @@ static __attribute__((__noreturn__)) void usage(const char *const name)
 	exit(-1);
 }
 
-void set_rootpath(const char *arg)
+static void set_rootpath(const char *arg)
 {
 	if (strlen(arg) == 1 && strncmp(arg, "/", 1) == 0) {
 		fprintf(stderr, "%s:  invalid alt_rootpath: %s\n",
@@ -64,7 +64,7 @@ void set_rootpath(const char *arg)
 	}
 }
 
-int canoncon(char **contextp)
+static int canoncon(char **contextp)
 {
 	char *context = *contextp, *tmpcon;
 	int rc = 0;
Signature · 1 line
2.33.1
#6 lore raw

PATCH 7/12 sandbox: mark local functions static

In reply to Christian Göttsche
    seunshare.c:92:6: warning: no previous prototype for ‘handler’ [-Wmissing-prototypes]
       92 | void handler(int sig) {
          |      ^~~~~~~
---
 sandbox/seunshare.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sandbox/seunshare.c +1 -1
index d626e98d..8917a0f9 100644
@@ -89,7 +89,7 @@ static int drop_privs(uid_t uid)
 /**
  * If the user sends a siginto to seunshare, kill the child's session
  */
-void handler(int sig) {
+static void handler(int sig) {
 	if (child > 0) kill(-child,sig);
 }
 
Signature · 1 line
2.33.1
#7 lore raw

PATCH 8/12 python: mark local functions static

In reply to Christian Göttsche

sepolgen-ifgen-attr-helper.c:59:5: warning: no previous prototype for ‘render_access_mask’ [-Wmissing-prototypes]
59 | int render_access_mask(uint32_t av, avtab_key_t *key, policydb_t *policydbp,
| ^~~~~~~~~~~~~~~~~~
sepolgen-ifgen-attr-helper.c:114:5: warning: no previous prototype for ‘output_avrule’ [-Wmissing-prototypes]
114 | int output_avrule(avtab_key_t *key, avtab_datum_t *datum, void *args)
| ^~~~~~~~~~~~~
sepolgen-ifgen-attr-helper.c:220:6: warning: no previous prototype for ‘usage’ [-Wmissing-prototypes]
220 | void usage(char *progname)
| ^~~~~

---
 python/audit2allow/sepolgen-ifgen-attr-helper.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
python/audit2allow/sepolgen-ifgen-attr-helper.c +3 -3
index f010c958..6f3ba962 100644
@@ -56,7 +56,7 @@ static int perm_name(hashtab_key_t key, hashtab_datum_t datum, void *data)
 	return 0;
 }
 
-int render_access_mask(uint32_t av, avtab_key_t *key, policydb_t *policydbp,
+static int render_access_mask(uint32_t av, avtab_key_t *key, policydb_t *policydbp,
 		       FILE *fp)
 {
 	struct val_to_name v;
@@ -111,7 +111,7 @@ struct callback_data
 	FILE *fp;
 };
 
-int output_avrule(avtab_key_t *key, avtab_datum_t *datum, void *args)
+static int output_avrule(avtab_key_t *key, avtab_datum_t *datum, void *args)
 {
 	struct callback_data *cb_data = (struct callback_data *)args;
 
@@ -217,7 +217,7 @@ static policydb_t *load_policy(const char *filename)
 
 }
 
-void usage(char *progname)
+static void usage(char *progname)
 {
 	printf("usage: %s out_file [policy_file]\n", progname);
 }
Signature · 1 line
2.33.1
#8 lore raw

PATCH 9/12 mcstrans: avoid missing prototypes

In reply to Christian Göttsche

Mark local functions static.
Export functions of mcscolor.c in mcscolor.h and avoid bare extern function declarations.
Drop unused function emit_whitespace().

---
 mcstrans/src/mcscolor.c  |  2 ++
 mcstrans/src/mcscolor.h  |  8 ++++++
 mcstrans/src/mcstrans.c  | 57 ++++++++++++++++++----------------------
 mcstrans/src/mcstrans.h  |  1 -
 mcstrans/src/mcstransd.c | 13 +++------
 5 files changed, 38 insertions(+), 43 deletions(-)
 create mode 100644 mcstrans/src/mcscolor.h
mcstrans/src/mcscolor.c +2 -0
index a3838850..9ff0ce2f 100644
@@ -11,6 +11,8 @@
 #include <syslog.h>
 #include <selinux/selinux.h>
 #include <selinux/context.h>
+
+#include "mcscolor.h"
 #include "mcstrans.h"
 
 /* Define data structures */
mcstrans/src/mcscolor.h new file +8 -0
new file mode 100644
index 00000000..c37fe6ed
@@ -0,0 +1,8 @@
+#ifndef __mcscolor_h__
+#define __mcscolor_h__
+
+extern void finish_context_colors(void);
+extern int init_colors(void);
+extern int raw_color(const char *raw, char **color_str);
+
+#endif
mcstrans/src/mcstrans.c +25 -32

213 lines not on this page · raw message

mcstrans/src/mcstrans.h +0 -1
index e5cda93b..0addb325 100644
@@ -6,4 +6,3 @@ extern int init_translations(void);
 extern void finish_context_translations(void);
 extern int trans_context(const char *, char **);
 extern int untrans_context(const char *, char **);
-
mcstrans/src/mcstransd.c +3 -10
index 59c152e7..536c0f32 100644
@@ -16,6 +16,8 @@
 #include <sys/types.h>
 #include <sys/uio.h>
 #include <sys/un.h>
+
+#include "mcscolor.h"
 #include "mcstrans.h"
 
 #ifdef UNUSED
@@ -43,15 +45,6 @@
 #define log_debug(fmt, ...) do {} while (0)
 #endif
 
-extern int init_translations(void);
-extern void finish_context_translations(void);
-extern int trans_context(const char *, char **);
-extern int untrans_context(const char *, char **);
-
-extern int init_colors(void);
-extern void finish_context_colors(void);
-extern int raw_color(const char *, char **);
-
 #define SETRANSD_PATHNAME "/sbin/mcstransd"
 
 /* name of program (for error messages) */
@@ -514,7 +507,7 @@ initialize(void)
 
 }
 
-void dropprivs(void)
+static void dropprivs(void)
 {
 	cap_t new_caps;
 
Signature · 1 line
2.33.1
#9 lore raw

PATCH 10/12 libsemanage: mark local functions static

In reply to Christian Göttsche

utilities.c:295:18: warning: no previous prototype for ‘list_addafter_controlmem’ [-Wmissing-prototypes]
295 | semanage_list_t *list_addafter_controlmem(semanage_list_t * item, char *data)
| ^~~~~~~~~~~~~~~~~~~~~~~~

---
 libsemanage/src/utilities.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsemanage/src/utilities.c +1 -1
index fc5a6a51..fdbb8ad6 100644
@@ -292,7 +292,7 @@ char *semanage_str_replace(const char *search, const char *replace,
  *
  * returns the newly created node or NULL on error
  */
-semanage_list_t *list_addafter_controlmem(semanage_list_t * item, char *data)
+static semanage_list_t *list_addafter_controlmem(semanage_list_t * item, char *data)
 {
 	semanage_list_t *temp = malloc(sizeof(semanage_list_t));
 
Signature · 1 line
2.33.1
#10 lore raw

PATCH 11/12 libsemanage: include paired header for prototypes

In reply to Christian Göttsche

context_record.c:11:13: warning: no previous prototype for ‘semanage_context_get_user’ [-Wmissing-prototypes]
11 | const char *semanage_context_get_user(const semanage_context_t * con)
| ^~~~~~~~~~~~~~~~~~~~~~~~~

---
 libsemanage/src/context_record.c | 2 ++
 1 file changed, 2 insertions(+)
libsemanage/src/context_record.c +2 -0
index 16ba518e..dafb90fc 100644
@@ -7,6 +7,8 @@ typedef sepol_context_t semanage_context_t;
 
 #define _SEMANAGE_CONTEXT_DEFINED_
 
+#include <semanage/context_record.h>
+
 /* User */
 const char *semanage_context_get_user(const semanage_context_t * con)
 {
Signature · 1 line
2.33.1
#11 lore raw

PATCH 12/12 libsemanage: add extern prototype for legacy function

In reply to Christian Göttsche

modules.c:171:13: warning: no previous prototype for ‘semanage_module_get_version’ [-Wmissing-prototypes]
171 | const char *semanage_module_get_version(semanage_module_info_t * modinfo
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~

---
 libsemanage/src/modules.c | 1 +
 1 file changed, 1 insertion(+)
libsemanage/src/modules.c +1 -0
index b6dd456c..c98df4dd 100644
@@ -168,6 +168,7 @@ const char *semanage_module_get_name(semanage_module_info_t * modinfo)
 /* Legacy function that remains to preserve ABI
  * compatibility.
  */
+extern const char *semanage_module_get_version(semanage_module_info_t *);
 const char *semanage_module_get_version(semanage_module_info_t * modinfo
 				__attribute__ ((unused)))
 {
Signature · 1 line
2.33.1
Acked-by on v1 1/12
#12 lore raw
In reply to Christian Göttsche
Quoted 7 lines · Christian Göttsche

The two variables policydb_lineno and source_lineno are both of the type unsigned long; use the appropriate format specifier.

Found by Cppcheck

For all 12 patches.

Quoted 39 lines
---
 checkpolicy/policy_scan.l | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
checkpolicy/policy_scan.l +4 -5
index 129a8a2a..ef9f1899 100644
@@ -308,11 +308,11 @@ GLBLUB                            { return(GLBLUB); }
 int yyerror(const char *msg)
 {
        if (source_file[0])
-               fprintf(stderr, "%s:%ld:",
+               fprintf(stderr, "%s:%lu:",
                        source_file, source_lineno);
        else
                fprintf(stderr, "(unknown source)::");
-       fprintf(stderr, "ERROR '%s' at token '%s' on line %ld:\n%s\n%s\n",
+       fprintf(stderr, "ERROR '%s' at token '%s' on line %lu:\n%s\n%s\n",
                        msg,
                        yytext,
                        policydb_lineno,
@@ -327,11 +327,11 @@ int yywarn(const char *msg)
                return yyerror(msg);
 
        if (source_file[0])
-               fprintf(stderr, "%s:%ld:",
+               fprintf(stderr, "%s:%lu:",
                        source_file, source_lineno);
        else
                fprintf(stderr, "(unknown source)::");
-       fprintf(stderr, "WARNING '%s' at token '%s' on line %ld:\n%s\n%s\n",
+       fprintf(stderr, "WARNING '%s' at token '%s' on line %lu:\n%s\n%s\n",
                        msg,
                        yytext,
                        policydb_lineno,
--

2.33.1

#13 lore raw
Quoted 7 lines · James Carter
Quoted 4 lines · Christian Göttsche

The two variables policydb_lineno and source_lineno are both of the type unsigned long; use the appropriate format specifier.

Quoted 7 lines
Quoted 3 lines

Found by Cppcheck

For all 12 patches.

This has been merged.
Thanks,
Jim

Quoted 39 lines
Quoted 39 lines
---
 checkpolicy/policy_scan.l | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
checkpolicy/policy_scan.l +4 -5
index 129a8a2a..ef9f1899 100644
@@ -308,11 +308,11 @@ GLBLUB                            { return(GLBLUB); }
 int yyerror(const char *msg)
 {
        if (source_file[0])
-               fprintf(stderr, "%s:%ld:",
+               fprintf(stderr, "%s:%lu:",
                        source_file, source_lineno);
        else
                fprintf(stderr, "(unknown source)::");
-       fprintf(stderr, "ERROR '%s' at token '%s' on line %ld:\n%s\n%s\n",
+       fprintf(stderr, "ERROR '%s' at token '%s' on line %lu:\n%s\n%s\n",
                        msg,
                        yytext,
                        policydb_lineno,
@@ -327,11 +327,11 @@ int yywarn(const char *msg)
                return yyerror(msg);
 
        if (source_file[0])
-               fprintf(stderr, "%s:%ld:",
+               fprintf(stderr, "%s:%lu:",
                        source_file, source_lineno);
        else
                fprintf(stderr, "(unknown source)::");
-       fprintf(stderr, "WARNING '%s' at token '%s' on line %ld:\n%s\n%s\n",
+       fprintf(stderr, "WARNING '%s' at token '%s' on line %lu:\n%s\n%s\n",
                        msg,
                        yytext,
                        policydb_lineno,
--

2.33.1