linux-security-module.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Denis Efremov <efremov@linux.com>
To: unlisted-recipients:; (no To-header on input)
Cc: Denis Efremov <efremov@linux.com>,
	linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Casey Schaufler <casey@schaufler-ca.com>,
	James Morris <jmorris@namei.org>,
	"Serge E. Hallyn" <serge@hallyn.com>,
	stable@vger.kernel.org
Subject: [PATCH] Smack: check length in smk_set_cipso()
Date: Wed, 20 Nov 2019 17:51:18 +0300	[thread overview]
Message-ID: <20191120145118.30402-1-efremov@linux.com> (raw)

It's possible to trigger out-of-bounds read in smk_set_cipso().
For example (from root):
$ echo "test 1" > /sys/fs/smackfs/cipso2
BUG: KASAN: slab-out-of-bounds in vsscanf+0x2203/0x2990
Read of size 1 at addr ffff888061b023c9 by task bash/5578

The patch adds length checks for SMK_LONG_FMT format.

The bug was found by syzkaller.

Cc: Casey Schaufler <casey@schaufler-ca.com>
Cc: James Morris <jmorris@namei.org>
Cc: "Serge E. Hallyn" <serge@hallyn.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Denis Efremov <efremov@linux.com>
---
 security/smack/smackfs.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/security/smack/smackfs.c b/security/smack/smackfs.c
index e3e05c04dbd1..fad50a5a807b 100644
--- a/security/smack/smackfs.c
+++ b/security/smack/smackfs.c
@@ -878,6 +878,9 @@ static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
 	else
 		rule += strlen(skp->smk_known) + 1;
 
+	if (rule - data + 2 * SMK_DIGITLEN - 1 >= count)
+		goto out;
+
 	ret = sscanf(rule, "%d", &maplevel);
 	if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
 		goto out;
@@ -887,15 +890,19 @@ static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
 	if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
 		goto out;
 
-	if (format == SMK_FIXED24_FMT &&
-	    count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
+	rule += SMK_DIGITLEN;
+
+	if ((format == SMK_FIXED24_FMT &&
+	     count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN)) ||
+	    (format == SMK_LONG_FMT &&
+	     count != (rule - data + catlen * SMK_DIGITLEN)))
 		goto out;
 
 	memset(mapcatset, 0, sizeof(mapcatset));
 
 	for (i = 0; i < catlen; i++) {
-		rule += SMK_DIGITLEN;
 		ret = sscanf(rule, "%u", &cat);
+		rule += SMK_DIGITLEN;
 		if (ret != 1 || cat > SMACK_CIPSO_MAXCATNUM)
 			goto out;
 
-- 
2.21.0


                 reply	other threads:[~2019-11-20 14:51 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20191120145118.30402-1-efremov@linux.com \
    --to=efremov@linux.com \
    --cc=casey@schaufler-ca.com \
    --cc=jmorris@namei.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=serge@hallyn.com \
    --cc=stable@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).