Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp3914554ybz; Mon, 4 May 2020 12:03:42 -0700 (PDT) X-Google-Smtp-Source: APiQypL6+2ms9/GrbpYCbTkCRua9RUQF7YlCPPAznf4lGqr+qM3bLi3vzScW5WtsydX4NwFF25hP X-Received: by 2002:aa7:cdcb:: with SMTP id h11mr15256615edw.264.1588619022664; Mon, 04 May 2020 12:03:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588619022; cv=none; d=google.com; s=arc-20160816; b=gRaTVbrz7+StwQD0CBPan9XOeUTcU76TqX63NAK6muREnEkvm8j03w8IuGtsaHoxyX MfSYVpL/ELjuw88wDav5rdl62w0LA18MppL4ALW6m3WaVqS2JbYRcbiq5TIrl8owYPz/ PaTx2zUtUGO1dMaTmfY+2RWHa7Wqd+AKvXG+Ljf1jliYqhpRSZdBQqpnTCv+CZUPOMCg znWdUbebVl7wSOsohv5fzFJsTduX+htkFDgFovpAGSdOLpQj+atKe2CPPOHDX9n7ejFx aPfvAvM8Mn3JydPcUum1YhVm0af4ATgpxEiPWwuoCxJcwGIqftK63CWTvSFAnKRLQbpN B+sA== 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 :dkim-signature; bh=nYFjkTQ/J9D4EU63dN0FFikbGAhF+LRtC+WCQwbW2tU=; b=l3cFK2x+RWu8jk9+zSA/sxb/V2YIyKUrnwoF3GFoLOoSZ2ioq5ag5ElivboLD6lUQ4 QUeWHlweyH+bP1cOK8dqswxXu4WJw0lTCFdeGTfR3ODuThwyrUbb6o7D8dZ8Uq7yXBNH oUf5YvsJ4LQpIyhQfA1H3gOtw5LcTiZ9O/JBCLFGCKAYGP+upR8Da88lEkJC00c7dh6x lakKkpVDKhAmU7WAfdfJyCOvKjQmGGafLWRO4+vm4N+aegh1O/Uqk41ZTk/YN2PCInhX 0YFHex6KKzBmxctzWgaQLWcdyOQDjoh8VnR+X6JsTw/Zp9K3RGT9/y+GJGmwKCVmTHEp sRNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=PAODR2kW; 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=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t30si6969209edt.410.2020.05.04.12.03.18; Mon, 04 May 2020 12:03:42 -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=@chromium.org header.s=google header.b=PAODR2kW; 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=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726908AbgEDTBP (ORCPT + 99 others); Mon, 4 May 2020 15:01:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726338AbgEDTBO (ORCPT ); Mon, 4 May 2020 15:01:14 -0400 Received: from mail-pf1-x444.google.com (mail-pf1-x444.google.com [IPv6:2607:f8b0:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17172C061A41 for ; Mon, 4 May 2020 12:01:14 -0700 (PDT) Received: by mail-pf1-x444.google.com with SMTP id r14so6004195pfg.2 for ; Mon, 04 May 2020 12:01:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=nYFjkTQ/J9D4EU63dN0FFikbGAhF+LRtC+WCQwbW2tU=; b=PAODR2kWLIQCPRYqkqyIdFR4k6cxeCTcVW1YMVscR7cA4wClEqMBJy9OpZ1qHUqOSD 7Q9ejnXg70wrUJp9Bz7sjaGojfO30MxpPoyv3mQVm1x4C/y5FRlKfiobLx8iMwGIWr75 IbOlJj8maIV6VMRn4ekVlm2cVBkVcBPVEBI6Y= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=nYFjkTQ/J9D4EU63dN0FFikbGAhF+LRtC+WCQwbW2tU=; b=PBSQvVoyzILFToI9ABmKXrROY6HoCvvwjq92DykYMBe5MD+pOYdYssxyxsTjE4wLoi yDKIK3QxNFslhdSE3Urd+8zrFGcTU1rMdgcUgSeAyKRRLSZivXn0Jb8g9Nac1Ju4b+rd fq2VMI1tEyDvX+OUabbHi9ozMNmbNkO4KkPFyGTrQJ45Ufw1HS7i1F/QuPo3hbdd1AEz ZvwFyIa1iFvWh8EntYbB5OOTaVcX9HFTGmUoA3dqLEuR82RVJMhO3HR8d0OzVoRN3KdM x9a6WP4GV68SrlbVy8BA1vYf/woVXULcXHylSQ4JmwzrCclN9V677HwEkZSMWJcdd0If ij8A== X-Gm-Message-State: AGi0Pua6Jwzb6kGRflVkc2sC4N6mNPt56a+6kkHgKFie7+Gst2+btOBy VlrcZ3S5VyJW2svn6SxkIa3DGQ== X-Received: by 2002:aa7:9297:: with SMTP id j23mr18213294pfa.15.1588618873424; Mon, 04 May 2020 12:01:13 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id q21sm9348194pfg.131.2020.05.04.12.01.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 May 2020 12:01:12 -0700 (PDT) Date: Mon, 4 May 2020 12:01:11 -0700 From: Kees Cook To: Christoph Hellwig Cc: 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, Andrey Ignatov Subject: Re: [PATCH 5/5] sysctl: pass kernel pointers to ->proc_handler Message-ID: <202005041154.CC19F03@keescook> References: <20200424064338.538313-1-hch@lst.de> <20200424064338.538313-6-hch@lst.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200424064338.538313-6-hch@lst.de> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Apr 24, 2020 at 08:43:38AM +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. This is a lovely cleanup; thank you! Tiny notes below... > diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c > index b6f5d459b087d..df2143e05c571 100644 > --- a/fs/proc/proc_sysctl.c > +++ b/fs/proc/proc_sysctl.c > @@ -539,13 +539,13 @@ static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry, > return err; > } > > -static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf, > +static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf, > size_t count, loff_t *ppos, int write) > { > struct inode *inode = file_inode(filp); > struct ctl_table_header *head = grab_header(inode); > struct ctl_table *table = PROC_I(inode)->sysctl_entry; > - void *new_buf = NULL; > + void *kbuf; > ssize_t error; > > if (IS_ERR(head)) > @@ -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); > + if (!kbuf) > + goto out; > + } > + > + error = BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, &kbuf, &count, > + ppos); > if (error) > - goto out; > + goto out_free_buf; > > /* careful: calling conventions are nasty here */ Is this comment still valid after doing these cleanups? > - if (new_buf) { > - mm_segment_t old_fs; > - > - old_fs = get_fs(); > - set_fs(KERNEL_DS); > - error = table->proc_handler(table, write, (void __user *)new_buf, > - &count, ppos); > - set_fs(old_fs); > - kfree(new_buf); > - } else { > - error = table->proc_handler(table, write, buf, &count, ppos); > + error = table->proc_handler(table, write, kbuf, &count, ppos); > + if (error) > + goto out_free_buf; > + > + if (!write) { > + error = -EFAULT; > + if (copy_to_user(ubuf, kbuf, count)) > + goto out_free_buf; > } Something I noticed here that existed in the original code, but might be nice to improve while we're here is to make sure that the "count" returned from proc_handler() cannot grow _larger_, since then we might expose heap memory beyond the end of the allocation. I'll send a patch for this... > > - if (!error) > - error = count; > + error = count; > +out_free_buf: > + kfree(kbuf); > out: > sysctl_head_finish(head); > > [...] > diff --git a/kernel/sysctl.c b/kernel/sysctl.c > index 511543d238794..e26fe7e8e19d7 100644 > --- a/kernel/sysctl.c > +++ b/kernel/sysctl.c > [...] > @@ -682,7 +661,6 @@ static int do_proc_douintvec_w(unsigned int *tbl_data, > left -= proc_skip_spaces(&p); > > out_free: > - kfree(kbuf); > if (err) > return -EINVAL; This label name isn't accurate any more... *shrug* -- Kees Cook