Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp241453ybz; Tue, 21 Apr 2020 19:48:47 -0700 (PDT) X-Google-Smtp-Source: APiQypLe1O04nRFXaPUJFPmubRQrlbE+nXqnwuE/fZtQKuW0SdUpkOwUmol8rt4uWfUmAk7T1+ZB X-Received: by 2002:a05:6402:95e:: with SMTP id h30mr16698684edz.117.1587523727272; Tue, 21 Apr 2020 19:48:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587523727; cv=none; d=google.com; s=arc-20160816; b=GLJDXCBtoaVTzqunoaEnbF47h1cWn8L/pR3h81V5rFIJZ13ni031XU5Y6GF70BvJZA 51ZY3h6Kt7HJp6IdxqgC75mpdmpOfMoAmvjSuNVyBhxS+qK2dqbxlzGNMZ4WbFgGIdAZ M/PUfjxJDcHd3V281QHDNmeQRTTZ3j2AG0HflWeOwQa8a9cs+FfxfV0uEYbPus2FHkhC PZZ71iIos6ZFjVBGYH7woXiU96gIf5ZEX/5YRPGaBv6ifTiUN9UT4hyTSPYrnA9uv/jo I97JBrxQq0k57hIUfQ6Rw5GDU4RbOr57kbvAaP2LICWcYWbnihZPzCwkUGDYEJwoDLfb K8mw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=95koDKFhFYfQZQbRmj9Wiy4jK5I9e1ulkaEid3M8bfE=; b=xoCLIX6UxCTfCEXaSiCZDDfQesJHO9mFMRWSnyWsaY/EQrGBid46/RAIO34ebmGLfJ wrkQ5Wt//xLN+oN80NtVewIxBdPficOK2N/l62gAfln7QqADjMNx6GMhNr4It9yHwbCe KHU/E1a1xPyZSTQRg5imv3fL0cd6sinqe0EUqojRg1j/GTLipH7bZR4LNw12xXE95imn An0WcwaN/rN1tBzVFbnxynid38b3IWoUaZ7TMEYD6SeXI8sH3/pBz1ixQ50ECemdBseP 7D+L9d8KdL2NPl/GhRN12o6Xhn/UWHotwdNNTkFSjYu8pkR++aPa3tClhfyrPac3yv7v f8+A== ARC-Authentication-Results: i=1; mx.google.com; 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 m4si2885430ejc.103.2020.04.21.19.48.24; Tue, 21 Apr 2020 19:48:47 -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; 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 S1726422AbgDVCqi (ORCPT + 99 others); Tue, 21 Apr 2020 22:46:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49794 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1725912AbgDVCqi (ORCPT ); Tue, 21 Apr 2020 22:46:38 -0400 X-Greylist: delayed 27013 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Tue, 21 Apr 2020 19:46:37 PDT Received: from ZenIV.linux.org.uk (zeniv.linux.org.uk [IPv6:2002:c35c:fd02::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BBF37C0610D6; Tue, 21 Apr 2020 19:46:37 -0700 (PDT) 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> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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...