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=-3.9 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, 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 470DEC4724C for ; Mon, 4 May 2020 19:01:16 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id EF36A206C0 for ; Mon, 4 May 2020 19:01:15 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="PAODR2kW" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EF36A206C0 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 666868E0071; Mon, 4 May 2020 15:01:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5EF3C8E0058; Mon, 4 May 2020 15:01:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B7A98E0071; Mon, 4 May 2020 15:01:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0164.hostedemail.com [216.40.44.164]) by kanga.kvack.org (Postfix) with ESMTP id 330028E0058 for ; Mon, 4 May 2020 15:01:15 -0400 (EDT) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id DC5B0180AD83B for ; Mon, 4 May 2020 19:01:14 +0000 (UTC) X-FDA: 76779954468.30.room28_7f4114d66dc5f X-HE-Tag: room28_7f4114d66dc5f X-Filterd-Recvd-Size: 6446 Received: from mail-pf1-f196.google.com (mail-pf1-f196.google.com [209.85.210.196]) by imf01.hostedemail.com (Postfix) with ESMTP for ; Mon, 4 May 2020 19:01:14 +0000 (UTC) Received: by mail-pf1-f196.google.com with SMTP id 18so5985562pfx.6 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=VZDPFSvjmoS4zpvqJ+dWTK7rAU1kIopdu84yl7oLJaWV0ewB928EOdFtDheiLvyWGs QuuvcxLAEzyLaNGGqf2Q13J0z/4CiI6i3rj1LaFSNJMlZWYfrYtZU8dYMPZFuOuxoxUt aFxiVziLh6DIJTyOTWDfSHNnyj7JjFFajDaH1gBJYmQdrMbRTVgdWoDOcYTRePd/2zOf SZ09DmXxAwXklgr4qMMwP/053rtDvRhhWNEav1SCwRnAaENAbZ5EyP8xQ0gKSxpKB8m6 HDxGM4rFqbBjbcbSTKRKv4/CyLURcBc5LzeJDai9rERwVKKBqmmniGCnaTB9R+w0AArN UaOQ== X-Gm-Message-State: AGi0PuY1ZKwHWaMK4qXrYeyoaBczcNzYPl0fAu4NvxyBvW4dDG/MSTQt XmGCy9PZOUfv64MqQDjbymwcWQ== X-Google-Smtp-Source: APiQypL++aZZkzcir41Et1you9KunQZkaeZXnSeZAMZdvN+uRXbMIhhCUTrk8vcGvmD9Ym6nnNHuDA== 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> 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 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