Skip to contentSkip to conversation

audit: Fix check of return value of strnlen_user()

PATCH Applied Jan Kara 2 participants 7 messages Jun 2, 2015 – Jun 11, 2015 Audit

lore
Share & tools
lore

Jan Kara

author
#0 lore raw

PATCH 1/1 audit: Fix check of return value of strnlen_user()

strnlen_user() returns 0 when it hits fault, not -1. Fix the test in audit_log_single_execve_arg(). Luckily this shouldn't ever happen unless there's a kernel bug so it's mostly a cosmetic fix.

---
 kernel/auditsc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/auditsc.c +1 -1
index 9fb9d1cb83ce..bb947ceeee4d 100644
@@ -1023,7 +1023,7 @@ static int audit_log_single_execve_arg(struct audit_context *context,
 	 * for strings that are too long, we should not have created
 	 * any.
 	 */
-	if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
+	if (unlikely((len == 0) || len > MAX_ARG_STRLEN - 1)) {
 		WARN_ON(1);
 		send_sig(SIGKILL, current, 0);
 		return -1;
Signature · 1 line
2.1.4
maintainer
#1 lore raw
Quoted 17 lines · Jan Kara

strnlen_user() returns 0 when it hits fault, not -1. Fix the test in audit_log_single_execve_arg(). Luckily this shouldn't ever happen unless there's a kernel bug so it's mostly a cosmetic fix.

---
 kernel/auditsc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/auditsc.c +0 -0
index 9fb9d1cb83ce..bb947ceeee4d 100644
@@ -1023,7 +1023,7 @@ static int audit_log_single_execve_arg(struct

audit_context *context, * for strings that are too long, we should not have created

kernel/auditsc.c static int audit_log_single_execve_arg(struct audit_context *context, · line 1026

Quoted 4 lines
kernel/auditsc.c +1 -1
 	 * any.
 	 */
-	if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
+	if (unlikely((len == 0) || len > MAX_ARG_STRLEN - 1)) {

While we're at it, should we make it just "len > MAX_ARG_STRLEN" as well? Reading the comments in include/uapi/linux/binfmts.h as well as valid_arg_len() that seems to be the correct logic.

Quoted 3 lines
kernel/auditsc.c +0 -0
 		WARN_ON(1);
 		send_sig(SIGKILL, current, 0);
 		return -1;
Signature · 2 lines
paul moore
www.paul-moore.com
author
#2 lore raw
Quoted 11 lines · Paul Moore
Quoted 10 lines · Jan Kara

strnlen_user() returns 0 when it hits fault, not -1. Fix the test in audit_log_single_execve_arg(). Luckily this shouldn't ever happen unless there's a kernel bug so it's mostly a cosmetic fix.

---
 kernel/auditsc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

kernel/auditsc.c static int audit_log_single_execve_arg(struct audit_context *context, · line 1026

Quoted 11 lines
kernel/auditsc.c +0 -0
index 9fb9d1cb83ce..bb947ceeee4d 100644
@@ -1023,7 +1023,7 @@ static int audit_log_single_execve_arg(struct

audit_context *context, * for strings that are too long, we should not have created

kernel/auditsc.c +1 -1
 	 * any.
 	 */
-	if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
+	if (unlikely((len == 0) || len > MAX_ARG_STRLEN - 1)) {

While we're at it, should we make it just "len > MAX_ARG_STRLEN" as well? Reading the comments in include/uapi/linux/binfmts.h as well as valid_arg_len() that seems to be the correct logic.

Umm, but audit_log_single_execve_arg() does decrement 1 from
strnlen_user() result before doing the comparison. So the current test seems to match the one in valid_arg_len() exactly...

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#3 lore raw
Quoted 28 lines · Jan Kara
Quoted 11 lines · Paul Moore
Quoted 10 lines · Jan Kara

strnlen_user() returns 0 when it hits fault, not -1. Fix the test in audit_log_single_execve_arg(). Luckily this shouldn't ever happen unless there's a kernel bug so it's mostly a cosmetic fix.

---
 kernel/auditsc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

kernel/auditsc.c static int audit_log_single_execve_arg(struct audit_context *context, · line 1026

Quoted 11 lines
kernel/auditsc.c +0 -0
index 9fb9d1cb83ce..bb947ceeee4d 100644
@@ -1023,7 +1023,7 @@ static int audit_log_single_execve_arg(struct

audit_context *context, * for strings that are too long, we should not have created
* any.
*/

kernel/auditsc.c +1 -1
-   if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
+   if (unlikely((len == 0) || len > MAX_ARG_STRLEN - 1)) {

While we're at it, should we make it just "len > MAX_ARG_STRLEN" as well? Reading the comments in include/uapi/linux/binfmts.h as well as valid_arg_len() that seems to be the correct logic.

Quoted 3 lines

Umm, but audit_log_single_execve_arg() does decrement 1 from
strnlen_user() result before doing the comparison. So the current test seems to match the one in valid_arg_len() exactly...

For reference (taken from fs/exec.c in Linus' tree just now):

  static bool valid_arg_len(struct linux_binprm *bprm, long len)
  {
          return len <= MAX_ARG_STRLEN;
  }

The valid_arg_len() returns true when the length is less than or equal to MAX_ARG_STRLEN, implying that lengths greater than MAX_ARG_STRLEN are invalid. The existing test in audit_log_single_execve_arg() treats lengths greater than (MAX_ARG_STRLEN-1) as invalid.

These two tests do not look the same to me.

Signature · 2 lines
paul moore
www.paul-moore.com
author
#4 lore raw
Quoted 40 lines · Paul Moore
Quoted 28 lines · Jan Kara
Quoted 11 lines · Paul Moore
Quoted 10 lines · Jan Kara

strnlen_user() returns 0 when it hits fault, not -1. Fix the test in audit_log_single_execve_arg(). Luckily this shouldn't ever happen unless there's a kernel bug so it's mostly a cosmetic fix.

---
 kernel/auditsc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

kernel/auditsc.c static int audit_log_single_execve_arg(struct audit_context *context, · line 1026

Quoted 11 lines
kernel/auditsc.c +0 -0
index 9fb9d1cb83ce..bb947ceeee4d 100644
@@ -1023,7 +1023,7 @@ static int audit_log_single_execve_arg(struct

audit_context *context, * for strings that are too long, we should not have created
* any.
*/

kernel/auditsc.c +1 -1
-   if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
+   if (unlikely((len == 0) || len > MAX_ARG_STRLEN - 1)) {

While we're at it, should we make it just "len > MAX_ARG_STRLEN" as well? Reading the comments in include/uapi/linux/binfmts.h as well as valid_arg_len() that seems to be the correct logic.

Quoted 3 lines

Umm, but audit_log_single_execve_arg() does decrement 1 from
strnlen_user() result before doing the comparison. So the current test seems to match the one in valid_arg_len() exactly...

For reference (taken from fs/exec.c in Linus' tree just now):

  static bool valid_arg_len(struct linux_binprm *bprm, long len)
  {
          return len <= MAX_ARG_STRLEN;
  }
Quoted 4 lines

The valid_arg_len() returns true when the length is less than or equal to MAX_ARG_STRLEN, implying that lengths greater than MAX_ARG_STRLEN are invalid. The existing test in audit_log_single_execve_arg() treats lengths greater than (MAX_ARG_STRLEN-1) as invalid.

But the 'len' passed to valid_arg_len() is the return value of
strnlen_user() and that one returns lenght *including* the terminating '\0'. So in fact valid_arg_len() tests whether the argument length is <= (MAX_ARG_STRLEN-1). Hmm?

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#5 lore raw
Quoted 41 lines · Jan Kara
Quoted 40 lines · Paul Moore
Quoted 28 lines · Jan Kara
Quoted 11 lines · Paul Moore
Quoted 10 lines · Jan Kara

strnlen_user() returns 0 when it hits fault, not -1. Fix the test in audit_log_single_execve_arg(). Luckily this shouldn't ever happen unless there's a kernel bug so it's mostly a cosmetic fix.

---
 kernel/auditsc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

kernel/auditsc.c static int audit_log_single_execve_arg(struct audit_context *context, · line 1026

Quoted 11 lines
kernel/auditsc.c +0 -0
index 9fb9d1cb83ce..bb947ceeee4d 100644
@@ -1023,7 +1023,7 @@ static int audit_log_single_execve_arg(struct

audit_context *context, * for strings that are too long, we should not have created
* any.
*/

kernel/auditsc.c +1 -1
-   if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
+   if (unlikely((len == 0) || len > MAX_ARG_STRLEN - 1)) {

While we're at it, should we make it just "len > MAX_ARG_STRLEN" as well? Reading the comments in include/uapi/linux/binfmts.h as well as valid_arg_len() that seems to be the correct logic.

Quoted 3 lines

Umm, but audit_log_single_execve_arg() does decrement 1 from
strnlen_user() result before doing the comparison. So the current test seems to match the one in valid_arg_len() exactly...

For reference (taken from fs/exec.c in Linus' tree just now):

  static bool valid_arg_len(struct linux_binprm *bprm, long len)
  {
          return len <= MAX_ARG_STRLEN;
  }
Quoted 8 lines
Quoted 4 lines

The valid_arg_len() returns true when the length is less than or equal to MAX_ARG_STRLEN, implying that lengths greater than MAX_ARG_STRLEN are invalid. The existing test in audit_log_single_execve_arg() treats lengths greater than (MAX_ARG_STRLEN-1) as invalid.

But the 'len' passed to valid_arg_len() is the return value of
strnlen_user() and that one returns lenght *including* the terminating '\0'. So in fact valid_arg_len() tests whether the argument length is <= (MAX_ARG_STRLEN-1). Hmm?

I must have spaced on the fact that audit subtracts the NULL from the output of strnlen_user() when it calls it earlier in the function.

Signature · 2 lines
paul moore
www.paul-moore.com
maintainer Applied
#6 lore raw
In reply to Jan Kara
Quoted 9 lines · Jan Kara

strnlen_user() returns 0 when it hits fault, not -1. Fix the test in audit_log_single_execve_arg(). Luckily this shouldn't ever happen unless there's a kernel bug so it's mostly a cosmetic fix.

---
 kernel/auditsc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Merged to audit#next, thanks for your patience.

Quoted 14 lines
kernel/auditsc.c +0 -0
index 9fb9d1cb83ce..bb947ceeee4d 100644
@@ -1023,7 +1023,7 @@ static int audit_log_single_execve_arg(struct

audit_context *context, * for strings that are too long, we should not have created

kernel/auditsc.c +1 -1
 	 * any.
 	 */
-	if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
+	if (unlikely((len == 0) || len > MAX_ARG_STRLEN - 1)) {
 		WARN_ON(1);
 		send_sig(SIGKILL, current, 0);
 		return -1;
Signature · 2 lines
paul moore
www.paul-moore.com