Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp6480666ybi; Wed, 31 Jul 2019 15:23:27 -0700 (PDT) X-Google-Smtp-Source: APXvYqx6kAEzijYbbTjmghc+L/rxElpfbb/cJEwc8pe0WiA+LTlYr4nzCBhwRCrYtijl5ygnmEgJ X-Received: by 2002:a17:90a:350c:: with SMTP id q12mr5123931pjb.46.1564611807591; Wed, 31 Jul 2019 15:23:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564611807; cv=none; d=google.com; s=arc-20160816; b=c8f4irKVy8clN/bq9eLN2/lIdhRmogUVyKAWnM09YUBsPQcRxPrf4jK5fso4A2MUko Ya+DpKN+lS0yHob9GeITw2Ynfj10gcwJ7GGWvzMcqE4yfEFAnn6Ib80v0ELW+mXTC39E MISQjaN4YiztbLv6MFOD6HZqS+n/cx/s3uR6ZbTKLGyFcVtl1J1XrazsLf6DIr/JrMjv BFBMIZ4ivH1B/9uS4bR1S1/iVGZj+CeTjJ0f/BADAVhuI2/tblS6Q3rwLfCyptt7k6yA Yzrv7cKZz0GpPF+hBS/bhEZp+7oBu3plz+rrlpAUm/qSj75W9wswTg7OU6QoK0uXcakj 2Cug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=8yt0J0pt8aHuQPT7nXOB+XR/9vHKjGA14LTvJs583ko=; b=RGeSn7Uzc1H83MKYPw+V0nbKtCoay1pbLGPlJw4lKf6A15Wx8bdfFL18XOlhJ25QRU xhWd01clzAvdbETBhBWYHn+MWJwX7lkS4ogziNUyrqO0Lr3OrBes+p0qwooKwiob1YZY RzJroH1zioHdM1wqWQaCkC+N+xdrkjS26sJlv1dTFR9+gn57e0pmfkYDyxBcj73zy6xl 7sAdLBSDZAW6rxtWbuNlgyCZjO1xcvLkoE1JdNRnmYcNVqprZsdhPTLI45tiRXdNf7xC gPKov8nr+Xpq/qDKQnDnKXZh8qxsz/y1g+iRLyYF3r9OheH5twSsLI9dTs5SCevsSVYL FFNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=qaM4FD9h; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id gn3si29314123plb.321.2019.07.31.15.23.12; Wed, 31 Jul 2019 15:23:27 -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=@google.com header.s=20161025 header.b=qaM4FD9h; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731390AbfGaWSo (ORCPT + 99 others); Wed, 31 Jul 2019 18:18:44 -0400 Received: from mail-qk1-f201.google.com ([209.85.222.201]:50088 "EHLO mail-qk1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731371AbfGaWR0 (ORCPT ); Wed, 31 Jul 2019 18:17:26 -0400 Received: by mail-qk1-f201.google.com with SMTP id l14so59246382qke.16 for ; Wed, 31 Jul 2019 15:17:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=8yt0J0pt8aHuQPT7nXOB+XR/9vHKjGA14LTvJs583ko=; b=qaM4FD9hTiJEd2YW/5LgkZdUqi2/uuM8SnVUWfcL6vnIqCHEq9Taa7iUdxqe18Wcrv Dn5SENlGYiQb0xgBozezRWkkgmae8+cqjo4xKs+XBU5VuB4o2O4mEJYQN2SMj5IHuPRQ 8EsLmPOLnfbYXxE7Ti3pwPdUjYl3YcH6psOQ0YEk/eHV/9MfDH4OCRrZWN7oH+tXz54z voYa91Ir+BPbk87GQE4+ZorXx+Ti3PMkr5HzwhFEcTN7Y9r+zUuqu5nA+tfF94JVAuoS BqVZgERJ2tAMvDEkAQ1ixhEEXkVZePif6MlzDRDj7Qs/9/iaAfY2jlUrvPyPKYtpgf9E kloA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=8yt0J0pt8aHuQPT7nXOB+XR/9vHKjGA14LTvJs583ko=; b=mBcNc+dHX3BSdQnMH1xWFt7p/wnAvvIyszzt/oQ4hyOjTaBa2UlTdiIa5cIeHdLw1d IDSPpQpGQMdmo81HieydmUJeGcabVH6hWGXrAUwJ03etMIIIH6jFO0l807GrVy7tWFJE CNXP8HSULgjjTIbixEyXDdV/m6EVgaDkbIpApt/0mW8pxAkxupwjBz5duXB/2TzMpdGn xK0X57RI07Mds7z0NDQF1Wvz5mmCB+oJmb+wiTN8OwIE4nThzt4zGtsLWVwRZp5z9mMx 4n0mq+hl1+UZiC6Afrj7uayeRFiZhu/6C8sqqMrNxzzIaQJGqzLC7Nfbqvxuqp553zRt a+6w== X-Gm-Message-State: APjAAAWzMiwuuJM/ihp8w7RpLRm7qrmlt3NecgASkNC29b5waB6XSN+E 6d21N/7K/eL3aCkWKZBXPRvH1Z4ceQq4X6VdRuJY+A== X-Received: by 2002:a37:b0c6:: with SMTP id z189mr81991658qke.208.1564611444988; Wed, 31 Jul 2019 15:17:24 -0700 (PDT) Date: Wed, 31 Jul 2019 15:16:11 -0700 In-Reply-To: <20190731221617.234725-1-matthewgarrett@google.com> Message-Id: <20190731221617.234725-24-matthewgarrett@google.com> Mime-Version: 1.0 References: <20190731221617.234725-1-matthewgarrett@google.com> X-Mailer: git-send-email 2.22.0.770.g0f2c4a37fd-goog Subject: [PATCH V37 23/29] bpf: Restrict bpf when kernel lockdown is in confidentiality mode From: Matthew Garrett To: jmorris@namei.org Cc: linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, David Howells , Alexei Starovoitov , Matthew Garrett , Kees Cook , netdev@vger.kernel.org, Chun-Yi Lee , Daniel Borkmann Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: David Howells bpf_read() and bpf_read_str() could potentially be abused to (eg) allow private keys in kernel memory to be leaked. Disable them if the kernel has been locked down in confidentiality mode. Suggested-by: Alexei Starovoitov Signed-off-by: Matthew Garrett Reviewed-by: Kees Cook 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 | 10 ++++++++++ security/lockdown/lockdown.c | 1 + 3 files changed, 12 insertions(+) diff --git a/include/linux/security.h b/include/linux/security.h index 987d8427f091..8dd1741a52cd 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -118,6 +118,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 ca1255d14576..492a8bfaae98 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -142,8 +142,13 @@ 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 < 0) + goto out; + ret = probe_kernel_read(dst, unsafe_ptr, size); if (unlikely(ret < 0)) +out: memset(dst, 0, size); return ret; @@ -569,6 +574,10 @@ BPF_CALL_3(bpf_probe_read_str, void *, dst, u32, size, { int ret; + ret = security_locked_down(LOCKDOWN_BPF_READ); + if (ret < 0) + goto out; + /* * The strncpy_from_unsafe() call will likely not fill the entire * buffer, but that's okay in this circumstance as we're probing @@ -580,6 +589,7 @@ BPF_CALL_3(bpf_probe_read_str, void *, dst, u32, size, */ ret = strncpy_from_unsafe(dst, unsafe_ptr, size); if (unlikely(ret < 0)) +out: memset(dst, 0, size); return ret; diff --git a/security/lockdown/lockdown.c b/security/lockdown/lockdown.c index 6b123cbf3748..1b89d3e8e54d 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.770.g0f2c4a37fd-goog