Skip to contentSkip to conversation

apparmor: replace strcpy with strscpy

PATCH In review Ryan Foster 2 participants 2 messages Jan 20 – Feb 5

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

PATCH 1/1 apparmor: replace strcpy with strscpy

Found by checkpatch. Replace strcpy() with strscpy() for safer string handling per KSPP recommendations.

Two changes:
- apparmorfs.c: gen_symlink_name() uses tracked buffer size
- lib.c: aa_policy_init() uses exact allocation size

---
 security/apparmor/apparmorfs.c | 2 +-
 security/apparmor/lib.c        | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
security/apparmor/apparmorfs.c +1 -1
index 907bd2667e28..f38974231df2 100644
@@ -1614,7 +1614,7 @@ static char *gen_symlink_name(int depth, const char *dirname, const char *fname)
 		return ERR_PTR(-ENOMEM);
 
 	for (; depth > 0; depth--) {
-		strcpy(s, "../../");
+		strscpy(s, "../../", size);
 		s += 6;
 		size -= 6;
 	}
security/apparmor/lib.c +1 -1
index 82dbb97ad406..7cb393f91a10 100644
@@ -487,7 +487,7 @@ bool aa_policy_init(struct aa_policy *policy, const char *prefix,
 	} else {
 		hname = aa_str_alloc(strlen(name) + 1, gfp);
 		if (hname)
-			strcpy(hname, name);
+			strscpy(hname, name, strlen(name) + 1);
 	}
 	if (!hname)
 		return false;
Signature · 1 line
2.52.0
maintainer
#1 lore raw
Quoted 9 lines · Ryan Foster

Found by checkpatch. Replace strcpy() with strscpy() for safer string handling per KSPP recommendations.

Two changes:
- apparmorfs.c: gen_symlink_name() uses tracked buffer size
- lib.c: aa_policy_init() uses exact allocation size

this collides with two different patches I previously pulled in

1c90ed1f14c98 apparmor: Replace deprecated strcpy with memcpy in gen_symlink_name b31d3f7385fbb apparmor: Replace sprintf/strcpy with scnprintf/strscpy in aa_policy_init

if you want to refresh this, I will look at pulling it in

Quoted 31 lines
---
  security/apparmor/apparmorfs.c | 2 +-
  security/apparmor/lib.c        | 2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)
security/apparmor/apparmorfs.c +1 -1
index 907bd2667e28..f38974231df2 100644
@@ -1614,7 +1614,7 @@ static char *gen_symlink_name(int depth, const char *dirname, const char *fname)
  		return ERR_PTR(-ENOMEM);
 
  	for (; depth > 0; depth--) {
-		strcpy(s, "../../");
+		strscpy(s, "../../", size);
  		s += 6;
  		size -= 6;
  	}
security/apparmor/lib.c +1 -1
index 82dbb97ad406..7cb393f91a10 100644
@@ -487,7 +487,7 @@ bool aa_policy_init(struct aa_policy *policy, const char *prefix,
  	} else {
  		hname = aa_str_alloc(strlen(name) + 1, gfp);
  		if (hname)
-			strcpy(hname, name);
+			strscpy(hname, name, strlen(name) + 1);
  	}
  	if (!hname)
  		return false;