From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 07F26C2BA19 for ; Wed, 22 Apr 2020 02:46:41 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C9F86206D5 for ; Wed, 22 Apr 2020 02:46:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C9F86206D5 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=zeniv.linux.org.uk Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 6C6698E0005; Tue, 21 Apr 2020 22:46:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 64FD08E0003; Tue, 21 Apr 2020 22:46:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 53E7F8E0005; Tue, 21 Apr 2020 22:46:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 3719B8E0003 for ; Tue, 21 Apr 2020 22:46:40 -0400 (EDT) Received: from smtpin02.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id F1E1D4DD0 for ; Wed, 22 Apr 2020 02:46:39 +0000 (UTC) X-FDA: 76733952918.02.bone20_5742376c2c314 X-HE-Tag: bone20_5742376c2c314 X-Filterd-Recvd-Size: 3585 Received: from ZenIV.linux.org.uk (zeniv.linux.org.uk [195.92.253.2]) by imf07.hostedemail.com (Postfix) with ESMTP for ; Wed, 22 Apr 2020 02:46:39 +0000 (UTC) Received: from viro by ZenIV.linux.org.uk with local (Exim 4.92.3 #3 (Red Hat Linux)) id 1jR5P8-0081T1-82; Wed, 22 Apr 2020 02:46:26 +0000 Date: Wed, 22 Apr 2020 03:46:26 +0100 From: Al Viro To: Christoph Hellwig Cc: Kees Cook , Iurii Zaikin , Alexei Starovoitov , Daniel Borkmann , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org Subject: Re: [PATCH 5/5] sysctl: pass kernel pointers to ->proc_handler Message-ID: <20200422024626.GI23230@ZenIV.linux.org.uk> References: <20200421171539.288622-1-hch@lst.de> <20200421171539.288622-6-hch@lst.de> <20200421191615.GE23230@ZenIV.linux.org.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200421191615.GE23230@ZenIV.linux.org.uk> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Tue, Apr 21, 2020 at 08:16:15PM +0100, Al Viro wrote: > On Tue, Apr 21, 2020 at 07:15:39PM +0200, Christoph Hellwig wrote: > > Instead of having all the sysctl handlers deal with user pointers, which > > is rather hairy in terms of the BPF interaction, copy the input to and > > from userspace in common code. This also means that the strings are > > always NUL-terminated by the common code, making the API a little bit > > safer. > > > > As most handler just pass through the data to one of the common handlers > > a lot of the changes are mechnical. > > > @@ -564,27 +564,38 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf, > > if (!table->proc_handler) > > goto out; > > > > - error = BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, buf, &count, > > - ppos, &new_buf); > > + if (write) { > > + kbuf = memdup_user_nul(ubuf, count); > > + if (IS_ERR(kbuf)) { > > + error = PTR_ERR(kbuf); > > + goto out; > > + } > > + } else { > > + error = -ENOMEM; > > + kbuf = kzalloc(count, GFP_KERNEL); > > Better allocate count + 1 bytes here, that way a lot of insanity in the > instances can be simply converted to snprintf(). Yes, I know it'll bring > the Church Of Avoiding The Abomination Of Sprintf out of the woodwork, > but... FWIW, consider e.g. net/sunrpc/sysctl.c: Nevermind that the read side should be simply int err = proc_douintvec(table, write, buffer, lenp, ppos); /* Display the RPC tasks on writing to rpc_debug */ if (!err && strcmp(table->procname, "rpc_debug") == 0) rpc_show_tasks(&init_net); return err; the write side would become len = snprintf(buffer, *lenp + 1, "0x%04x\n", *(unsigned int *)table->data); if (len > *lenp) len = *lenp; *lenp -= len; *ppos += len; return 0; and I really wonder if lifting the trailing boilerplate into the caller would've been better. Note that e.g. gems like if (!first) err = proc_put_char(&buffer, &left, '\t'); if (err) break; err = proc_put_long(&buffer, &left, lval, neg); if (err) break; are due to lack of snprintf-to-user; now, lose the "to user" part and we suddenly can be rid of that stuff...