Skip to contentSkip to conversation

audit: fix potential integer overflow in audit_log_n_hex()

PATCH Superseded Ricardo Robaina 3 participants 4 messages Jun 1 – Jul 1 Audit

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 audit: fix potential integer overflow in audit_log_n_hex()

The function calculates new_len as len << 1 for hex encoding. This has two overflow risks: the shift itself can overflow when len is large, and the result can be truncated when assigned to new_len (declared as int) from the size_t calculation.

Fix by using check_shl_overflow() to catch shift overflow and changing new_len and loop counter i to size_t to prevent truncation.

Fixes 168b7173959f ("AUDIT: Clean up logging of untrusted strings")

---
Changes in v2:
- Use check_shl_overflow() instead of manual overflow check.

 kernel/audit.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/audit.c +8 -2
index e1d489bc2dff..8ca268610641 100644
@@ -62,6 +62,7 @@
 #include <net/ip.h>
 #include <net/ipv6.h>
 #include <linux/sctp.h>
+#include <linux/overflow.h>
 
 #include "audit.h"
 
@@ -2076,7 +2077,8 @@ void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
 void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
 		size_t len)
 {
-	int i, avail, new_len;
+	int avail;
+	size_t i, new_len;
 	unsigned char *ptr;
 	struct sk_buff *skb;
 
@@ -2084,9 +2086,13 @@ void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
 		return;
 
 	BUG_ON(!ab->skb);
+
 	skb = ab->skb;
 	avail = skb_tailroom(skb);
-	new_len = len<<1;
+
+	if (check_shl_overflow(len, 1, &new_len))
+		return;
+
 	if (new_len >= avail) {
 		/* Round the buffer request up to the next multiple */
 		new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
Signature · 1 line
2.53.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 10 lines · Ricardo Robaina

The function calculates new_len as len << 1 for hex encoding. This has two overflow risks: the shift itself can overflow when len is large, and the result can be truncated when assigned to new_len (declared as int) from the size_t calculation.

Fix by using check_shl_overflow() to catch shift overflow and changing new_len and loop counter i to size_t to prevent truncation.

Fixes 168b7173959f ("AUDIT: Clean up logging of untrusted strings")
Quoted 48 lines

---
Changes in v2:
- Use check_shl_overflow() instead of manual overflow check.

 kernel/audit.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/audit.c +8 -2
index e1d489bc2dff..8ca268610641 100644
@@ -62,6 +62,7 @@
 #include <net/ip.h>
 #include <net/ipv6.h>
 #include <linux/sctp.h>
+#include <linux/overflow.h>
 
 #include "audit.h"
 
@@ -2076,7 +2077,8 @@ void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
 void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
 		size_t len)
 {
-	int i, avail, new_len;
+	int avail;
+	size_t i, new_len;
 	unsigned char *ptr;
 	struct sk_buff *skb;
 
@@ -2084,9 +2086,13 @@ void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
 		return;
 
 	BUG_ON(!ab->skb);
+
 	skb = ab->skb;
 	avail = skb_tailroom(skb);
-	new_len = len<<1;
+
+	if (check_shl_overflow(len, 1, &new_len))
+		return;
+
 	if (new_len >= avail) {
 		/* Round the buffer request up to the next multiple */
 		new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
Signature · 1 line
2.53.0

- RGB

--
Richard Guy Briggs <[email protected]>
Sr. S/W Engineer, Kernel Security, Base Operating Systems Remote, Ottawa, Red Hat Canada
Upstream IRC: SunRaycer
Voice: +1.613.860 2354 SMS: +1.613.518.6570

maintainer
#2 lore raw
In reply to Ricardo Robaina
Quoted 42 lines · On Jun 1, 2026 Ricardo Robaina <[email protected]> wrote:

The function calculates new_len as len << 1 for hex encoding. This has two overflow risks: the shift itself can overflow when len is large, and the result can be truncated when assigned to new_len (declared as int) from the size_t calculation.

Fix by using check_shl_overflow() to catch shift overflow and changing new_len and loop counter i to size_t to prevent truncation.

Fixes 168b7173959f ("AUDIT: Clean up logging of untrusted strings")

---
Changes in v2:
- Use check_shl_overflow() instead of manual overflow check.

 kernel/audit.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/audit.c +3 -1
index e1d489bc2dff..8ca268610641 100644
@@ -62,6 +62,7 @@
 #include <net/ip.h>
 #include <net/ipv6.h>
 #include <linux/sctp.h>
+#include <linux/overflow.h>
 
 #include "audit.h"
 
@@ -2076,7 +2077,8 @@ void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
 void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
 		size_t len)
 {
-	int i, avail, new_len;
+	int avail;
+	size_t i, new_len;
 	unsigned char *ptr;
 	struct sk_buff *skb;
 

kernel/audit.c void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf, · line 2094

Quoted 10 lines
kernel/audit.c +4 -1
@@ -2084,9 +2086,13 @@ void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
 		return;
 
 	BUG_ON(!ab->skb);
+
 	skb = ab->skb;
 	avail = skb_tailroom(skb);
-	new_len = len<<1;
+
+	if (check_shl_overflow(len, 1, &new_len))
+		return;

By returning without logging a value I worry we could end up with an oddly formatted audit record, e.g. "... A=foo B= C=bar ...". Instead of simply returning, should we log a '?' for the value and then return?

Quoted 5 lines
kernel/audit.c +0 -0
 	if (new_len >= avail) {
 		/* Round the buffer request up to the next multiple */
 		new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
Signature · 1 line
2.53.0

--
paul-moore.com

#3 lore raw
Quoted 46 lines · Paul Moore
Quoted 44 lines · On Jun 1, 2026 Ricardo Robaina <[email protected]> wrote:

The function calculates new_len as len << 1 for hex encoding. This has two overflow risks: the shift itself can overflow when len is large, and the result can be truncated when assigned to new_len (declared as int) from the size_t calculation.

Fix by using check_shl_overflow() to catch shift overflow and changing new_len and loop counter i to size_t to prevent truncation.

Fixes 168b7173959f ("AUDIT: Clean up logging of untrusted strings")

---
Changes in v2:
- Use check_shl_overflow() instead of manual overflow check.

 kernel/audit.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/audit.c +3 -1
index e1d489bc2dff..8ca268610641 100644
@@ -62,6 +62,7 @@
 #include <net/ip.h>
 #include <net/ipv6.h>
 #include <linux/sctp.h>
+#include <linux/overflow.h>
 
 #include "audit.h"
 
@@ -2076,7 +2077,8 @@ void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
 void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
              size_t len)
 {
-     int i, avail, new_len;
+     int avail;
+     size_t i, new_len;
      unsigned char *ptr;
      struct sk_buff *skb;
 
@@ -2084,9 +2086,13 @@ void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
              return;
 
Quoted 13 lines

kernel/audit.c void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf, · line 2094

Quoted 8 lines
kernel/audit.c +4 -1
      BUG_ON(!ab->skb);
+
      skb = ab->skb;
      avail = skb_tailroom(skb);
-     new_len = len<<1;
+
+     if (check_shl_overflow(len, 1, &new_len))
+             return;

By returning without logging a value I worry we could end up with an oddly formatted audit record, e.g. "... A=foo B= C=bar ...". Instead of simply returning, should we log a '?' for the value and then return?

It makes sense, Paul. I'll send a new version shortly.

Quoted 9 lines
Quoted 5 lines
kernel/audit.c +0 -1
      if (new_len >= avail) {
              /* Round the buffer request up to the next multiple */
              new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
--

2.53.0

--
paul-moore.com