Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp871236ybk; Wed, 13 May 2020 15:38:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyb4VfGPhoUxxTPTECOpif9xxjNIYewPioSKb7a1vaCkTqxyzTd1BhU67BD2bSGusuFdwtk X-Received: by 2002:a17:906:c113:: with SMTP id do19mr1247370ejc.286.1589409534647; Wed, 13 May 2020 15:38:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589409534; cv=none; d=google.com; s=arc-20160816; b=r9uLUD2QuB8/S5lxGg3Lg7azbMeMDTrXBJ9/l81DTXPPvhZjYjPBI3oUHySlftGPFW rFzY6m2wqwxsbJL/kS9LbmVmaxED5m+4HHrVy42JGAWdJBN34qsFrBi43aiKkYNwt32S r6KWj41i4+UWpFmjX1cy11H2hyFIyWwacglcK9Y2jItXOMKkQSYqYfmuDI1CaO9jDghK uQaThdOGZT5YD5oSUTAqHZHulCJ/lZlpYr/bOZrIl1wacTx4bjNuEnMrpQxWtr+5WHoX YnzmWKo0f1wTkEUrCdAn2/fBX3FVb4bEkaZfJDuay3uFwg3YYQ8Pn+5Bsf2VEUK12Tlp ScCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=kOcxEMOAy2HMgSU/8BEDS+d32pMkwsGCrFijI7oJ16A=; b=wGFW676hM/n11K/iWg8ACzujP7/9FMua4/wK/pc9ExTiClJCSWsTDEAZ6cB6XYiSu1 PAlNoIqBkC0OI+oAfQqoOSm5J+ZGmGLEqVJExAGLbV9ITHv464SyPByyF81Qj+QKRcqi 80PwfTaCV5Pk2NH4BA1xoVkWZY0uyL3THT62xHox0/h9FNOv4TIH9BuE3qJJvMMqIuVz Xz8UuATJoDFtbkETqAoHodyDvuVs0hUP3fmV/8jICZXZVjyM5kqoGL86IR/OJMtTgPln Ez744ey0JpTjb/1ehHoqYia0lMUtqXoJWjvU1tvDHcwANRdoHMv6ShzqiT/td4+sZL5n SMiA== 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 cm14si507331edb.546.2020.05.13.15.38.31; Wed, 13 May 2020 15:38:54 -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 S1729708AbgEMWge (ORCPT + 99 others); Wed, 13 May 2020 18:36:34 -0400 Received: from www62.your-server.de ([213.133.104.62]:60982 "EHLO www62.your-server.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726034AbgEMWgd (ORCPT ); Wed, 13 May 2020 18:36:33 -0400 Received: from sslproxy03.your-server.de ([88.198.220.132]) by www62.your-server.de with esmtpsa (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89_1) (envelope-from ) id 1jYzzJ-0003yk-NJ; Thu, 14 May 2020 00:36:29 +0200 Received: from [178.196.57.75] (helo=pc-9.home) by sslproxy03.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jYzzJ-000Svu-Af; Thu, 14 May 2020 00:36:29 +0200 Subject: Re: [PATCH 11/18] maccess: remove strncpy_from_unsafe To: Christoph Hellwig , Linus Torvalds Cc: the arch/x86 maintainers , Alexei Starovoitov , Masami Hiramatsu , Andrew Morton , linux-parisc@vger.kernel.org, linux-um , Netdev , bpf@vger.kernel.org, Linux-MM , Linux Kernel Mailing List References: <20200513160038.2482415-1-hch@lst.de> <20200513160038.2482415-12-hch@lst.de> <20200513192804.GA30751@lst.de> From: Daniel Borkmann Message-ID: <0c1a7066-b269-9695-b94a-bb5f4f20ebd8@iogearbox.net> Date: Thu, 14 May 2020 00:36:28 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: <20200513192804.GA30751@lst.de> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Authenticated-Sender: daniel@iogearbox.net X-Virus-Scanned: Clear (ClamAV 0.102.2/25811/Wed May 13 14:11:53 2020) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 5/13/20 9:28 PM, Christoph Hellwig wrote: > 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. It's used for both. Given this is enabled on pretty much all program types, my assumption would be that usage is still more often on kernel memory than user one.