Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp4012734ybf; Tue, 3 Mar 2020 17:56:42 -0800 (PST) X-Google-Smtp-Source: ADFU+vvnEXvq04xSvQ4mlcRGtRZPABn1FzjSnOP9ZZ+rlW97G6QVcauy5mVYzAO6F4mym5ygMEmM X-Received: by 2002:a05:6830:1684:: with SMTP id k4mr636038otr.75.1583287001857; Tue, 03 Mar 2020 17:56:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583287001; cv=none; d=google.com; s=arc-20160816; b=BS1xmzHqSuy+b6cNrumECZecYNlqHvPLm3yMKQD9CCbKU0tKiP5OW9E2furK3EgkTy JIksb13zwOMEWrLzmE4rvSXmXzfBAg7XHEf/WZFrhb48t2ob6FVF0VcTqD6Gyb3oOl+e zxmAR7D/eP0Qq48N0A133JQs3ZFv6NWO3+UOfQXzgQQuRlWAeBUD9qdKf7eqQFktxIve gpDZ8rHgV0fGt9sXD5nSUixCPfAg3Ok4I9pj56yXtULbo4jajbDcvgJ/52LHXv+kQOji Hc+rX3UrltIm0fSPSS2JO93KqVkihfeuoA0mYcFv7u3OqHWm3Ww983e123x4y1PxVkEN H7BA== 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:date:subject:cc:to:from :dkim-signature; bh=LFwEDFxwFXmUVLWHUZGmwB/3Av3jfmxGtvvX+N4FdrI=; b=BdZVAg/FkhZl07NhYzeTcP1dDAbet+abLwPe3JBCf7Vaz6SzgGcw14DZt6SVOY2wIa HcQ+CeSqiRXGCIvKueQmh7CsKeywPYLyrlsVsKIDF+fkk7MVtU8ew++WZ43/fxqz9UDK Y6/+/vGfhGTuRwiJeeOJGF80et+y3rw73P8Hc99FDj87uSU8eYhAICx9Qp0CWlO17Zwo LQg8TvDbMnk94aeRSOF3uSRvV7OASQmB8upSW5MyGrd4hUtP1zxqczKTWXmYDoFO5W0l 9oXoNM1XGlvxpLCaAAyxIXlNF6VibXT4TgDIYrSoJ6vvIWBp4iSx6nRbgik9fiKBBTCm z7bg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Qj2MnbGG; 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 k15si244323otb.208.2020.03.03.17.56.30; Tue, 03 Mar 2020 17:56:41 -0800 (PST) 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=Qj2MnbGG; 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 S2387627AbgCDBzp (ORCPT + 99 others); Tue, 3 Mar 2020 20:55:45 -0500 Received: from mail-wm1-f66.google.com ([209.85.128.66]:40248 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387583AbgCDBzm (ORCPT ); Tue, 3 Mar 2020 20:55:42 -0500 Received: by mail-wm1-f66.google.com with SMTP id e26so146593wme.5 for ; Tue, 03 Mar 2020 17:55:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=LFwEDFxwFXmUVLWHUZGmwB/3Av3jfmxGtvvX+N4FdrI=; b=Qj2MnbGGaVof30dTj27lh/GZBYLE3PIXTsAxY968A4WBF2LCnLGMHNx5w536mCOBWJ kWzWp+sIQJ0HY2sfzs6Gv0oivqxnZ48PvtYlJSfAqI0rLyefi4arTvONE9v+n/Wmrnzl 54vExE7YOctQs++jGd+knO1HDUin25BmGNJWI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=LFwEDFxwFXmUVLWHUZGmwB/3Av3jfmxGtvvX+N4FdrI=; b=tI3nR20MU1gwhACF6chqzIZsTqfUHw/BM3PJpGVgzy5aEUQOYUeWmTMwiBxhV4GBis BZ40No9Q+sVvlKsCxGh58+9UsRjsSV0dT0Vb6DL4mIWY5SxtHQGieP+Nv/cdPA8oKa8R XhxVjDNXQcujxE7z4Vj+GeljH6n9Ot3BIQKk8aYlGJbEDqwwfkBmCO20CK/kE0Gc0srs g0wdtHISyy1hhbGvgiN36NtaChw4VPUbd3nND8z0F4SFnsH7GlfMIOATgSlwu5idDsm5 KaFiwHmmdLyRDQ+FPJ54r/fJbc+42J1KR+vV1AbqpQgkpUx+l0qnASsuF97RT69puucP DJOg== X-Gm-Message-State: ANhLgQ0gpp5Rzw405YO73LPQhQZe9BTKg68kLuG4Ye8OoIrkZW3/DUjG ZdJX5+1Q1kE+h9OO/Fhg3UEeIA== X-Received: by 2002:a1c:9dcb:: with SMTP id g194mr623325wme.114.1583286940417; Tue, 03 Mar 2020 17:55:40 -0800 (PST) Received: from kpsingh-kernel.localdomain (77-56-209-237.dclient.hispeed.ch. [77.56.209.237]) by smtp.gmail.com with ESMTPSA id a184sm1475444wmf.29.2020.03.03.17.55.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Mar 2020 17:55:39 -0800 (PST) From: KP Singh To: linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Paul Turner , Jann Horn , Florent Revest , Brendan Jackman Subject: [PATCH bpf-next v2 4/7] bpf: Attachment verification for BPF_MODIFY_RETURN Date: Wed, 4 Mar 2020 02:55:25 +0100 Message-Id: <20200304015528.29661-5-kpsingh@chromium.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200304015528.29661-1-kpsingh@chromium.org> References: <20200304015528.29661-1-kpsingh@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: KP Singh - Allow BPF_MODIFY_RETURN attachment only to functions that are: * Whitelisted by for error injection i.e. by checking within_error_injection_list. Similar disucssions happened for the bpf_overrie_return helper. * security hooks, this is expected to be cleaned up with the LSM changes after the KRSI patches introduce the LSM_HOOK macro: https://lore.kernel.org/bpf/20200220175250.10795-1-kpsingh@chromium.org/ - The attachment is currently limited to functions that return an int. This can be extended later other types (e.g. PTR). Signed-off-by: KP Singh --- kernel/bpf/btf.c | 28 ++++++++++++++++++++-------- kernel/bpf/verifier.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 30841fb8b3c0..50080add2ab9 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -3710,14 +3710,26 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, nr_args--; } - if ((prog->expected_attach_type == BPF_TRACE_FEXIT || - prog->expected_attach_type == BPF_MODIFY_RETURN) && - arg == nr_args) { - if (!t) - /* Default prog with 5 args. 6th arg is retval. */ - return true; - /* function return type */ - t = btf_type_by_id(btf, t->type); + if (arg == nr_args) { + if (prog->expected_attach_type == BPF_TRACE_FEXIT) { + if (!t) + return true; + t = btf_type_by_id(btf, t->type); + } else if (prog->expected_attach_type == BPF_MODIFY_RETURN) { + /* For now the BPF_MODIFY_RETURN can only be attached to + * functions that return an int. + */ + if (!t) + return false; + + t = btf_type_skip_modifiers(btf, t->type, NULL); + if (!btf_type_is_int(t)) { + bpf_log(log, + "ret type %s not allowed for fmod_ret\n", + btf_kind_str[BTF_INFO_KIND(t->info)]); + return false; + } + } } else if (arg >= nr_args) { bpf_log(log, "func '%s' doesn't have %d-th argument\n", tname, arg + 1); diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 2460c8e6b5be..ae32517d4ccd 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "disasm.h" @@ -9800,6 +9801,33 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env) return 0; } +#define SECURITY_PREFIX "security_" + +static int check_attach_modify_return(struct bpf_verifier_env *env) +{ + struct bpf_prog *prog = env->prog; + unsigned long addr = (unsigned long) prog->aux->trampoline->func.addr; + + if (within_error_injection_list(addr)) + return 0; + + /* This is expected to be cleaned up in the future with the KRSI effort + * introducing the LSM_HOOK macro for cleaning up lsm_hooks.h. + */ + if (!strncmp(SECURITY_PREFIX, prog->aux->attach_func_name, + sizeof(SECURITY_PREFIX) - 1)) { + + if (!capable(CAP_MAC_ADMIN)) + return -EPERM; + + return 0; + } + + verbose(env, "fmod_ret attach_btf_id %u (%s) is not modifiable\n", + prog->aux->attach_btf_id, prog->aux->attach_func_name); + + return -EINVAL; +} static int check_attach_btf_id(struct bpf_verifier_env *env) { @@ -10000,6 +10028,9 @@ static int check_attach_btf_id(struct bpf_verifier_env *env) } tr->func.addr = (void *)addr; prog->aux->trampoline = tr; + + if (prog->expected_attach_type == BPF_MODIFY_RETURN) + ret = check_attach_modify_return(env); out: mutex_unlock(&tr->mutex); if (ret) -- 2.20.1