Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp217920pxf; Wed, 31 Mar 2021 22:21:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzhPg7CwteKyWKT7lfSLR/SSyg5r1vKshyp7wPLBImCLfewjlFVVk0IU1n+5Hlr2SgaHU7c X-Received: by 2002:a50:f311:: with SMTP id p17mr8206914edm.188.1617254467853; Wed, 31 Mar 2021 22:21:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617254467; cv=none; d=google.com; s=arc-20160816; b=p9PVP4/SlTakKCZZB7r2T6q41ebcqNTr23q6r531wE8KvTOkCIXBWRcDtwZM27aph/ YNKtPvKfSSzCmxatAHvd2uFq6KCXvl0HlYJtaeueYBnfrAF2S6jb4MxcCxcUzE8b1Caz B0dv54hHyUF8KG/W8iWhGHrXzM9tgWPA5S6nxmAlm8W6wIyXl9il40yIf7rM0gENesa/ qTScfshsl24+vxR7Umg8vI0edEhnPsBR/pvPr1Aho+1ZX/rU5XHpzk8mcgYoFgkHvswl ce9bEcU8bUu0guhZ+lAQOFftvaNTrUDvftSeLs8OSyNaNsMWWiYambhMir3PFHwCZ1oy xPlA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=pYxUWYwria0F2t7xR5J5E6BWgVNzD9JTRB0JCs+5HDw=; b=TJ2PMi+bn2y3BWJL/SONk3v/jVmcvuQAwFMN6KmuwnEANio4GRwyhPFiNBFk9AYWkI BtV/a8qJqdYzUiptNnDwInbewqYBcJKsoLgdbrs4kJHUTLfV+UOyCr1aZjFi0h63hG2s uoVv3SkK5DEGGaauAisN5Nn+zN9D/OquyM5Sqp2EDin0uY27Cjio/+LzoFZ6p4Z4BXXw LA0/xv8Rm2ldeYDQuA1iDD8PnIoxYagTsRFujWCWIl5xeucNl8KvrMzueSHxwc1qzX3X NK9nP1nmZQp1THoJw0XPLd2NshzoxWpdlmTNjAml8QLf1NIOmETwLb+5GlgF+dcQzQG0 bl3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=eANJakmG; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id hr7si3782158ejc.596.2021.03.31.22.20.34; Wed, 31 Mar 2021 22:21:07 -0700 (PDT) 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=@linuxfoundation.org header.s=korg header.b=eANJakmG; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232585AbhDAFRQ (ORCPT + 99 others); Thu, 1 Apr 2021 01:17:16 -0400 Received: from mail.kernel.org ([198.145.29.99]:60856 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230291AbhDAFRA (ORCPT ); Thu, 1 Apr 2021 01:17:00 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 3C54160698; Thu, 1 Apr 2021 05:16:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1617254220; bh=2mRDIp06P0j/bB8232tnYtSW3E01K7ZZc1Nw02ZWyvY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=eANJakmGRxZDb+Xkpg421mz30JzRtYNFWUnykPf8kp/sZKoEOtEErPBGB2HpWyXqx t92s4r9KhaYXa6eTo3TIyafa1BXwR1XenEht3RXQlSQf3csya55JuOiqJclmYP1KVg v/buf82YL6FnQpARpGkTeDBAO96lXob+GzD9MA0Y= Date: Thu, 1 Apr 2021 07:16:56 +0200 From: Greg Kroah-Hartman To: Kees Cook Cc: Andrew Morton , "Rafael J. Wysocki" , Michal Hocko , Alexey Dobriyan , Lee Duncan , Chris Leech , Adam Nichols , linux-fsdevel@vger.kernel.org, linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3] sysfs: Unconditionally use vmalloc for buffer Message-ID: References: <20210401022145.2019422-1-keescook@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210401022145.2019422-1-keescook@chromium.org> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Mar 31, 2021 at 07:21:45PM -0700, Kees Cook wrote: > The sysfs interface to seq_file continues to be rather fragile > (seq_get_buf() should not be used outside of seq_file), as seen with > some recent exploits[1]. Move the seq_file buffer to the vmap area > (while retaining the accounting flag), since it has guard pages that > will catch and stop linear overflows. This seems justified given that > sysfs's use of seq_file already uses kvmalloc(), is almost always using > a PAGE_SIZE or larger allocation, has normally short-lived allocations, > and is not normally on a performance critical path. > > Once seq_get_buf() has been removed (and all sysfs callbacks using > seq_file directly), this change can also be removed. > > [1] https://blog.grimm-co.com/2021/03/new-old-bugs-in-linux-kernel.html > > Signed-off-by: Kees Cook > --- > v3: > - Limit to only sysfs (instead of all of seq_file). > v2: https://lore.kernel.org/lkml/20210315174851.622228-1-keescook@chromium.org/ > v1: https://lore.kernel.org/lkml/20210312205558.2947488-1-keescook@chromium.org/ > --- > fs/sysfs/file.c | 23 +++++++++++++++++++++++ > 1 file changed, 23 insertions(+) > > diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c > index 9aefa7779b29..70e7a450e5d1 100644 > --- a/fs/sysfs/file.c > +++ b/fs/sysfs/file.c > @@ -16,6 +16,7 @@ > #include > #include > #include > +#include > > #include "sysfs.h" > > @@ -32,6 +33,25 @@ static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn) > return kobj->ktype ? kobj->ktype->sysfs_ops : NULL; > } > > +/* > + * To be proactively defensive against sysfs show() handlers that do not > + * correctly stay within their PAGE_SIZE buffer, use the vmap area to gain > + * the trailing guard page which will stop linear buffer overflows. > + */ > +static void *sysfs_kf_seq_start(struct seq_file *sf, loff_t *ppos) > +{ > + struct kernfs_open_file *of = sf->private; > + struct kernfs_node *kn = of->kn; > + > + WARN_ON_ONCE(sf->buf); How can buf ever not be NULL? And if it is, we will leak memory in the next line so we shouldn't have _ONCE, we should always know, but not rebooting the machine would be nice. > + sf->buf = __vmalloc(kn->attr.size, GFP_KERNEL_ACCOUNT); > + if (!sf->buf) > + return ERR_PTR(-ENOMEM); > + sf->size = kn->attr.size; > + > + return NULL + !*ppos; > +} Will this also cause the vmalloc fragmentation/abuse that others have mentioned as userspace can trigger this? And what code frees it? thanks, greg k-h