From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from goalie.tycho.ncsc.mil (goalie [144.51.242.250]) by tarius.tycho.ncsc.mil (8.14.4/8.14.4) with ESMTP id s8BL5EH1004736 for ; Thu, 11 Sep 2014 17:05:14 -0400 Received: by mail-wg0-f41.google.com with SMTP id k14so5789061wgh.12 for ; Thu, 11 Sep 2014 14:05:19 -0700 (PDT) Received: from eversmanni.localdomain (193.147.102.84.rev.sfr.net. [84.102.147.193]) by mx.google.com with ESMTPSA id pk9sm2415508wjb.16.2014.09.11.14.05.17 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 11 Sep 2014 14:05:18 -0700 (PDT) From: Nicolas Iooss To: selinux@tycho.nsa.gov Subject: [PATCH] libsepol/cil: fix compilation with -Wformat-security Date: Thu, 11 Sep 2014 23:05:10 +0200 Message-Id: <1410469510-5089-1-git-send-email-nicolas.iooss@m4x.org> List-Id: "Security-Enhanced Linux \(SELinux\) mailing list" List-Post: List-Help: Once a printf format attribute is added to cil_log, compiling with gcc -Wformat -Wformat-security -Werror fails with two kind of errors: * format '%s' expects argument of type 'char *', but argument 3 has type 'void *' * format '%n' expects argument of type 'int *', but argument 4 has type 'uint32_t' The second one is quite surprising and it seems %n has been used instead of %u several times in cil_resolve_ast.c. Fix these printf-format-check errors. --- libsepol/cil/include/cil/cil.h | 4 ++++ libsepol/cil/src/cil_build_ast.c | 9 +++++---- libsepol/cil/src/cil_log.h | 4 +--- libsepol/cil/src/cil_resolve_ast.c | 12 ++++++------ libsepol/cil/src/cil_verify.c | 10 +++++----- 5 files changed, 21 insertions(+), 18 deletions(-) diff --git a/libsepol/cil/include/cil/cil.h b/libsepol/cil/include/cil/cil.h index 00047a48cbea..902ca3e87b01 100644 --- a/libsepol/cil/include/cil/cil.h +++ b/libsepol/cil/include/cil/cil.h @@ -58,6 +58,10 @@ enum cil_log_level { }; extern void cil_set_log_level(enum cil_log_level lvl); extern void cil_set_log_handler(void (*handler)(int lvl, char *msg)); + +#ifdef __GNUC__ +__attribute__ ((format(printf, 2, 3))) +#endif extern void cil_log(enum cil_log_level lvl, const char *msg, ...); extern void cil_set_malloc_error_handler(void (*handler)(void)); diff --git a/libsepol/cil/src/cil_build_ast.c b/libsepol/cil/src/cil_build_ast.c index 945b0a8bf306..3a38559b7c0d 100644 --- a/libsepol/cil/src/cil_build_ast.c +++ b/libsepol/cil/src/cil_build_ast.c @@ -2271,7 +2271,7 @@ static int __cil_fill_expr(struct cil_tree_node *current, enum cil_flavor flavor if (current->cl_head == NULL) { enum cil_flavor op = __cil_get_expr_operator_flavor(current->data); if (op != CIL_NONE) { - cil_log(CIL_ERR,"Operator (%s) not in an expression\n", current->data); + cil_log(CIL_ERR, "Operator (%s) not in an expression\n", (char*)current->data); goto exit; } cil_list_append(expr, CIL_STRING, current->data); @@ -2378,7 +2378,7 @@ static int __cil_fill_constraint_leaf_expr(struct cil_tree_node *current, enum c leaf_expr_flavor = CIL_LEVEL; break; default: - cil_log(CIL_ERR,"Invalid left operand (%s)\n",current->next->data); + cil_log(CIL_ERR, "Invalid left operand (%s)\n", (char*)current->next->data); goto exit; } @@ -2705,7 +2705,7 @@ int cil_gen_condblock(struct cil_db *db, struct cil_tree_node *parse_current, st exit: cil_log(CIL_ERR, "Bad %s condition declaration at line %d of %s\n", - parse_current->data, parse_current->line, parse_current->path); + (char*)parse_current->data, parse_current->line, parse_current->path); cil_destroy_condblock(cb); return rc; } @@ -2765,7 +2765,8 @@ int cil_gen_alias(struct cil_db *db, struct cil_tree_node *parse_current, struct return SEPOL_OK; exit: - cil_log(CIL_ERR, "Bad %s declaration at line %d of %s\n", parse_current->data, parse_current->line, parse_current->path); + cil_log(CIL_ERR, "Bad %s declaration at line %d of %s\n", + (char*)parse_current->data, parse_current->line, parse_current->path); cil_destroy_alias(alias); cil_clear_node(ast_node); return rc; diff --git a/libsepol/cil/src/cil_log.h b/libsepol/cil/src/cil_log.h index 46646c5b329b..9c2ff2e83ebc 100644 --- a/libsepol/cil/src/cil_log.h +++ b/libsepol/cil/src/cil_log.h @@ -34,8 +34,6 @@ #define MAX_LOG_SIZE 512 - - -void cil_log(enum cil_log_level lvl, const char *msg, ...); +__attribute__ ((format(printf, 2, 3))) void cil_log(enum cil_log_level lvl, const char *msg, ...); #endif // CIL_LOG_H_ diff --git a/libsepol/cil/src/cil_resolve_ast.c b/libsepol/cil/src/cil_resolve_ast.c index 3b79e90f478b..a36b23808581 100644 --- a/libsepol/cil/src/cil_resolve_ast.c +++ b/libsepol/cil/src/cil_resolve_ast.c @@ -128,7 +128,7 @@ static int __cil_resolve_perms(symtab_t *class_symtab, symtab_t *common_symtab, } } if (rc != SEPOL_OK) { - cil_log(CIL_ERR, "Failed to resolve permission %s\n", curr->data); + cil_log(CIL_ERR, "Failed to resolve permission %s\n", (char*)curr->data); goto exit; } cil_list_append(*perm_datums, CIL_DATUM, perm_datum); @@ -2210,7 +2210,7 @@ int cil_resolve_bounds(struct cil_tree_node *current, void *extra_args, enum cil if (user->bounds != NULL) { struct cil_tree_node *node = user->bounds->datum.nodes->head->data; - cil_log(CIL_ERR, "User %s already bound by parent at line %n of %s\n", bounds->child_str, node->line, node->path); + cil_log(CIL_ERR, "User %s already bound by parent at line %u of %s\n", bounds->child_str, node->line, node->path); rc = SEPOL_ERR; goto exit; } @@ -2223,7 +2223,7 @@ int cil_resolve_bounds(struct cil_tree_node *current, void *extra_args, enum cil if (role->bounds != NULL) { struct cil_tree_node *node = role->bounds->datum.nodes->head->data; - cil_log(CIL_ERR, "Role %s already bound by parent at line %n of %s\n", bounds->child_str, node->line, node->path); + cil_log(CIL_ERR, "Role %s already bound by parent at line %u of %s\n", bounds->child_str, node->line, node->path); rc = SEPOL_ERR; goto exit; } @@ -2237,8 +2237,8 @@ int cil_resolve_bounds(struct cil_tree_node *current, void *extra_args, enum cil if (type->bounds != NULL) { node = ((struct cil_symtab_datum *)type->bounds)->nodes->head->data; - cil_log(CIL_ERR, "Type %s already bound by parent at line %n of %s\n", bounds->child_str, node->line, node->path); - cil_log(CIL_ERR, "Now being bound to parent %s at line %n of %s\n", bounds->parent_str, current->line, current->path); + cil_log(CIL_ERR, "Type %s already bound by parent at line %u of %s\n", bounds->child_str, node->line, node->path); + cil_log(CIL_ERR, "Now being bound to parent %s at line %u of %s\n", bounds->parent_str, current->line, current->path); rc = SEPOL_ERR; goto exit; } @@ -2267,7 +2267,7 @@ int cil_resolve_bounds(struct cil_tree_node *current, void *extra_args, enum cil return SEPOL_OK; exit: - cil_log(CIL_ERR, "Bad bounds statement at line %n of %s\n", current->line, current->path); + cil_log(CIL_ERR, "Bad bounds statement at line %u of %s\n", current->line, current->path); return rc; } diff --git a/libsepol/cil/src/cil_verify.c b/libsepol/cil/src/cil_verify.c index 995496c5e757..a1576e761f1f 100644 --- a/libsepol/cil/src/cil_verify.c +++ b/libsepol/cil/src/cil_verify.c @@ -166,13 +166,13 @@ int cil_verify_expr_syntax(struct cil_tree_node *current, enum cil_flavor op, en case CIL_EQ: case CIL_NEQ: if (expr_flavor != CIL_BOOL && expr_flavor != CIL_TUNABLE ) { - cil_log(CIL_ERR,"Invalid operator (%s) for set expression\n", current->data); + cil_log(CIL_ERR,"Invalid operator (%s) for set expression\n", (char*)current->data); goto exit; } break; case CIL_ALL: if (expr_flavor == CIL_BOOL || expr_flavor == CIL_TUNABLE) { - cil_log(CIL_ERR,"Invalid operator (%s) for boolean or tunable expression\n", current->data); + cil_log(CIL_ERR,"Invalid operator (%s) for boolean or tunable expression\n", (char*)current->data); goto exit; } syntax[1] = CIL_SYN_END; @@ -180,7 +180,7 @@ int cil_verify_expr_syntax(struct cil_tree_node *current, enum cil_flavor op, en break; case CIL_RANGE: if (expr_flavor != CIL_CAT) { - cil_log(CIL_ERR,"Operator (%s) only valid for catset expression\n", current->data); + cil_log(CIL_ERR,"Operator (%s) only valid for catset expression\n", (char*)current->data); goto exit; } syntax[1] = CIL_SYN_STRING; @@ -192,7 +192,7 @@ int cil_verify_expr_syntax(struct cil_tree_node *current, enum cil_flavor op, en syntax_len = 2; break; default: - cil_log(CIL_ERR,"Unexpected value (%s) for expression operator\n", current->data); + cil_log(CIL_ERR,"Unexpected value (%s) for expression operator\n", (char*)current->data); goto exit; } @@ -298,7 +298,7 @@ int cil_verify_constraint_expr_syntax(struct cil_tree_node *current, enum cil_fl syntax[2] = CIL_SYN_STRING; break; default: - cil_log(CIL_ERR,"Invalid operator (%s) for constraint expression\n",current->data); + cil_log(CIL_ERR, "Invalid operator (%s) for constraint expression\n", (char*)current->data); goto exit; } -- 2.1.0