Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp55769ybm; Wed, 27 May 2020 19:08:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzlP11bgCTmv4r1JWJ189IhAfGycNlMkg5s7CfaAvTuNzrwDpm8nYv/PlmSpYsrazxWDQ6U X-Received: by 2002:a17:906:1dd1:: with SMTP id v17mr996610ejh.395.1590631719834; Wed, 27 May 2020 19:08:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590631719; cv=none; d=google.com; s=arc-20160816; b=EkcV0jbV/RLzUa3Ryed8NH475qsFAJTqTTNd0lR1bptW4TJa478IthFIm/TXgVuMkU R0fYvIlGJYWQYZftxE1gDJsrCF+xrz1mfCwf1Lx37s9MpPyIkAZjnl0WX5Yb83SctDgA 1wl8fbe/BF9COCIyVPAHixO+KEdFxjBRdDjSW0FsBC+usxrF31cGoCWEBeFXCggpq4wh os0qP53DfCT/Qm5SbAzQS/kYla5yuD0txXe0k1eoU3NrTouiotO6jz/rdFrzxMyc0R1l wbgTdq2InYxoTHpN3QKBKfIDLTi0ZR71O7aAY1AYiTnBtvkgSeiBCmJlebUxjPtadIh2 PAeg== 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:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=YL/bhPpg06x3y+8n0dmN86FBqCMqpjWGVejQhqFjSTI=; b=ZqLi5wXNfhDP6h+M6bYqOWM+dmrE54Dswn7gX9VZ4gaKRDtopaQt6Mr3kHY8BZQXtp 3hA97GMplKmNJegjLCdRrNOe4LKTV+fdmf9utKAgoge8sc+IvigPh0F2QrFDUOO+rWRx /pQncm107sZfE+UXmtgf/I9gnenvKHIv/FTY8XmB/WjAnmziFKypCbZbEi5IEI9Bufu/ d7JorFJm/ha2QnCDj3sfuIzH8OEv4S++ZUqOdxEdYxibIwDbzergCthVoxOVpsSGgbIg nX8d2I7LfTh+hvJY3GEr02lLmXjzjH+3DO0ExXbdc1IowkLj1752Nb2thDIrE4wA+7P4 qyuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=P1CBXYMU; 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 r13si2441457edq.64.2020.05.27.19.08.16; Wed, 27 May 2020 19:08:39 -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=@kernel.org header.s=default header.b=P1CBXYMU; 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 S1726451AbgE1CEf (ORCPT + 99 others); Wed, 27 May 2020 22:04:35 -0400 Received: from mail.kernel.org ([198.145.29.99]:54258 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725849AbgE1CEe (ORCPT ); Wed, 27 May 2020 22:04:34 -0400 Received: from localhost.localdomain (c-73-231-172-41.hsd1.ca.comcast.net [73.231.172.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 7583D207CB; Thu, 28 May 2020 02:04:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1590631473; bh=Bm2dTuY2AQspqM7SzMj2c/DT+ozxm6kUi6HWpA8TsHk=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=P1CBXYMU6KgliH8T3/2k6OMetj5IIP9IfCeW6vXHFuAVES38CNoXgmhsLExw5WPRS BGr2sIDNzBzrcfeE+DMAJESNZIIeUKJWWPI8HGY2ObOdmL8XyU7KUun0WVlXahBjEC cDYAJp751q754Z0TTj9fuvNilxFW+islAyRbCf+k= Date: Wed, 27 May 2020 19:04:32 -0700 From: Andrew Morton To: Christoph Hellwig Cc: x86@kernel.org, Alexei Starovoitov , Daniel Borkmann , Masami Hiramatsu , Linus Torvalds , linux-parisc@vger.kernel.org, linux-um@lists.infradead.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 12/23] bpf: handle the compat string in bpf_trace_copy_string better Message-Id: <20200527190432.e4af1fba00c13cb1421f5a37@linux-foundation.org> In-Reply-To: <20200521152301.2587579-13-hch@lst.de> References: <20200521152301.2587579-1-hch@lst.de> <20200521152301.2587579-13-hch@lst.de> X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.31; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 21 May 2020 17:22:50 +0200 Christoph Hellwig wrote: > User the proper helper for kernel or userspace addresses based on > TASK_SIZE instead of the dangerous strncpy_from_unsafe function. > > ... > > --- a/kernel/trace/bpf_trace.c > +++ b/kernel/trace/bpf_trace.c > @@ -331,8 +331,11 @@ static void bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype, > switch (fmt_ptype) { > case 's': > #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE > - strncpy_from_unsafe(buf, unsafe_ptr, bufsz); > - break; > + if ((unsigned long)unsafe_ptr < TASK_SIZE) { > + strncpy_from_user_nofault(buf, user_ptr, bufsz); > + break; > + } > + fallthrough; > #endif > case 'k': > strncpy_from_kernel_nofault(buf, unsafe_ptr, bufsz); Another user of strncpy_from_unsafe() has popped up in linux-next's bpf. I did the below, but didn't try very hard - it's probably wrong if CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE=n? Anyway, please take a look at all the bpf_trace.c changes in linux-next. From: Andrew Morton Subject: bpf:bpf_seq_printf(): handle potentially unsafe format string better User the proper helper for kernel or userspace addresses based on TASK_SIZE instead of the dangerous strncpy_from_unsafe function. Cc: Christoph Hellwig Cc: Alexei Starovoitov Cc: Daniel Borkmann Cc: "H. Peter Anvin" Cc: Ingo Molnar Cc: Masami Hiramatsu Cc: Thomas Gleixner Signed-off-by: Andrew Morton --- kernel/trace/bpf_trace.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) --- a/kernel/trace/bpf_trace.c~xxx +++ a/kernel/trace/bpf_trace.c @@ -588,15 +588,22 @@ BPF_CALL_5(bpf_seq_printf, struct seq_fi } if (fmt[i] == 's') { + void *unsafe_ptr; + /* try our best to copy */ if (memcpy_cnt >= MAX_SEQ_PRINTF_MAX_MEMCPY) { err = -E2BIG; goto out; } - err = strncpy_from_unsafe(bufs->buf[memcpy_cnt], - (void *) (long) args[fmt_cnt], - MAX_SEQ_PRINTF_STR_LEN); + unsafe_ptr = (void *)(long)args[fmt_cnt]; + if ((unsigned long)unsafe_ptr < TASK_SIZE) { + err = strncpy_from_user_nofault( + bufs->buf[memcpy_cnt], unsafe_ptr, + MAX_SEQ_PRINTF_STR_LEN); + } else { + err = -EFAULT; + } if (err < 0) bufs->buf[memcpy_cnt][0] = '\0'; params[fmt_cnt] = (u64)(long)bufs->buf[memcpy_cnt]; _