Skip to contentSkip to conversation

Fix userspace audit compiler warnings

PATCH Applied Tyler Hicks 2 participants 8 messages Feb 9, 2013 Library code

lore
Share & tools
lore
#0 lore raw

cover Fix userspace audit compiler warnings

These patches fix the majority of compiler warnings seen when building audit on Ubuntu.

The audit codebase rarely checked the return value of asprintf(), which can cause problems if memory allocation fails. Most of the missing checks are in libauparse, which is probably fine but who knows what will use it in the future. Also, all of these warnings clutter up the build output and make it more difficult to see more serious warnings.

The second patch contains some other ignored return value warnings fixes, as well. These show up on Ubuntu when the -D_FORTIFY_SOURCE=2 and -Wunused-return flags are in use. They don't seem to show up on Fedora in the same situation, but I believe that they should, despite casting the function's return value to (void). Despite that, they looked to be areas in the code that should at least log a message or even prevent auditd from running in some situations.

The last patch cleans up two const warnings.

There are still a number of warnings in the zos-remote audispd plugin that I didn't get to and a few that look ok to me in the embedded libev code.

Tyler

#1 lore raw

PATCH 1/3 Don't ignore the return value of asprintf()

If an error occurs in asprintf(), the contents of the strp variable are undefined. asprintf()'s return value must be checked and the parameter passed into asprintf() should be set to NULL upon error.

---
 audisp/audispd.c                        |    1 +
 audisp/plugins/prelude/audisp-prelude.c |    4 +-
 auparse/ellist.c                        |    3 +-
 auparse/expression.c                    |   87 +++++-----
 auparse/interpret.c                     |  265 +++++++++++++++++++------------
 lib/audit_logging.c                     |    6 +-
 src/auditctl.c                          |   22 +--
 7 files changed, 232 insertions(+), 156 deletions(-)
audisp/audispd.c +1 -0
index 9216fb0..749128f 100644
@@ -613,6 +613,7 @@ static int event_loop(void)
 			len = asprintf(&v, "type=%s msg=%.*s\n", 
 				type, e->hdr.size, e->data);
 		if (len <= 0) {
+			v = NULL;
 			free(e); /* Either corrupted event or no memory */
 			continue;
 		}
audisp/plugins/prelude/audisp-prelude.c +3 -1
index 5cf1bef..0724b21 100644
@@ -967,7 +967,9 @@ static int add_execve_data(auparse_state_t *au, idmef_alert_t *alert)
 			int len2;
 			len2 = asprintf(&ptr, "%s=%s ", var,
 					auparse_interpret_field(au));
-			if (len2 > 0 && (len2 + len + 1) < sizeof(msg)) {
+			if (len2 < 0) {
+				ptr = NULL;
+			} else if (len2 > 0 && (len2 + len + 1) < sizeof(msg)) {
 				strcat(msg, ptr);
 				len += len2;
 			}
auparse/ellist.c +2 -1
index eafcfee..81d2d1e 100644
@@ -91,7 +91,8 @@ static char *escape(const char *tmp)
 		}
 		p++;
 	}
-	asprintf(&name, "\"%s\"", tmp);
+	if (asprintf(&name, "\"%s\"", tmp) < 0)
+		name = NULL;
 	return name;
 }
 
auparse/expression.c +45 -42

167 lines not on this page · raw message

auparse/interpret.c +165 -100

659 lines not on this page · raw message

lib/audit_logging.c +4 -2
index 4b59e30..85d1338 100644
@@ -133,12 +133,14 @@ char *audit_encode_nv_string(const char *name, const char *value,
 		char *tmp = malloc(2*vlen + 1);
 		if (tmp) {
 			audit_encode_value(tmp, value, vlen);
-			asprintf(&str, "%s=%s", name, tmp);
+			if (asprintf(&str, "%s=%s", name, tmp) < 0)
+				str = NULL;
 			free(tmp);
 		} else
 			str = NULL;
 	} else
-		asprintf(&str, "%s=\"%s\"", name, value ? value : "?");
+		if (asprintf(&str, "%s=\"%s\"", name, value ? value : "?") < 0)
+			str = NULL;
 	return str;
 }
 
src/auditctl.c +12 -10
index 62a8c51..b062e8b 100644
@@ -930,16 +930,16 @@ static int setopt(int count, int lineno, char *vars[])
 		flags = del & AUDIT_FILTER_MASK;
 
 	/* Build the command */
-	asprintf(&cmd, "key=%s", key);
-	if (cmd) {
+	if (asprintf(&cmd, "key=%s", key) < 0) {
+		cmd = NULL;
+		fprintf(stderr, "Out of memory adding key\n");
+		retval = -1;
+	} else {
 		/* Add this to the rule */
 		int ret = audit_rule_fieldpair_data(&rule_new, cmd, flags);
 		if (ret < 0)
 			retval = -1;
 		free(cmd);
-	} else {
-		fprintf(stderr, "Out of memory adding key\n");
-		retval = -1;
 	}
     }
     if (retval == -1 && errno == ECONNREFUSED)
@@ -1351,8 +1351,9 @@ int key_match(struct audit_reply *rep)
 		int field = rep->ruledata->fields[i] & ~AUDIT_OPERATORS;
 		if (field == AUDIT_FILTERKEY) {
 			char *keyptr;
-			asprintf(&keyptr, "%.*s", rep->ruledata->values[i],
-				&rep->ruledata->buf[boffset]);
+			if (asprintf(&keyptr, "%.*s", rep->ruledata->values[i],
+				     &rep->ruledata->buf[boffset]) < 0)
+				keyptr = NULL;
 			if (strstr(keyptr, key)) {
 				free(keyptr);
 				return 1;
@@ -1467,9 +1468,10 @@ static int audit_print_reply(struct audit_reply *rep)
 						    rep->ruledata->values[i];
 					} else if (field == AUDIT_FILTERKEY) {
 						char *rkey, *ptr;
-						asprintf(&rkey, "%.*s",
-						rep->ruledata->values[i],
-						&rep->ruledata->buf[boffset]);
+						if (asprintf(&rkey, "%.*s",
+						      rep->ruledata->values[i],
+						      &rep->ruledata->buf[boffset]) < 0)
+							rkey = NULL;
 						boffset +=
 						    rep->ruledata->values[i];
 						ptr = strtok(rkey, key_sep);
Signature · 1 line
1.7.10.4
Applied
#2 lore raw

Re: [PATCH 1/3] Don't ignore the return value of asprintf()

Quoted 3 lines · Tyler Hicks

If an error occurs in asprintf(), the contents of the strp variable are undefined. asprintf()'s return value must be checked and the parameter passed into asprintf() should be set to NULL upon error.

Applied. I think there are a couple places where it will obviously segfault in auditctl.c. But the patch does clean up a bunch of warnings.

-Steve

#3 lore raw

PATCH 2/3 Fix Wunused-return warnings

In reply to Tyler Hicks

When building with -D_FORTIFY_SOURCE=2 and -W-unused-return, there are a number of warnings caused by return values of functions marked with the warn_unused_result attribute being ignored. The audit codebase makes an attempt to suppress these warnings by casting the return value to void, but that does not work when D_FORITY_SOURCE is in use.

Here's an explanation of how this patch fixes the warnings and how the potential error conditions are handled:

Errors writing to the auditd pid file should be logged since errors opening the pid file are logged. These write() errors aren't treated as fatal.

Problems adjusting auditd's out of memory score should be logged, if simply to catch a change to the kernel interface. These errors aren't treated as fatal.

Auditd refuses to start when nice() fails during initialization, so it should take disk_error_action whenever nice() fails during a reconfigure.

Failure to chdir("/") while daemonizing should be logged and treated as fatal since errors while redirecting stdin, stdout, and stderr are logged and considered fatal.

All nice() return values are handled sufficiently by relying on errno. However, they still throw warnings when D_FORTIFY_SOURCE is used. This patch quiets those warnings by capturing the return value and using it and errno to determine if nice() failed.

Failure to adjust audit log file owner (fchown) and permissions (fchmod) are logged and considered fatal when opening the log file for the first time. They are not treated as fatal when the operations fail on during log rotation since we made sure that they file owner and permissions were correct when originally opening the log file.

---
 audisp/audispd.c   |    6 ++++--
 src/auditd-event.c |   45 +++++++++++++++++++++++++++++++++++++--------
 src/auditd.c       |   52 +++++++++++++++++++++++++++++++++-------------------
 src/autrace.c      |    6 +++++-
 4 files changed, 79 insertions(+), 30 deletions(-)
audisp/audispd.c +4 -2
index 749128f..0ef4b8e 100644
@@ -369,9 +369,11 @@ int main(int argc, char *argv[])
 
 	/* Now boost priority to make sure we are getting time slices */
 	if (daemon_config.priority_boost != 0) {
+		int rc;
+
 		errno = 0;
-		(void) nice((int)-daemon_config.priority_boost);
-		if (errno) {
+		rc = nice((int)-daemon_config.priority_boost);
+		if (rc == -1 && errno) {
 			syslog(LOG_ERR, "Cannot change priority (%s)",
 					strerror(errno));
 			/* Stay alive as this is better than stopping */
src/auditd-event.c +37 -8

64 lines not on this page · raw message

src/auditd.c +33 -19

84 lines not on this page · raw message

src/autrace.c +5 -1
index e124660..47c6c4f 100644
@@ -245,7 +245,11 @@ int main(int argc, char *argv[])
 					exit(1);
 				}
 				sleep(1);
-				(void)write(fd[1],"1", 1);
+				if (write(fd[1],"1", 1) != 1) {
+					kill(pid,SIGTERM);
+					(void)delete_all_rules(audit_fd);
+					exit(1);
+				}
 				waitpid(pid, NULL, 0);
 				close(fd[1]);
 				puts("Cleaning up...");
Signature · 1 line
1.7.10.4
#4 lore raw

Re: [PATCH 2/3] Fix Wunused-return warnings

Quoted 12 lines · Tyler Hicks

When building with -D_FORTIFY_SOURCE=2 and -W-unused-return, there are a number of warnings caused by return values of functions marked with the warn_unused_result attribute being ignored. The audit codebase makes an attempt to suppress these warnings by casting the return value to void, but that does not work when D_FORITY_SOURCE is in use.

Here's an explanation of how this patch fixes the warnings and how the potential error conditions are handled:

Errors writing to the auditd pid file should be logged since errors opening the pid file are logged. These write() errors aren't treated as fatal.

Quoted 6 lines

Problems adjusting auditd's out of memory score should be logged, if simply to catch a change to the kernel interface. These errors aren't treated as fatal.

Auditd refuses to start when nice() fails during initialization, so it should take disk_error_action whenever nice() fails during a reconfigure.

During a reconfigure, I would not consider this fatal. Its better to stay running than exit. I'll adjust the patch.

-Steve

Quoted 14 lines

Failure to chdir("/") while daemonizing should be logged and treated as fatal since errors while redirecting stdin, stdout, and stderr are logged and considered fatal.

All nice() return values are handled sufficiently by relying on errno. However, they still throw warnings when D_FORTIFY_SOURCE is used. This patch quiets those warnings by capturing the return value and using it and errno to determine if nice() failed.

Failure to adjust audit log file owner (fchown) and permissions (fchmod) are logged and considered fatal when opening the log file for the first time. They are not treated as fatal when the operations fail on during log rotation since we made sure that they file owner and permissions were correct when originally opening the log file.

#5 lore raw

Re: [PATCH 2/3] Fix Wunused-return warnings

Quoted 17 lines · Steve Grubb
Quoted 16 lines · Tyler Hicks

When building with -D_FORTIFY_SOURCE=2 and -W-unused-return, there are a number of warnings caused by return values of functions marked with the warn_unused_result attribute being ignored. The audit codebase makes an attempt to suppress these warnings by casting the return value to void, but that does not work when D_FORITY_SOURCE is in use.

Here's an explanation of how this patch fixes the warnings and how the potential error conditions are handled:

Errors writing to the auditd pid file should be logged since errors opening the pid file are logged. These write() errors aren't treated as fatal.

Problems adjusting auditd's out of memory score should be logged, if simply to catch a change to the kernel interface. These errors aren't treated as fatal.

Quoted 5 lines
Quoted 2 lines

Auditd refuses to start when nice() fails during initialization, so it should take disk_error_action whenever nice() fails during a reconfigure.

During a reconfigure, I would not consider this fatal. Its better to stay running than exit. I'll adjust the patch.

That's probably the best way to go.

Thanks for getting these applied.

Tyler

Quoted 17 lines

-Steve

Quoted 14 lines

Failure to chdir("/") while daemonizing should be logged and treated as fatal since errors while redirecting stdin, stdout, and stderr are logged and considered fatal.

All nice() return values are handled sufficiently by relying on errno. However, they still throw warnings when D_FORTIFY_SOURCE is used. This patch quiets those warnings by capturing the return value and using it and errno to determine if nice() failed.

Failure to adjust audit log file owner (fchown) and permissions (fchmod) are logged and considered fatal when opening the log file for the first time. They are not treated as fatal when the operations fail on during log rotation since we made sure that they file owner and permissions were correct when originally opening the log file.

#6 lore raw

PATCH 3/3 Fix discards 'const' qualifier from pointer target type warnings

In reply to Tyler Hicks

The event_note_list pointer is reassigned and its members are also reassigned. It should not be declared with the const qualifier.

The ptr variable, in unescape(), cannot be used to modify a string since it is initialized to the const char *buf input parameter. Rather than modifying buf, we can use ptr as a placeholder and use strndup() to allocate str. Later in the function a new, non-const pointer is used to modify str. These changes allow unescape() to still take a const char * as its input parameter.

---
 src/aureport-options.c |    2 +-
 src/ausearch-common.h  |    2 +-
 src/ausearch-lookup.c  |   16 +++++++---------
 src/ausearch-options.c |    2 +-
 4 files changed, 10 insertions(+), 12 deletions(-)
src/aureport-options.c +1 -1
index 72a1d15..2af8714 100644
@@ -42,7 +42,7 @@ int force_logs = 0;
 
 /* These are for compatibility with parser */
 unsigned int event_id = -1;
-const slist *event_node_list = NULL;
+slist *event_node_list = NULL;
 const char *event_key = NULL;
 const char *event_filename = NULL;
 const char *event_exe = NULL;
src/ausearch-common.h +1 -1
index 2ee1f33..95c9980 100644
@@ -35,7 +35,7 @@ extern gid_t event_gid, event_egid;
 extern pid_t event_pid;
 extern int event_exact_match;
 extern uid_t event_uid, event_euid, event_loginuid;
-const slist *event_node_list;
+slist *event_node_list;
 extern const char *event_comm;
 extern const char *event_filename;
 extern const char *event_hostname;
src/ausearch-lookup.c +7 -9
index cb13003..0c8f4bf 100644
@@ -318,7 +318,8 @@ static unsigned char x2c(unsigned char *buf)
 char *unescape(const char *buf)
 {
 	int len, i;
-	char saved, *ptr = buf, *str;
+	char *str, *strptr;
+	const char *ptr = buf;
 
 	/* Find the end of the name */
 	if (*ptr == '(') {
@@ -331,10 +332,7 @@ char *unescape(const char *buf)
 		while (isxdigit(*ptr))
 			ptr++;
 	}
-	saved = *ptr;
-	*ptr = 0;
-	str = strdup(buf);
-	*ptr = saved;
+	str = strndup(buf, ptr - buf);
 
 	if (*buf == '(')
 		return str;
@@ -347,12 +345,12 @@ char *unescape(const char *buf)
 		free(str);
 		return NULL;
 	}
-	ptr = str;
+	strptr = str;
 	for (i=0; i<len; i+=2) {
-		*ptr = x2c((unsigned char *)&str[i]);
-		ptr++;
+		*strptr = x2c((unsigned char *)&str[i]);
+		strptr++;
 	}
-	*ptr = 0;
+	*strptr = 0;
 	return str;
 }
 
src/ausearch-options.c +1 -1
index 769d8fa..06a85bb 100644
@@ -68,7 +68,7 @@ const char *event_vmname = NULL;
 report_t report_format = RPT_DEFAULT;
 ilist *event_type;
 
-const slist *event_node_list = NULL;
+slist *event_node_list = NULL;
 
 struct nv_pair {
     int        value;
Signature · 1 line
1.7.10.4
Applied
#7 lore raw

Re: [PATCH 3/3] Fix discards 'const' qualifier from pointer target type warnings

Quoted 11 lines · Tyler Hicks

The event_note_list pointer is reassigned and its members are also reassigned. It should not be declared with the const qualifier.

The ptr variable, in unescape(), cannot be used to modify a string since it is initialized to the const char *buf input parameter. Rather than modifying buf, we can use ptr as a placeholder and use strndup() to allocate str. Later in the function a new, non-const pointer is used to modify str. These changes allow unescape() to still take a const char * as its input parameter.

Applied. Thanks for all 3 patches.

-Steve