From: Michal Kubecek <mkubecek@suse.com>
Subject: Fix non working autrace
References: https://fedorahosted.org/audit/changeset?old_path=%2F&old=145&new_path=%2F&new=146
References: bnc#714083, bnc#478007
Signed-off-by: Tony Jones <tonyj@suse.de>

Selected backport of changes from svn revision 146, which are marked as code
cleanup only but obviously fixed bug.

--- audit-1.7.7.orig/audisp/audispd-builtins.c	2008-09-17 21:57:13.000000000 +0200
+++ audit-1.7.7/audisp/audispd-builtins.c	2011-08-25 08:23:50.000000000 +0200
@@ -171,8 +171,10 @@ static void init_af_unix(const plugin_co
 					closedir(d);
 					unlink(conf->args[i]);
 					if (create_af_unix_socket(
-							conf->args[i], mode)<0)
+						    conf->args[i], mode)<0) {
+						free(dir);
 						return;
+					}
 					path = strdup(conf->args[i]);
 					bad = 0;
 				} else
@@ -221,7 +223,7 @@ void send_af_unix_binary(event_t *e)
 
 		vec[0].iov_base = &e->hdr;
 		vec[0].iov_len = sizeof(struct audit_dispatcher_header);
-		vec[1].iov_base = &e->data;
+		vec[1].iov_base = e->data;
 		vec[1].iov_len = MAX_AUDIT_MESSAGE_LENGTH;
 		do {
 			rc = writev(conn, vec, 2);
--- a/audisp/audispd.c	2008-09-17 21:57:13.000000000 +0200
+++ b/audisp/audispd.c	2011-08-25 08:31:06.000000000 +0200
@@ -618,7 +618,7 @@ static int event_loop(void)
 					vec[0].iov_len = sizeof(struct
 						audit_dispatcher_header);
 
-					vec[1].iov_base = &e->data;
+					vec[1].iov_base = e->data;
 					vec[1].iov_len =
 						MAX_AUDIT_MESSAGE_LENGTH;
 					do {
--- a/lib/audit_logging.c	2008-09-17 21:57:13.000000000 +0200
+++ b/lib/audit_logging.c	2011-08-25 08:23:50.000000000 +0200
@@ -85,7 +85,9 @@ int audit_value_needs_encoding(const cha
 	int i;
 
 	for (i=0; i<size; i++) {
-		if (str[i] == '"' || str[i] < 0x21 || str[i] > 0x7f)
+		// we don't test for > 0x7f because str[] is signed
+		// and str[i] < 0x21 covers it.
+		if (str[i] == '"' || str[i] < 0x21)
 			return 1;
 	}
 	return 0;
--- a/lib/deprecated.c	2008-09-17 21:57:13.000000000 +0200
+++ b/lib/deprecated.c	2011-08-25 08:23:50.000000000 +0200
@@ -405,7 +405,7 @@ int audit_rule_fieldpair(struct audit_ru
 			audit_archadded = 1;
 			break;
 		case AUDIT_FILETYPE:
-			if (flags != AUDIT_FILTER_EXIT || flags != AUDIT_FILTER_ENTRY)
+			if (flags != AUDIT_FILTER_EXIT && flags != AUDIT_FILTER_ENTRY)
 				return -17;
 			rule->values[rule->field_count] =
 				audit_name_to_ftype(v);
@@ -440,7 +440,7 @@ int audit_rule_fieldpair(struct audit_ru
 					return -13;
 			}
 			if (field == AUDIT_PPID && (flags != AUDIT_FILTER_EXIT
-				|| flags != AUDIT_FILTER_ENTRY))
+				&& flags != AUDIT_FILTER_ENTRY))
 				return -17;
 			
 			if (flags == AUDIT_FILTER_EXCLUDE)
--- a/lib/gen_tables.c	2008-09-17 21:57:13.000000000 +0200
+++ b/lib/gen_tables.c	2011-08-25 08:23:50.000000000 +0200
@@ -343,6 +343,7 @@ output_i2s_transtab(const char *prefix)
 	       "#define %s_NUM_ENTRIES "
 	       "(sizeof(%s_table) / sizeof(*%s_table))\n", uc_prefix, prefix,
 	       prefix);
+	free(uc_prefix);
 }
 
 int
--- a/lib/libaudit.c	2008-09-17 21:57:13.000000000 +0200
+++ b/lib/libaudit.c	2011-08-25 08:23:50.000000000 +0200
@@ -665,6 +665,7 @@ int audit_make_equivalent(int fd, const
 		audit_msg(audit_priority(errno),
 			"Error sending make_equivalent command (%s)",
 			strerror(-rc));
+	free(cmd);
 	return rc;
 }
 
@@ -846,7 +847,6 @@ int audit_rule_fieldpair_data(struct aud
 		case AUDIT_SUID:
 		case AUDIT_FSUID:
 		case AUDIT_LOGINUID:
-			vlen = strlen(v);
 			if (isdigit((char)*(v))) 
 				rule->values[rule->field_count] = 
 					strtol(v, NULL, 0);
@@ -919,6 +919,9 @@ int audit_rule_fieldpair_data(struct aud
 			 * but exit */
 			if (flags != AUDIT_FILTER_EXIT)
 				return -7;
+			if (field == AUDIT_WATCH || field == AUDIT_DIR)
+				audit_permadded = 1;
+
 			/* fallthrough */
 		case AUDIT_SUBJ_USER:
 		case AUDIT_SUBJ_ROLE:
@@ -947,9 +950,6 @@ int audit_rule_fieldpair_data(struct aud
 			}
 			strncpy(&rule->buf[offset], v, vlen);
 
-			if(flags && (AUDIT_WATCH || AUDIT_DIR))
-				audit_permadded = 1;
-
 			break;
 		case AUDIT_ARCH:
 			if (audit_syscalladded) 
@@ -1075,11 +1075,11 @@ int audit_rule_fieldpair_data(struct aud
 			}
 			break;
 		case AUDIT_FILETYPE:
-			if (flags != AUDIT_FILTER_EXIT || flags != AUDIT_FILTER_ENTRY)
+			if (flags != AUDIT_FILTER_EXIT && flags != AUDIT_FILTER_ENTRY)
 				return -17;
 			rule->values[rule->field_count] = 
 				audit_name_to_ftype(v);
-			if (rule->values[rule->field_count] < 0) {
+			if ((int)rule->values[rule->field_count] < 0) {
 				return -16;
 			}
 			break;
@@ -1094,8 +1094,8 @@ int audit_rule_fieldpair_data(struct aud
 					return -13;
 			}
 
-			if (field == AUDIT_PPID && (flags != AUDIT_FILTER_EXIT 
-				|| flags != AUDIT_FILTER_ENTRY))
+			if (field == AUDIT_PPID && flags != AUDIT_FILTER_EXIT 
+				&& flags != AUDIT_FILTER_ENTRY)
 				return -17;
 			
 			if (flags == AUDIT_FILTER_EXCLUDE)
--- a/src/auditd-listen.c	2008-09-17 21:57:13.000000000 +0200
+++ b/src/auditd-listen.c	2011-08-25 08:23:50.000000000 +0200
@@ -599,7 +599,7 @@ more_messages:
 		}
 	} else
 #endif
-	if (AUDIT_RMW_IS_MAGIC (io->buffer, io->bufptr+r)) {
+	if (AUDIT_RMW_IS_MAGIC (io->buffer, (io->bufptr+r))) {
 		uint32_t type, len, seq;
 		int hver, mver;
 		unsigned char *header = (unsigned char *)io->buffer;
