Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp7240pxb; Wed, 24 Feb 2021 16:29:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJy+YlFKXuOUmANWc/8a/hna7J3pgb26BEi9DqNDp+LSXco7slYpk5CPpGN173RMe2Ke+Lhc X-Received: by 2002:aa7:c6d5:: with SMTP id b21mr428321eds.180.1614212976351; Wed, 24 Feb 2021 16:29:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614212976; cv=none; d=google.com; s=arc-20160816; b=NIHX0SK9R1F175fsMztRgbywYoHPGjaVdGcx0j97iP+SWLfmh0/JC/66nxYSsPH7zU N2aW1CRIeOcHoN+7YErGqL9584g0PfxDGacon3xVN/ytxPRD2f9obIz+HwHb9BiqzRhA I793XOKqaIWc8oz8j6qv/9iTF9LWkB36AbIEpq61LUmlH6SXCtk1Ur/WWhh4JM7toWE0 JQl7uyuwOK5jstPQcnpGaZH55f4sLAGEz3jeotcDwrLh6L/mWFL5MoTtL+KYqICeZ1QM qV1UNE8A3wirysQ+Dygn385r/G7XP2DP52lCKLyRbGfqpppUOMJH4zhsbEaTeyMLpWzP NTYQ== 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=HIvdOxSDrA0ZKT4ZpDh0Z8Vbnt+nmwFltNzrU2IvoKI=; b=q1hhPhT6pnn9zDb1M9noKG2R3YR6VEdItICFK1a7Lxy9JBDwt9FZrPOAv5QggJ/DVJ bdRMFL17vKu0Xr/SCkRWY9HCMA3JP3S9hEqJ80M0i16eB67EyjslhKHr0NxUnhhOe4do ST3jrWjyrT/wzu/LARqUqPe/Eafm7q7iguAbnHiEL5rJKaBIU7zwhjCWhWSgVnLeNkdL cIp77XkJVCv8SqGZoHm0/noB902T9N22RMH04lnZRvcyNXzi/pc+vLyY7vuq30G7Eh+B B0AbhMDuoW51m9RKbvRH3DYWA+KT61yTMxTCooGuzqGN3fnT2Y1T+X2LSc9ZK/RKksVs 9C6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=X0YUz9bB; 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 y23si2442265eds.278.2021.02.24.16.28.55; Wed, 24 Feb 2021 16:29:36 -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=X0YUz9bB; 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 S237719AbhBXN6X (ORCPT + 99 others); Wed, 24 Feb 2021 08:58:23 -0500 Received: from mail.kernel.org ([198.145.29.99]:58406 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235779AbhBXNGo (ORCPT ); Wed, 24 Feb 2021 08:06:44 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 5F33864F6F; Wed, 24 Feb 2021 12:54:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1614171259; bh=aILvyt+thfgO51TYEgG0XK1pq4MMRzLdDyiGFATa0g8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=X0YUz9bBsD/IQnTXsbnlOGH3N6GMi+kvDlLClmT/A2GKFaR1xmXH/Q9nVuiMU9KeD Mp8cBS191WAuhwc6Wcr2Qe7cgNmCxWM6977ZQHDQz5QE1AiHIpfTdkwZomADZ5ASY7 iWEaFW8s0R5ykeAZgwOYBp5DF78vY07Twxq0UwsVWu2VJWXhoFdnC1dwqMmEmO8hIu gijt64NpimTbxSUKY9Otel0rDDLVgy4O0bb0pkooPXiXQKd/4xLpxOMlbemQtwNpGo mFJLRUICrkPzGohtNCBt/W8hcjt7tUQAMKfmbMQoQtXlPVj37LGneZ7cWUOcbjk8v1 ZnuHBwGe4Gf/Q== 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 5.4 29/40] smackfs: restrict bytes count in smackfs write functions Date: Wed, 24 Feb 2021 07:53:29 -0500 Message-Id: <20210224125340.483162-29-sashal@kernel.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210224125340.483162-1-sashal@kernel.org> References: <20210224125340.483162-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 9c4308077574c..5e75ff2e1b14f 100644 --- a/security/smack/smackfs.c +++ b/security/smack/smackfs.c @@ -1163,7 +1163,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); @@ -1423,7 +1423,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); @@ -1830,6 +1830,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); @@ -2001,6 +2005,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); @@ -2088,6 +2095,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); @@ -2643,6 +2653,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); @@ -2735,10 +2749,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