Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp9218pxb; Wed, 24 Feb 2021 16:33:06 -0800 (PST) X-Google-Smtp-Source: ABdhPJwgGYsA03Q9uMM32EbpQD6+mGrzkLsen0eoO8NqsB15/utlK3G7WrjaYrjtpwRawQLqg65E X-Received: by 2002:a05:6402:4ce:: with SMTP id n14mr397528edw.309.1614213186448; Wed, 24 Feb 2021 16:33:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614213186; cv=none; d=google.com; s=arc-20160816; b=VQia3vKhmQHK+KuylDq5iR+77jrY4lxJVVpEoP9SHGAFrrsuLyNMAk6QF8+5JGyTFQ Ssopj6pazZ8ZZt3uzelH8gCmtfeWgfalLaisPsq+dr6edsDhzOxfj+NCO4XShBg6YrOt VREB49f9R181hX/OuquCHw0HzW6L0Nwy53WxJPpqe/Fw8cQH6zz+CuOYU99wo4rHkKyM 7LReYZDHPekfE/Ez3pMvxrwNH1UTpm+k+OC8cLKaqtnNqDNq/XLbGu5ve5umN1xKdK75 hYXiGU+xZ4hCDbbtpNfx//4hzJKGiW2Rri4WdL02T7L19oi79KWh0JWMhDGfh8XlodoH eiAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=7o5kdETpwTcC8qna4mjEO/sCftAiWSWn36wnEEi2j4w=; b=ly+lfy3qBSGz60tiD3Fg3ytc4z5Ymzh1OKJMlGOPiPHfX3kyrse2kVaJFxunDISdLC UidOipAFohgRWR1Fvbl49CksCuYA1ATRkoz14qBPPo1KwG7NRLBpJcP+tD1kfneAslQm iVWJSjhS8ucqC7bWtn7gy5quRRDymjsH+0X3ndjbtMS54EN1AWacxuv1yaCd1g3Vc+5k Mfv3GVYbsLS6tk3tRKxrrXGe/IyQkCQu+ycFd7Xojbvb80tlSAklyMqhloPd25S/6AQ2 DuX2dYRhqhMqTqmajEBGc5YCzLoNyB47wKIINmtT4qMpXrFFmjwUXaLzLI63qL0ykQLT NQLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=VcJxoZf4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q21si2013219edt.445.2021.02.24.16.32.43; Wed, 24 Feb 2021 16:33:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=VcJxoZf4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236352AbhBXOQP (ORCPT + 99 others); Wed, 24 Feb 2021 09:16:15 -0500 Received: from mail.kernel.org ([198.145.29.99]:59904 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236031AbhBXNL0 (ORCPT ); Wed, 24 Feb 2021 08:11:26 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 175AF64F9A; Wed, 24 Feb 2021 12:55:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1614171332; bh=d+pnYoD9pnmVHjio02s/0OG0i3lMIpPT+tZE/Hi8ttY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VcJxoZf4UeNzjaI260+lf4ynZ/RbTWEYnPSA1XfucVh8i6bV7GX/44Wf6KByNOVKc e9xnztsmQnjnEsImnTcWiQtov7Wm9M1OburiGnRQL3UeY6f0ecgXaYskYPKKJ2JySk xE2nhSWwTEgz/QqvMFXMT4qXvvExso+X8rt2HkMSGkP0ePLvaOUNYH6fpzNj4/BVOi Fx7KqG/H27WTDt+QS5saMS89DtazSEGAHR2qCDkoh/SCiOsnCzEH8Gq0ut9EHgTt94 kQoU9ZCjCt7V/sHNFW7DHdtMtVARt0/FYHVNwGxRu1ewzqx4JvUNDngoNlsXLUUium zaXTeeIjvSYEg== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Sabyrzhan Tasbolatov , syzbot+a71a442385a0b2815497@syzkaller.appspotmail.com, Casey Schaufler , Sasha Levin , linux-security-module@vger.kernel.org Subject: [PATCH AUTOSEL 4.14 14/16] smackfs: restrict bytes count in smackfs write functions Date: Wed, 24 Feb 2021 07:55:11 -0500 Message-Id: <20210224125514.483935-14-sashal@kernel.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210224125514.483935-1-sashal@kernel.org> References: <20210224125514.483935-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sabyrzhan Tasbolatov [ Upstream commit 7ef4c19d245f3dc233fd4be5acea436edd1d83d8 ] syzbot found WARNINGs in several smackfs write operations where bytes count is passed to memdup_user_nul which exceeds GFP MAX_ORDER. Check count size if bigger than PAGE_SIZE. Per smackfs doc, smk_write_net4addr accepts any label or -CIPSO, smk_write_net6addr accepts any label or -DELETE. I couldn't find any general rule for other label lengths except SMK_LABELLEN, SMK_LONGLABEL, SMK_CIPSOMAX which are documented. Let's constrain, in general, smackfs label lengths for PAGE_SIZE. Although fuzzer crashes write to smackfs/netlabel on 0x400000 length. Here is a quick way to reproduce the WARNING: python -c "print('A' * 0x400000)" > /sys/fs/smackfs/netlabel Reported-by: syzbot+a71a442385a0b2815497@syzkaller.appspotmail.com Signed-off-by: Sabyrzhan Tasbolatov Signed-off-by: Casey Schaufler Signed-off-by: Sasha Levin --- security/smack/smackfs.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/security/smack/smackfs.c b/security/smack/smackfs.c index accd3846f1e3e..4f8c1a272df07 100644 --- a/security/smack/smackfs.c +++ b/security/smack/smackfs.c @@ -1191,7 +1191,7 @@ static ssize_t smk_write_net4addr(struct file *file, const char __user *buf, return -EPERM; if (*ppos != 0) return -EINVAL; - if (count < SMK_NETLBLADDRMIN) + if (count < SMK_NETLBLADDRMIN || count > PAGE_SIZE - 1) return -EINVAL; data = memdup_user_nul(buf, count); @@ -1451,7 +1451,7 @@ static ssize_t smk_write_net6addr(struct file *file, const char __user *buf, return -EPERM; if (*ppos != 0) return -EINVAL; - if (count < SMK_NETLBLADDRMIN) + if (count < SMK_NETLBLADDRMIN || count > PAGE_SIZE - 1) return -EINVAL; data = memdup_user_nul(buf, count); @@ -1858,6 +1858,10 @@ static ssize_t smk_write_ambient(struct file *file, const char __user *buf, if (!smack_privileged(CAP_MAC_ADMIN)) return -EPERM; + /* Enough data must be present */ + if (count == 0 || count > PAGE_SIZE) + return -EINVAL; + data = memdup_user_nul(buf, count); if (IS_ERR(data)) return PTR_ERR(data); @@ -2029,6 +2033,9 @@ static ssize_t smk_write_onlycap(struct file *file, const char __user *buf, if (!smack_privileged(CAP_MAC_ADMIN)) return -EPERM; + if (count > PAGE_SIZE) + return -EINVAL; + data = memdup_user_nul(buf, count); if (IS_ERR(data)) return PTR_ERR(data); @@ -2116,6 +2123,9 @@ static ssize_t smk_write_unconfined(struct file *file, const char __user *buf, if (!smack_privileged(CAP_MAC_ADMIN)) return -EPERM; + if (count > PAGE_SIZE) + return -EINVAL; + data = memdup_user_nul(buf, count); if (IS_ERR(data)) return PTR_ERR(data); @@ -2669,6 +2679,10 @@ static ssize_t smk_write_syslog(struct file *file, const char __user *buf, if (!smack_privileged(CAP_MAC_ADMIN)) return -EPERM; + /* Enough data must be present */ + if (count == 0 || count > PAGE_SIZE) + return -EINVAL; + data = memdup_user_nul(buf, count); if (IS_ERR(data)) return PTR_ERR(data); @@ -2761,10 +2775,13 @@ static ssize_t smk_write_relabel_self(struct file *file, const char __user *buf, return -EPERM; /* + * No partial write. * Enough data must be present. */ if (*ppos != 0) return -EINVAL; + if (count == 0 || count > PAGE_SIZE) + return -EINVAL; data = memdup_user_nul(buf, count); if (IS_ERR(data)) -- 2.27.0