All of lore.kernel.org
 help / color / mirror / Atom feed
From: Casey Schaufler <casey@schaufler-ca.com>
To: Kees Cook <keescook@chromium.org>
Cc: LSM <linux-security-module@vger.kernel.org>,
	James Morris <jmorris@namei.org>,
	John Johansen <john.johansen@canonical.com>,
	Stephen Smalley <sds@tycho.nsa.gov>,
	Paul Moore <paul@paul-moore.com>,
	Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>,
	LKLM <linux-kernel@vger.kernel.org>
Subject: [PATCH v4 4/3] LSM: Improve context interface for proc attrs
Date: Fri, 24 Jun 2016 09:38:45 -0700	[thread overview]
Message-ID: <61a5d3be-b99c-69d3-8619-e5c4d1004dd5@schaufler-ca.com> (raw)
In-Reply-To: <CAGXu5j+e_0MG1mi69dgagFvbmm_93v8rezr-kPRoCsmEvOY0ow@mail.gmail.com>

Subject: [PATCH v4 4/3] LSM: Improve context interface for proc attrs

Replace kzalloc ... sprintf with kasprintf in the
"context" procfs attr code.

Signed-off-by: Casey Schaufler <casey@schaufler-ca.com>

---
 security/apparmor/lsm.c    | 47 +++++++++++++++++++++++-----------------------
 security/selinux/hooks.c   |  4 +---
 security/smack/smack_lsm.c |  5 +----
 3 files changed, 26 insertions(+), 30 deletions(-)

diff --git a/security/apparmor/lsm.c b/security/apparmor/lsm.c
index 3790a7d..5cac15f 100644
--- a/security/apparmor/lsm.c
+++ b/security/apparmor/lsm.c
@@ -476,6 +476,8 @@ static int apparmor_getprocattr(struct task_struct *task, char *name,
 	const struct cred *cred = get_task_cred(task);
 	struct aa_task_cxt *cxt = cred_cxt(cred);
 	struct aa_profile *profile = NULL;
+	char *vp;
+	char *np;
 
 	if (strcmp(name, "current") == 0)
 		profile = aa_get_newest_profile(cxt->profile);
@@ -488,30 +490,29 @@ static int apparmor_getprocattr(struct task_struct *task, char *name,
 	else
 		error = -EINVAL;
 
-	if (profile) {
-		if (strcmp(name, "context") == 0) {
-			char *vp;
-			char *np;
-
-			error = aa_getprocattr(profile, &vp);
-			if (error > 0) {
-				error += 12;
-				*value = kzalloc(error, GFP_KERNEL);
-				if (*value == NULL)
-					error = -ENOMEM;
-				else {
-					sprintf(*value, "apparmor='%s'", vp);
-					np = strchr(*value, '\n');
-					if (np != NULL) {
-						np[0] = '\'';
-						np[1] = '\0';
-					}
-				}
-			}
-		} else
-			error = aa_getprocattr(profile, value);
-	}
+	if (profile == NULL)
+		goto put_out;
+
+	error = aa_getprocattr(profile, &vp);
+	if (error < 0)
+		goto put_out;
+
+	if (strcmp(name, "context") == 0) {
+		*value = kasprintf(GFP_KERNEL, "apparmor='%s'", vp);
+		if (*value == NULL) {
+			error = -ENOMEM;
+			goto put_out;
+		}
+		np = strchr(*value, '\n');
+		if (np != NULL) {
+			np[0] = '\'';
+			np[1] = '\0';
+		}
+		error = strlen(*value);
+	} else
+		*value = vp;
 
+put_out:
 	aa_put_profile(profile);
 	put_cred(cred);
 
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 3a21c2b..6397721 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -5737,11 +5737,9 @@ static int selinux_getprocattr(struct task_struct *p,
 
 		error = security_sid_to_context(sid, &vp, &len);
 		if (!error) {
-			*value = kzalloc(len + 10, GFP_KERNEL);
+			*value = kasprintf(GFP_KERNEL, "selinux='%s'", vp);
 			if (*value == NULL)
 				error = -ENOMEM;
-			else
-				sprintf(*value, "selinux='%s'", vp);
 		}
 	}
 
diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
index d2d8624..92e66f8 100644
--- a/security/smack/smack_lsm.c
+++ b/security/smack/smack_lsm.c
@@ -3574,18 +3574,15 @@ static int smack_getprocattr(struct task_struct *p, char *name, char **value)
 {
 	struct smack_known *skp = smk_of_task_struct(p);
 	char *cp;
-	int slen;
 
 	if (strcmp(name, "current") == 0) {
 		cp = kstrdup(skp->smk_known, GFP_KERNEL);
 		if (cp == NULL)
 			return -ENOMEM;
 	} else if (strcmp(name, "context") == 0) {
-		slen = strlen(skp->smk_known) + 9;
-		cp = kzalloc(slen, GFP_KERNEL);
+		cp = kasprintf(GFP_KERNEL, "smack='%s'", skp->smk_known);
 		if (cp == NULL)
 			return -ENOMEM;
-		sprintf(cp, "smack='%s'", skp->smk_known);
 	} else
 		return -EINVAL;
 

  parent reply	other threads:[~2016-06-24 16:38 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-23 21:07 [PATCH v4 0/3] LSM: module hierarchy in /proc/.../attr Casey Schaufler
2016-06-23 21:10 ` [PATCH v4 1/3] LSM: Add /sys/kernel/security/lsm Casey Schaufler
2016-06-24 18:56   ` Paul Moore
2016-06-23 21:11 ` [PATCH v4 2/3] LSM: module hierarchy in /proc/.../attr Casey Schaufler
2016-06-24 19:11   ` Paul Moore
2016-06-24 20:05     ` Casey Schaufler
2016-06-24 20:08       ` Kees Cook
2016-06-24 20:29         ` Paul Moore
2016-06-24 23:26           ` [PATCH v5 0/3] LSM: security module information improvements Casey Schaufler
2016-06-24 23:27             ` [PATCH v5 1/3] LSM: Add /sys/kernel/security/lsm Casey Schaufler
2016-06-29 17:01               ` Paul Moore
2016-07-02 17:21                 ` John Johansen
2016-06-24 23:29             ` [PATCH v5 2/3] LSM: module hierarchy in /proc/.../attr Casey Schaufler
2016-06-29 17:03               ` Paul Moore
2016-07-02 17:24                 ` John Johansen
2016-06-24 23:29             ` [PATCH v4 3/3] LSM: Add context interface for proc attrs Casey Schaufler
2016-06-24 23:38               ` [PATCH v5 " Casey Schaufler
2016-06-29 17:04               ` [PATCH v4 " Paul Moore
2016-07-02 17:25                 ` John Johansen
2016-07-05 15:52                   ` [PATCH v5 0/3] LSM: security module information improvements - Acked Casey Schaufler
2016-07-08 10:05                     ` James Morris
2016-07-08 15:31                       ` Casey Schaufler
2016-07-05 15:52                   ` [PATCH v5 1/3] LSM: Add /sys/kernel/security/lsm " Casey Schaufler
2016-07-05 15:52                   ` [PATCH v5 2/3] LSM: module hierarchy in /proc/.../attr " Casey Schaufler
2016-07-05 15:52                   ` [PATCH v5 3/3] LSM: Add context interface for proc attrs " Casey Schaufler
2016-06-23 21:11 ` [PATCH v4 3/3] LSM: Add context interface for proc attrs Casey Schaufler
2016-06-23 21:49   ` Kees Cook
2016-06-23 22:10     ` Casey Schaufler
2016-06-24 16:38     ` Casey Schaufler [this message]
2016-06-24 17:48       ` [PATCH v4 4/3] LSM: Improve " Kees Cook
2016-06-24 19:15   ` [PATCH v4 3/3] LSM: Add " Paul Moore
2016-06-24 19:56     ` Casey Schaufler

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=61a5d3be-b99c-69d3-8619-e5c4d1004dd5@schaufler-ca.com \
    --to=casey@schaufler-ca.com \
    --cc=jmorris@namei.org \
    --cc=john.johansen@canonical.com \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=paul@paul-moore.com \
    --cc=penguin-kernel@i-love.sakura.ne.jp \
    --cc=sds@tycho.nsa.gov \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.