Skip to contentSkip to conversation

apparmor: Replace memcpy + NUL termination with kmemdup_nul in do_setattr

PATCH Superseded Thorsten Blum 2 participants 2 messages Jan 25 – Mar 18

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 apparmor: Replace memcpy + NUL termination with kmemdup_nul in do_setattr

Use kmemdup_nul() to copy 'value' instead of using memcpy() followed by a manual NUL termination. No functional changes.

---
 security/apparmor/lsm.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
security/apparmor/lsm.c +1 -4
index a87cd60ed206..98b92af5890e 100644
@@ -866,12 +866,9 @@ static int do_setattr(u64 attr, void *value, size_t size)
 
 	/* AppArmor requires that the buffer must be null terminated atm */
 	if (args[size - 1] != '\0') {
-		/* null terminate */
-		largs = args = kmalloc(size + 1, GFP_KERNEL);
+		largs = args = kmemdup_nul(value, size, GFP_KERNEL);
 		if (!args)
 			return -ENOMEM;
-		memcpy(args, value, size);
-		args[size] = '\0';
 	}
 
 	error = -EINVAL;
Signature · 2 lines
Thorsten Blum <[email protected]>
GPG: 1D60 735E 8AEF 3BE4 73B6  9D84 7336 78FD 8DFE EAD4
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 4 lines · Thorsten Blum

Use kmemdup_nul() to copy 'value' instead of using memcpy() followed by a manual NUL termination. No functional changes.

I have pulled this into my tree

Quoted 22 lines
---
  security/apparmor/lsm.c | 5 +----
  1 file changed, 1 insertion(+), 4 deletions(-)
security/apparmor/lsm.c +1 -4
index a87cd60ed206..98b92af5890e 100644
@@ -866,12 +866,9 @@ static int do_setattr(u64 attr, void *value, size_t size)
 
  	/* AppArmor requires that the buffer must be null terminated atm */
  	if (args[size - 1] != '\0') {
-		/* null terminate */
-		largs = args = kmalloc(size + 1, GFP_KERNEL);
+		largs = args = kmemdup_nul(value, size, GFP_KERNEL);
  		if (!args)
  			return -ENOMEM;
-		memcpy(args, value, size);
-		args[size] = '\0';
  	}
 
  	error = -EINVAL;