Skip to contentSkip to conversation

Cleanup code in and around audit_expand()

RFC PATCH In review Paul Moore 2 participants 8 messages Jul 24 – Jul 27 Audit

lore
Share & tools
lore

Paul Moore

author maintainer
#0 lore raw

cover Cleanup code in and around audit_expand()

The recent audit match overflow patches caused me to take a closer look at audit_expand() and how it is used, which unsurprisingly revealed a number of things that we could do a bit better. This patchset is the result of that and aims to cleanup some of the ugliness while also making the code a bit more robust.

Currently a RFC pending some closer self-review, but the patchset compiles, boots, and passes the audit-testsuite and selinux-testsuite cleanly.

--
Paul Moore (5):
audit: only call audit_expand() once in audit_log_vformat()
audit: convert audit_expand() to operate in multiples of
AUDIT_BUFSIZ
audit: move from a size_t buffer length to an int buffer length
audit: add a missing function definition when !CONFIG_AUDIT
audit: drop BUG_ON() from audit_log_XXX()

 include/linux/audit.h |   22 +++++-----
 kernel/audit.c        |   89 +++++++++++++++++++++---------------------
 2 files changed, 57 insertions(+), 54 deletions(-)
author maintainer
#1 lore raw

RFC PATCH 1/5 audit: only call audit_expand() once in audit_log_vformat()

The audit_expand() function ends up calling into pskb_expand_head() which does a non-trivial amount of work. Limiting the number of times we call audit_expand() is a positive from a performance perspective.

We also know that audit buffers are originally allocated with a size of AUDIT_BUFSIZ (1024 bytes at the time of this patch), which makes it very unlikely that an audit buffer would ever be fully depleted and in need of expansion.

With all of this in mind, don't call audit_expand() in audit_log_vformat() until we know how much buffer space we need, cleaning up the code and potentially dropping one audit_expand() call from the audit hot path.

---
 kernel/audit.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
kernel/audit.c +3 -8
index 9412af9144bc..ec88dd10eb79 100644
@@ -2023,11 +2023,6 @@ void audit_log_vformat(struct audit_buffer *ab, const char *fmt, va_list args)
 	BUG_ON(!ab->skb);
 	skb = ab->skb;
 	avail = skb_tailroom(skb);
-	if (avail == 0) {
-		avail = audit_expand(ab, AUDIT_BUFSIZ);
-		if (!avail)
-			goto out;
-	}
 	va_copy(args2, args);
 	len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args);
 	if (len >= avail) {
@@ -2037,14 +2032,14 @@ void audit_log_vformat(struct audit_buffer *ab, const char *fmt, va_list args)
 		avail = audit_expand(ab,
 			max_t(unsigned int, AUDIT_BUFSIZ, 1+len-avail));
 		if (!avail)
-			goto out_va_end;
+			goto out;
 		len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args2);
 	}
 	if (len > 0)
 		skb_put(skb, len);
-out_va_end:
-	va_end(args2);
+
 out:
+	va_end(args2);
 	return;
 }
 
Signature · 1 line
2.55.0
author maintainer
#2 lore raw

RFC PATCH 2/5 audit: convert audit_expand() to operate in multiples of AUDIT_BUFSIZ

In reply to Paul Moore

All of the audit_expand() callers grow the audit buffer in multiples of AUDIT_BUFSIZ so we're going to simplify things by allowing callers to call audit_expand() with arbitrary lengths and move the AUDIT_BUFSIZ buffer calculations into audit_expand().

We also take this opportunity to normalize how/when audit_expand() is called across audit_log_vformat(), audit_log_n_hex(), and audit_log_n_string(). When the requested buffer size is at least as large as the remaining buffer size, expand the audit buffer such that the request buffer and an extra byte can fit into the newly expanded audit buffer. This should support callers regardless of if they specify a NUL byte at the end of the string being logged, as well as any audit buffers that might be empty (not curently possible, but who knows).

---
 kernel/audit.c | 40 ++++++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 18 deletions(-)
kernel/audit.c +22 -18
index ec88dd10eb79..520244544907 100644
@@ -1983,25 +1983,35 @@ struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
 /**
  * audit_expand - expand skb in the audit buffer
  * @ab: audit_buffer
- * @extra: space to add at tail of the skb
+ * @extra: space to add to the end of the buffer
  *
  * Returns 0 (no space) on failed expansion, or available space if
  * successful.
  */
 static inline int audit_expand(struct audit_buffer *ab, int extra)
 {
+	int old, new;
+	int expand_bufs, expand_bytes;
 	struct sk_buff *skb = ab->skb;
-	int oldtail = skb_tailroom(skb);
-	int ret = pskb_expand_head(skb, 0, extra, ab->gfp_mask);
-	int newtail = skb_tailroom(skb);
 
-	if (ret < 0) {
-		audit_log_lost("out of memory in audit_expand");
+	old = skb_tailroom(skb);
+	if (extra <= 0)
+		return old;
+
+	/* expand in multiples of AUDIT_BUFSIZ */
+	expand_bufs = (extra / AUDIT_BUFSIZ) + 1;
+	if (check_mul_overflow(expand_bufs, AUDIT_BUFSIZ, &expand_bytes)) {
+		audit_log_lost("size overflow in audit_expand()");
 		return 0;
 	}
+	if (pskb_expand_head(skb, 0, expand_bytes, ab->gfp_mask) < 0) {
+		audit_log_lost("out of memory in audit_expand()");
+		return 0;
+	}
+	new = skb_tailroom(skb);
+	skb->truesize += new - old;
 
-	skb->truesize += newtail - oldtail;
-	return newtail;
+	return new;
 }
 
 /*
@@ -2026,11 +2036,7 @@ void audit_log_vformat(struct audit_buffer *ab, const char *fmt, va_list args)
 	va_copy(args2, args);
 	len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args);
 	if (len >= avail) {
-		/* The printk buffer is 1024 bytes long, so if we get
-		 * here and AUDIT_BUFSIZ is at least 1024, then we can
-		 * log everything that printk could have logged. */
-		avail = audit_expand(ab,
-			max_t(unsigned int, AUDIT_BUFSIZ, 1+len-avail));
+		avail = audit_expand(ab, len - avail + 1);
 		if (!avail)
 			goto out;
 		len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args2);
@@ -2094,9 +2100,7 @@ void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
 	}
 
 	if (new_len >= avail) {
-		/* Round the buffer request up to the next multiple */
-		new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
-		avail = audit_expand(ab, new_len);
+		avail = audit_expand(ab, new_len - avail + 1);
 		if (!avail)
 			return;
 	}
@@ -2133,8 +2137,8 @@ void audit_log_n_string(struct audit_buffer *ab, const char *string,
 		return;
 	}
 
-	if (new_len > avail) {
-		avail = audit_expand(ab, new_len);
+	if (new_len >= avail) {
+		avail = audit_expand(ab, new_len - avail + 1);
 		if (!avail)
 			return;
 	}
Signature · 1 line
2.55.0
author maintainer
#3 lore raw

RFC PATCH 3/5 audit: move from a size_t buffer length to an int buffer length

In reply to Paul Moore

Currently the audit buffer is simply a sk_buff which is limited to an unsigned int in length and we rely on pskb_expand_head() to grow the buffer which is limited to a signed int for incremental growth. Since a 32-bit signed integer length value should be far more than we would ever need (or want!) for an audit buffer, move all of the buffer length types from 'size_t' to 'int' to reduce the potential for conversion and overflow problems.

---
 include/linux/audit.h | 18 ++++++++----------
 kernel/audit.c        | 37 +++++++++++++++++++++----------------
 2 files changed, 29 insertions(+), 26 deletions(-)
include/linux/audit.h +8 -10
index 45abb3722d30..d327113c913d 100644
@@ -172,16 +172,14 @@ extern __printf(2, 3)
 void audit_log_format(struct audit_buffer *ab, const char *fmt, ...);
 extern void		    audit_log_end(struct audit_buffer *ab);
 extern bool		    audit_string_contains_control(const char *string,
-							  size_t len);
+							  int len);
 extern void		    audit_log_n_hex(struct audit_buffer *ab,
-					  const unsigned char *buf,
-					  size_t len);
+					    const unsigned char *buf, int len);
 extern void		    audit_log_n_string(struct audit_buffer *ab,
-					       const char *buf,
-					       size_t n);
+					       const char *buf, int len);
 extern void		    audit_log_n_untrustedstring(struct audit_buffer *ab,
 							const char *string,
-							size_t n);
+							int len);
 extern void		    audit_log_untrustedstring(struct audit_buffer *ab,
 						      const char *string);
 extern void		    audit_log_d_path(struct audit_buffer *ab,
@@ -240,13 +238,13 @@ void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
 static inline void audit_log_end(struct audit_buffer *ab)
 { }
 static inline void audit_log_n_hex(struct audit_buffer *ab,
-				   const unsigned char *buf, size_t len)
+				   const unsigned char *buf, int len)
 { }
-static inline void audit_log_n_string(struct audit_buffer *ab,
-				      const char *buf, size_t n)
+static inline void audit_log_n_string(struct audit_buffer *ab, const char *buf,
+				      int len)
 { }
 static inline void  audit_log_n_untrustedstring(struct audit_buffer *ab,
-						const char *string, size_t n)
+						const char *string, int len)
 { }
 static inline void audit_log_untrustedstring(struct audit_buffer *ab,
 					     const char *string)
kernel/audit.c +21 -16
index 520244544907..9e10adf21c76 100644
@@ -2080,14 +2080,13 @@ void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
  * ascii hex digits. The new string is placed onto the skb.
  */
 void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
-		size_t len)
+		     int len)
 {
-	int avail;
-	size_t i, new_len;
+	int avail, new_len, i;
 	unsigned char *ptr;
 	struct sk_buff *skb;
 
-	if (!ab)
+	if (!ab || len <= 0)
 		return;
 
 	BUG_ON(!ab->skb);
@@ -2109,22 +2108,21 @@ void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
 	for (i = 0; i < len; i++)
 		ptr = hex_byte_pack_upper(ptr, buf[i]);
 	*ptr = 0;
-	skb_put(skb, len << 1); /* new string is twice the old string */
+	skb_put(skb, new_len);
 }
 
 /*
- * Format a string of no more than slen characters into the audit buffer,
+ * Format a string of no more than len characters into the audit buffer,
  * enclosed in quote marks.
  */
 void audit_log_n_string(struct audit_buffer *ab, const char *string,
-			size_t slen)
+			int len)
 {
-	int avail;
-	size_t new_len;
+	int avail, new_len;
 	unsigned char *ptr;
 	struct sk_buff *skb;
 
-	if (!ab)
+	if (!ab || len <= 0)
 		return;
 
 	BUG_ON(!ab->skb);
@@ -2132,7 +2130,7 @@ void audit_log_n_string(struct audit_buffer *ab, const char *string,
 	avail = skb_tailroom(skb);
 
 	/* enclosing quotes + null terminator */
-	if (check_add_overflow(slen, 3, &new_len)) {
+	if (check_add_overflow(len, 3, &new_len)) {
 		audit_log_format(ab, "?");
 		return;
 	}
@@ -2144,11 +2142,11 @@ void audit_log_n_string(struct audit_buffer *ab, const char *string,
 	}
 	ptr = skb_tail_pointer(skb);
 	*ptr++ = '"';
-	memcpy(ptr, string, slen);
-	ptr += slen;
+	memcpy(ptr, string, len);
+	ptr += len;
 	*ptr++ = '"';
 	*ptr = 0;
-	skb_put(skb, slen + 2);	/* don't include null terminator */
+	skb_put(skb, len + 2); /* don't include null terminator */
 }
 
 /**
@@ -2156,9 +2154,13 @@ void audit_log_n_string(struct audit_buffer *ab, const char *string,
  * @string: string to be checked
  * @len: max length of the string to check
  */
-bool audit_string_contains_control(const char *string, size_t len)
+bool audit_string_contains_control(const char *string, int len)
 {
 	const unsigned char *p;
+
+	if (!string || len <= 0)
+		return false;
+
 	for (p = string; p < (const unsigned char *)string + len; p++) {
 		if (*p == '"' || *p < 0x21 || *p > 0x7e)
 			return true;
@@ -2181,8 +2183,11 @@ bool audit_string_contains_control(const char *string, size_t len)
  * or may not be the entire string.
  */
 void audit_log_n_untrustedstring(struct audit_buffer *ab, const char *string,
-				 size_t len)
+				 int len)
 {
+	if (len <= 0)
+		return;
+
 	if (audit_string_contains_control(string, len))
 		audit_log_n_hex(ab, string, len);
 	else
Signature · 1 line
2.55.0
author maintainer
#4 lore raw

RFC PATCH 4/5 audit: add a missing function definition when !CONFIG_AUDIT

In reply to Paul Moore

The audit_string_contains_control() function does not have a dummy function definition in the !CONFIG_AUDIT case.

---
 include/linux/audit.h | 4 ++++
 1 file changed, 4 insertions(+)
include/linux/audit.h +4 -0
index d327113c913d..ac8f5f77d442 100644
@@ -237,6 +237,10 @@ void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
 { }
 static inline void audit_log_end(struct audit_buffer *ab)
 { }
+static inline bool audit_string_contains_control(const char *string, int len);
+{
+	return false;
+}
 static inline void audit_log_n_hex(struct audit_buffer *ab,
 				   const unsigned char *buf, int len)
 { }
Signature · 1 line
2.55.0
Reviewed-by on v1 4/5
#5 lore raw

Re: [RFC PATCH 4/5] audit: add a missing function definition when !CONFIG_AUDIT

Quoted 14 lines · Paul Moore

The audit_string_contains_control() function does not have a dummy function definition in the !CONFIG_AUDIT case.

---
 include/linux/audit.h | 4 ++++
 1 file changed, 4 insertions(+)
include/linux/audit.h +0 -0
index d327113c913d..ac8f5f77d442 100644

include/linux/audit.h void audit_log_format(struct audit_buffer *ab, const char *fmt, ...) · line 243

Quoted 7 lines
include/linux/audit.h +4 -0
@@ -237,6 +237,10 @@ void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
 { }
 static inline void audit_log_end(struct audit_buffer *ab)
 { }
+static inline bool audit_string_contains_control(const char *string, int len);
+{
+       return false;
+}

Hi Paul,

I noticed a stray semicolon here. Otherwise, it looks good.

Quoted 7 lines
include/linux/audit.h +0 -1
 static inline void audit_log_n_hex(struct audit_buffer *ab,
                                   const unsigned char *buf, int len)
 { }
--

2.55.0

-Ricardo

author maintainer
#6 lore raw

Re: [RFC PATCH 4/5] audit: add a missing function definition when !CONFIG_AUDIT

Quoted 25 lines · Ricardo Robaina
Quoted 14 lines · Paul Moore

The audit_string_contains_control() function does not have a dummy function definition in the !CONFIG_AUDIT case.

---
 include/linux/audit.h | 4 ++++
 1 file changed, 4 insertions(+)
include/linux/audit.h +0 -0
index d327113c913d..ac8f5f77d442 100644

include/linux/audit.h void audit_log_format(struct audit_buffer *ab, const char *fmt, ...) · line 243

Quoted 7 lines
include/linux/audit.h +4 -0
@@ -237,6 +237,10 @@ void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
 { }
 static inline void audit_log_end(struct audit_buffer *ab)
 { }
+static inline bool audit_string_contains_control(const char *string, int len);
+{
+       return false;
+}

Hi Paul,

Quoted 3 lines

I noticed a stray semicolon here. Otherwise, it looks good.

Good catch, thanks!

Signature · 1 line
paul-moore.com
author maintainer
#7 lore raw

RFC PATCH 5/5 audit: drop BUG_ON() from audit_log_XXX()

In reply to Paul Moore

We can easily drop the BUG_ON()s from the audit_log_XXX() helpers and simply return to the caller as we do if we are passed a NULL audit_buffer.

---
 kernel/audit.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
kernel/audit.c +3 -6
index 9e10adf21c76..f832c297fb33 100644
@@ -2027,10 +2027,9 @@ void audit_log_vformat(struct audit_buffer *ab, const char *fmt, va_list args)
 	struct sk_buff *skb;
 	va_list args2;
 
-	if (!ab)
+	if (!ab || !ab->skb)
 		return;
 
-	BUG_ON(!ab->skb);
 	skb = ab->skb;
 	avail = skb_tailroom(skb);
 	va_copy(args2, args);
@@ -2086,10 +2085,9 @@ void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
 	unsigned char *ptr;
 	struct sk_buff *skb;
 
-	if (!ab || len <= 0)
+	if (!ab || !ab->skb || len <= 0)
 		return;
 
-	BUG_ON(!ab->skb);
 	skb = ab->skb;
 	avail = skb_tailroom(skb);
 
@@ -2122,10 +2120,9 @@ void audit_log_n_string(struct audit_buffer *ab, const char *string,
 	unsigned char *ptr;
 	struct sk_buff *skb;
 
-	if (!ab || len <= 0)
+	if (!ab || !ab->skb || len <= 0)
 		return;
 
-	BUG_ON(!ab->skb);
 	skb = ab->skb;
 	avail = skb_tailroom(skb);
 
Signature · 1 line
2.55.0