Received: by 2002:a25:f815:0:0:0:0:0 with SMTP id u21csp852830ybd; Sat, 22 Jun 2019 17:10:28 -0700 (PDT) X-Google-Smtp-Source: APXvYqxmeYCzrZlq6DElEB1HvPSJxFIzjYUWfgN7UTV8ykL3R3T2/p6+5BQPayvtjQibDbaWTH+f X-Received: by 2002:a63:1322:: with SMTP id i34mr25958730pgl.424.1561248628446; Sat, 22 Jun 2019 17:10:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561248628; cv=none; d=google.com; s=arc-20160816; b=bkLvHe5JcAcsZQwOLWXk5eZCVJ2103xKcQR2sI1rCERTkU1ewbxpE6gUTlb1amZlJZ x1JYKcdg+fMfupz3Qh0f76pJQElNke1FOaZdWvSVdVFs2y++h32M64dLYwUBSOGmbtWy XFOQrgSnjis0HQ0/TqzlH8soeSPOoEZsLL7Vx9elNNn2EdNmurC6QcJAYuKT1ddALYkG z12TRbVwwMQDFVGPrgN7N2JX4ePFdyqASpL1hbdfhjXozAX6Q9arZw6rkVS9htmaY4Or y1yngDYh8EQlHblRuG9cZoL/kkxXMietZ/DOXqbbDicImTAOaiOg6z1saeCa6beH6nyk 9eQQ== 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=NG7nomNIPwxqmG4LxRLswXls78O7wQ5+xJa/DwxCyKE=; b=v0Z1SSuIWuHxFcnfm4NYYEL63WvvLbOYOuBkgBUA+hLDK3yybIc/Bb3MMrgSTsCeML GLNDZGr75FJM/szH8buY8ezujpRi3IJzfJJzYNtU+oPgtrjYSChi53twwdrJ5sW5QJPi ZR5oiHNIew2hUrIHXcaFDz9/zLCKPVMe4bFi7wtajwfpr2sjNe2RqGghy1aId/F3s/uz gGeUZfer2TC/BSwkcJiw1mPkJ6m8LBaBL0KP5glJF+3P6W/VVZPTvY9XHgrB9DdLzKAZ C7lnNGifB08FYGRG0/gmDsZPzDbWC2juNxkS2vFYbwm5EXwSnb9ZwRxFjNKyY0YIBTpu kxng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="H/Hji0SY"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id c38si6198464pgc.65.2019.06.22.17.10.10; Sat, 22 Jun 2019 17:10:28 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="H/Hji0SY"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1726599AbfFWAJj (ORCPT + 99 others); Sat, 22 Jun 2019 20:09:39 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:45351 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726452AbfFWAJi (ORCPT ); Sat, 22 Jun 2019 20:09:38 -0400 Received: by mail-pf1-f196.google.com with SMTP id r1so5422569pfq.12 for ; Sat, 22 Jun 2019 17:09:38 -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=NG7nomNIPwxqmG4LxRLswXls78O7wQ5+xJa/DwxCyKE=; b=H/Hji0SYg1fH86DYvsJI7X+8dn5kU6pRmDUpmPkjU13nWthuxcr1seWrhpVyesUTGc McVlNxmMiLfLd5s5iZTniqL7krDztr1k0t2qVKQqqG8VkkYi0/ZYxp80p53z9T0s9i+0 41FhnKUukzsuXpG07A+yrT4id43WUqhW2d1M8= 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=NG7nomNIPwxqmG4LxRLswXls78O7wQ5+xJa/DwxCyKE=; b=mPwNLuopp2w3P+LMPvFvacTekIxZRytEI9wVQLyze/o8LEhYjBg42gbPuhDGdYMo5N hfmnMvQwKtHxiUHo+mLVyZmNMJhX5P/oxxu1xtwDnt45u/vL+EcVY0WcIRJJrR3Jkb1n wnS/sGb+Dppyub4QkKAs25fPxypcWJ2+tWoQCq5cvWmMW1bR8XBnsik4Dk5qk8whRNr+ HamkoHWSeQID5Cuo1whz9lZje6NUQyXutgiat23Pp25/Wh5pYxZNSjzXQgololUt5ZNP Of2+/JhVkyvUlpkaYOAiOI+2RhBb7Nh6P2cnOkzscAMOy/oagD0zrGhVxUTdM2FUTjlN 2gCg== X-Gm-Message-State: APjAAAUxQwLAWK+/Zb14ZWM+m1ci0Zj/aLm2my51ORnOc4rhpulv+YYE md+b7rCVU7m9QOkJCl207DUXDg== X-Received: by 2002:a17:90a:372a:: with SMTP id u39mr15770033pjb.2.1561248577993; Sat, 22 Jun 2019 17:09:37 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id h62sm11402153pgc.54.2019.06.22.17.09.36 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 22 Jun 2019 17:09:36 -0700 (PDT) Date: Sat, 22 Jun 2019 17:09:35 -0700 From: Kees Cook To: Matthew Garrett Cc: jmorris@namei.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, David Howells , Alexei Starovoitov , Matthew Garrett , netdev@vger.kernel.org, Chun-Yi Lee , Daniel Borkmann Subject: Re: [PATCH V34 23/29] bpf: Restrict bpf when kernel lockdown is in confidentiality mode Message-ID: <201906221709.FC3AA888B@keescook> References: <20190622000358.19895-1-matthewgarrett@google.com> <20190622000358.19895-24-matthewgarrett@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190622000358.19895-24-matthewgarrett@google.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jun 21, 2019 at 05:03:52PM -0700, Matthew Garrett wrote: > From: David Howells > > There are some bpf functions can be used to read kernel memory: > bpf_probe_read, bpf_probe_write_user and bpf_trace_printk. These allow > private keys in kernel memory (e.g. the hibernation image signing key) to > be read by an eBPF program and kernel memory to be altered without > restriction. Disable them if the kernel has been locked down in > confidentiality mode. > > Suggested-by: Alexei Starovoitov > Signed-off-by: David Howells Reviewed-by: Kees Cook -Kees > Signed-off-by: Matthew Garrett > cc: netdev@vger.kernel.org > cc: Chun-Yi Lee > cc: Alexei Starovoitov > Cc: Daniel Borkmann > --- > include/linux/security.h | 1 + > kernel/trace/bpf_trace.c | 20 +++++++++++++++++++- > security/lockdown/lockdown.c | 1 + > 3 files changed, 21 insertions(+), 1 deletion(-) > > diff --git a/include/linux/security.h b/include/linux/security.h > index e6e3e2403474..de0d37b1fe79 100644 > --- a/include/linux/security.h > +++ b/include/linux/security.h > @@ -97,6 +97,7 @@ enum lockdown_reason { > LOCKDOWN_INTEGRITY_MAX, > LOCKDOWN_KCORE, > LOCKDOWN_KPROBES, > + LOCKDOWN_BPF_READ, > LOCKDOWN_CONFIDENTIALITY_MAX, > }; > > diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c > index d64c00afceb5..638f9b00a8df 100644 > --- a/kernel/trace/bpf_trace.c > +++ b/kernel/trace/bpf_trace.c > @@ -137,6 +137,10 @@ BPF_CALL_3(bpf_probe_read, void *, dst, u32, size, const void *, unsafe_ptr) > { > int ret; > > + ret = security_locked_down(LOCKDOWN_BPF_READ); > + if (ret) > + return ret; > + > ret = probe_kernel_read(dst, unsafe_ptr, size); > if (unlikely(ret < 0)) > memset(dst, 0, size); > @@ -156,6 +160,12 @@ static const struct bpf_func_proto bpf_probe_read_proto = { > BPF_CALL_3(bpf_probe_write_user, void *, unsafe_ptr, const void *, src, > u32, size) > { > + int ret; > + > + ret = security_locked_down(LOCKDOWN_BPF_READ); > + if (ret) > + return ret; > + > /* > * Ensure we're in user context which is safe for the helper to > * run. This helper has no business in a kthread. > @@ -205,7 +215,11 @@ BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1, > int fmt_cnt = 0; > u64 unsafe_addr; > char buf[64]; > - int i; > + int i, ret; > + > + ret = security_locked_down(LOCKDOWN_BPF_READ); > + if (ret) > + return ret; > > /* > * bpf_check()->check_func_arg()->check_stack_boundary() > @@ -534,6 +548,10 @@ BPF_CALL_3(bpf_probe_read_str, void *, dst, u32, size, > { > int ret; > > + ret = security_locked_down(LOCKDOWN_BPF_READ); > + if (ret) > + return ret; > + > /* > * The strncpy_from_unsafe() call will likely not fill the entire > * buffer, but that's okay in this circumstance as we're probing > diff --git a/security/lockdown/lockdown.c b/security/lockdown/lockdown.c > index 5a08c17f224d..2eea2cc13117 100644 > --- a/security/lockdown/lockdown.c > +++ b/security/lockdown/lockdown.c > @@ -33,6 +33,7 @@ static char *lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX+1] = { > [LOCKDOWN_INTEGRITY_MAX] = "integrity", > [LOCKDOWN_KCORE] = "/proc/kcore access", > [LOCKDOWN_KPROBES] = "use of kprobes", > + [LOCKDOWN_BPF_READ] = "use of bpf to read kernel RAM", > [LOCKDOWN_CONFIDENTIALITY_MAX] = "confidentiality", > }; > > -- > 2.22.0.410.gd8fdbe21b5-goog > -- Kees Cook