Skip to contentSkip to conversation

lsm: convert security_setselfattr() to use memdup_user()

PATCH Applied Paul Moore 2 participants 4 messages Nov 2, 2023 – Nov 13, 2023 Security (LSM)

lore
Share & tools
lore

Paul Moore

author maintainer
#0 lore raw

PATCH 1/1 lsm: convert security_setselfattr() to use memdup_user()

As suggested by the kernel test robot, memdup_user() is a better option than the combo of kmalloc()/copy_from_user().

---
 security/security.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
security/security.c +3 -8
index a808fd5eba6d..d7b15ea67c3f 100644
@@ -4011,14 +4011,9 @@ int security_setselfattr(unsigned int attr, struct lsm_ctx __user *uctx,
 	if (size > PAGE_SIZE)
 		return -E2BIG;
 
-	lctx = kmalloc(size, GFP_KERNEL);
-	if (lctx == NULL)
-		return -ENOMEM;
-
-	if (copy_from_user(lctx, uctx, size)) {
-		rc = -EFAULT;
-		goto free_out;
-	}
+	lctx = memdup_user(uctx, size);
+	if (IS_ERR(lctx))
+		return PTR_ERR(lctx);
 
 	if (size < lctx->len || size < lctx->ctx_len + sizeof(*lctx) ||
 	    lctx->len < lctx->ctx_len + sizeof(*lctx)) {
Signature · 1 line
2.42.0
Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · Paul Moore

As suggested by the kernel test robot, memdup_user() is a better option than the combo of kmalloc()/copy_from_user().

Quoted 26 lines
---
 security/security.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
security/security.c +3 -8
index a808fd5eba6d..d7b15ea67c3f 100644
@@ -4011,14 +4011,9 @@ int security_setselfattr(unsigned int attr, struct lsm_ctx __user *uctx,
 	if (size > PAGE_SIZE)
 		return -E2BIG;
 
-	lctx = kmalloc(size, GFP_KERNEL);
-	if (lctx == NULL)
-		return -ENOMEM;
-
-	if (copy_from_user(lctx, uctx, size)) {
-		rc = -EFAULT;
-		goto free_out;
-	}
+	lctx = memdup_user(uctx, size);
+	if (IS_ERR(lctx))
+		return PTR_ERR(lctx);
 
 	if (size < lctx->len || size < lctx->ctx_len + sizeof(*lctx) ||
 	    lctx->len < lctx->ctx_len + sizeof(*lctx)) {
author maintainer Applied
#2 lore raw
In reply to Paul Moore
Quoted 10 lines · Paul Moore

As suggested by the kernel test robot, memdup_user() is a better option than the combo of kmalloc()/copy_from_user().

---
 security/security.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)

Merged into lsm/dev-staging.

Signature · 1 line
paul-moore.com
author maintainer Applied
#3 lore raw
In reply to Paul Moore
Quoted 10 lines · Paul Moore

As suggested by the kernel test robot, memdup_user() is a better option than the combo of kmalloc()/copy_from_user().

---
 security/security.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)

Merged into lsm/dev.

Signature · 1 line
paul-moore.com