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=-2.2 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 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 F1CC6C433DF for ; Wed, 13 May 2020 19:28:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A3D60206CC for ; Wed, 13 May 2020 19:28:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390613AbgEMT2J (ORCPT ); Wed, 13 May 2020 15:28:09 -0400 Received: from verein.lst.de ([213.95.11.211]:48313 "EHLO verein.lst.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390291AbgEMT2J (ORCPT ); Wed, 13 May 2020 15:28:09 -0400 Received: by verein.lst.de (Postfix, from userid 2407) id 3747968B05; Wed, 13 May 2020 21:28:05 +0200 (CEST) Date: Wed, 13 May 2020 21:28:04 +0200 From: Christoph Hellwig To: Linus Torvalds Cc: Christoph Hellwig , the arch/x86 maintainers , Alexei Starovoitov , Daniel Borkmann , Masami Hiramatsu , Andrew Morton , linux-parisc@vger.kernel.org, linux-um , Netdev , bpf@vger.kernel.org, Linux-MM , Linux Kernel Mailing List Subject: Re: [PATCH 11/18] maccess: remove strncpy_from_unsafe Message-ID: <20200513192804.GA30751@lst.de> References: <20200513160038.2482415-1-hch@lst.de> <20200513160038.2482415-12-hch@lst.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.17 (2007-11-01) Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org On Wed, May 13, 2020 at 12:11:27PM -0700, Linus Torvalds wrote: > On Wed, May 13, 2020 at 9:01 AM Christoph Hellwig wrote: > > > > +static void bpf_strncpy(char *buf, long unsafe_addr) > > +{ > > + buf[0] = 0; > > + if (strncpy_from_kernel_nofault(buf, (void *)unsafe_addr, > > + BPF_STRNCPY_LEN)) > > + strncpy_from_user_nofault(buf, (void __user *)unsafe_addr, > > + BPF_STRNCPY_LEN); > > +} > > This seems buggy when I look at it. > > It seems to think that strncpy_from_kernel_nofault() returns an error code. > > Not so, unless I missed where you changed the rules. I didn't change the rules, so yes, this is wrong. > Also, I do wonder if we shouldn't gate this on TASK_SIZE, and do the > user trial first. On architectures where this thing is valid in the > first place (ie kernel and user addresses are separate), the test for > address size would allow us to avoid a pointless fault due to an > invalid kernel access to user space. > > So I think this function should look something like > > static void bpf_strncpy(char *buf, long unsafe_addr) > { > /* Try user address */ > if (unsafe_addr < TASK_SIZE) { > void __user *ptr = (void __user *)unsafe_addr; > if (strncpy_from_user_nofault(buf, ptr, BPF_STRNCPY_LEN) >= 0) > return; > } > > /* .. fall back on trying kernel access */ > buf[0] = 0; > strncpy_from_kernel_nofault(buf, (void *)unsafe_addr, > BPF_STRNCPY_LEN); > } > > or similar. No? So on say s390 TASK_SIZE_USUALLy is (-PAGE_SIZE), which means we'd alway try the user copy first, which seems odd. I'd really like to here from the bpf folks what the expected use case is here, and if the typical argument is kernel or user memory.