Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp473063pxj; Thu, 13 May 2021 09:09:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxu6iSuMxmhRdHqAx5TPh8TPcdgp/sMasmd0K0fkqNdmC6u617d6gmD6TR1erY+WUv1gs+A X-Received: by 2002:a17:906:3a45:: with SMTP id a5mr45602125ejf.288.1620922169114; Thu, 13 May 2021 09:09:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620922169; cv=none; d=google.com; s=arc-20160816; b=KC/i7XLhYybzgBq8qMWLR5vLTAIdwJxLpWgNRB4jsIUPyAtGZwXZxHpht6Mi8m1fnR vKKv0eXxkUuIB9lTjE4xAYId1XCcqeSmS/1Tw3FEDHxi8wF+Ejfw1XkZnuvDIptD0a3L ZJm2W8muiCw43bjRM7tKwEeOy7mOWVVPn5VtB0EM/lpbOdi2wTc3zOZyqDVm4uw7nWKi J/ljVC4zzWcONDYRxzKzdnYjuXzLiQEH4CZYnfv6ahvKILP30bDerKxv/L79A9gnDV3F eLs0iyaWacDUEj/WCAep5bhoDvSz4cBVvGvoNGSimmHIzuAyiEy/mCOZdBdPCKj91o7/ 0lKw== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=qE75pAUGQVDDCijtyhQQzwQRQrPrtPIyFgbLTPxgxEM=; b=iS4u+2uyh5bw+A2sMDs5ddlI4uQugYJyqip/Uz7rBSE48nFVfEnNgqYIMcIm7P2zRU mKdKTFQkylGegJ/RdQz2p9Y31NtVzCH4UydvgVBLtcjMvzZFAGUqE4YJmQDQxRAOy3gA j9FEoUAYGSfhbtu0mcndm/6C7RIOo7MHfPjHpqUxGwo1oTCZSokF4RTsWKwjr7gWnacK rnZ1lOyhEvfSJ0VltXqO7Ot2IyrDITMYhGjnN4gf8hgW2o8Fpjxyt1T7oAGgyKyiYdqw 7rGN57TAKyfaA6acjXEz29baXIqG41qifzueuO0OBwyw6WY1Ux3tB1v24dJFog9rDYO7 Gj2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=jNrgIeLb; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c14si3756708edr.0.2021.05.13.09.08.50; Thu, 13 May 2021 09:09:29 -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=@infradead.org header.s=casper.20170209 header.b=jNrgIeLb; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235007AbhEMQIq (ORCPT + 99 others); Thu, 13 May 2021 12:08:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235002AbhEMQIp (ORCPT ); Thu, 13 May 2021 12:08:45 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0FFBC061574; Thu, 13 May 2021 09:07:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:In-Reply-To:References; bh=qE75pAUGQVDDCijtyhQQzwQRQrPrtPIyFgbLTPxgxEM=; b=jNrgIeLb/4RcpygXlGApYPgOWs w5ipkSvQBQlD2dTCL8+dTS19yLiTuOZzFo1gLSMl3PGbYtEu8pS66aD3V4oSt5B2iy5hwJ5UBfq2L EwmTTdSpM373PPiHe8yzd2Mcez8/tnKTQHfQ+TgdtSmT7zfiecm5JKmf82wc+4/xPBEKTRDl7V21J zS70VzJ9d1lRe/eeWS/JqzcDIzK64Gw/LNsO8rcB/7L9+7RXd5XPPbl+IaAq6x2pIz0LFIp97yEFW yDhQmThQcDado/hzRYoDyGs9xLY2gb+G7PQpOgDDmgHtHfjDOiZN9WZ3JjhgWWcyJ7Gn3s0QeOjKt nfYdyN/Q==; Received: from willy by casper.infradead.org with local (Exim 4.94 #2 (Red Hat Linux)) id 1lhDrQ-009ZFz-TC; Thu, 13 May 2021 16:06:58 +0000 From: "Matthew Wilcox (Oracle)" To: linux-kernel@vger.kernel.org, Josef Bacik , Christoph Hellwig , Al Viro , Kees Cook Cc: "Matthew Wilcox (Oracle)" , linux-abi@vger.kernel.org, linux-fsdevel@vger.kernel.org, Alexey Dobriyan Subject: [PATCH] sysctl: Limit the size of I/Os to PAGE_SIZE Date: Thu, 13 May 2021 17:06:48 +0100 Message-Id: <20210513160649.2280429-1-willy@infradead.org> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We currently allow a read or a write that is up to KMALLOC_MAX_SIZE. This has caused problems when cat decides to do a 64kB read and so we allocate a 64kB buffer for the sysctl handler to store into. The immediate problem was fixed by switching to kvmalloc(), but it's ridiculous to allocate so much memory to read what is likely to be a few bytes. sysfs limits reads and writes to PAGE_SIZE, and I feel we should do the same for sysctl. The largest sysctl anyone's been able to come up with is 433 bytes for /proc/sys/dev/cdrom/info This will allow simplifying the BPF sysctl code later, but I'll leave that for someone who understands it better. Signed-off-by: Matthew Wilcox (Oracle) --- fs/proc/proc_sysctl.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c index dea0f5ee540c..a97a8a4ff270 100644 --- a/fs/proc/proc_sysctl.c +++ b/fs/proc/proc_sysctl.c @@ -562,11 +562,14 @@ static ssize_t proc_sys_call_handler(struct kiocb *iocb, struct iov_iter *iter, if (!table->proc_handler) goto out; - /* don't even try if the size is too large */ + /* reads may return short values; large writes must fail now */ + if (count >= PAGE_SIZE) { + if (write) + goto out; + count = PAGE_SIZE; + } error = -ENOMEM; - if (count >= KMALLOC_MAX_SIZE) - goto out; - kbuf = kvzalloc(count + 1, GFP_KERNEL); + kbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); if (!kbuf) goto out; @@ -582,12 +585,12 @@ static ssize_t proc_sys_call_handler(struct kiocb *iocb, struct iov_iter *iter, if (error) goto out_free_buf; - /* careful: calling conventions are nasty here */ error = table->proc_handler(table, write, kbuf, &count, &iocb->ki_pos); if (error) goto out_free_buf; if (!write) { + /* Give BPF the chance to override a read result here? */ error = -EFAULT; if (copy_to_iter(kbuf, count, iter) < count) goto out_free_buf; @@ -595,7 +598,7 @@ static ssize_t proc_sys_call_handler(struct kiocb *iocb, struct iov_iter *iter, error = count; out_free_buf: - kvfree(kbuf); + kfree(kbuf); out: sysctl_head_finish(head); -- 2.30.2