From nobody Wed Feb 11 01:25:45 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BF1AD7EF04 for ; Thu, 18 Apr 2024 23:20:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713482446; cv=none; b=al1XeP+ESu6mLmPqubOqTw83DpEaSjiP6i5Slr6l93/HkoetpxCucgVUl34gLOZzRDWzPysG9C/lZrUduv1EFdo0dPfdZq4rnFxEmCd+mA1GQDWOHfmsHRRPO3iPp/mmdImGkVgWaotBPxygyRrp7xuWzOG7nMUSPlppm8LkjB8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713482446; c=relaxed/simple; bh=zL0IOoCobZDTZUsmgY7nEkobUjMzIyGPYXzHlOIP8G0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qawkyzZ1gy9ufhTALGkKRwoQez+aVsCfqrAAkJ4l986tIII8DWWBSeVs2dV6twIzgvEwl38bbjNpM5zHul5kEqqqdu2eQLDSqtTlvVYmuVImhrIUiXKRoVpI9CxGl3XNDSQ+d39KR1rcLQo9jXENZF0NS3CAms52rqEGL4wccOc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--edliaw.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ttOHZScW; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--edliaw.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ttOHZScW" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2a517457163so1589844a91.3 for ; Thu, 18 Apr 2024 16:20:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1713482444; x=1714087244; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=kPcj1Y72oL0YYRAb0yR7vLtHAgm/BnGjHu6ktOeso8Y=; b=ttOHZScW0ujDrq2DfIxckZlOlLOZMzhZtWidnNPMN6UiswgVfHVeTBH2kNZ+PD0500 JchMGSuFGr5Y/TiAC+KPCYc24Hw+goHBHFvEUYhk7l4AhCxjNKD2vmw+GwgRUDwBxbI+ WmhZXpdrbXoA9A1Zxkgwij2IJsYIT1NNf15ZL1bqVFTcBNdRs13AaGYA0eGLFqCbxQZw LMtNyeiLIUHnR0O72/qta+00h/aTYv4HdDYsBfPSZ5xkhd8LoatHCz6ns/M54Imi5lgR t/oaXFP4OXN29exKLNQVk4dR+l2mwh8xFv1b9L4b22IZBBY9bpb3mfD0WKrVha1kZ7F0 SH8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713482444; x=1714087244; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=kPcj1Y72oL0YYRAb0yR7vLtHAgm/BnGjHu6ktOeso8Y=; b=Mm0OKE3tmXytegsxNJW4875HFxCJ+ZF8NY4cFGA6GqIpOxvSViKAGc+dsu1DAH6eQv 3QIH8eCHJGTzcYKsZhHy+/0iH3/Il2t7F2XcHeXexios2RDt8A2p12tUhqFCizJ+rJpC kBbBkrDd/kqhKcLcakMMmjOifiVrpKtSKyg7vuHJgsj6n+iI15AwuMB5J8Ot0CwV4ivl RtsYbOAdR9P9YZkaPHgyZaibbWsDphoUpkRMbE/rFipiOw3pl7Nql66Utvy0OMlcllaK QNvJRQSGp+sTi0a9jJ/2HSDlTTW8tn3KuyU+sJSF5QR4hnshIPEWUu99pdLVe3+OKKH8 G+Gw== X-Forwarded-Encrypted: i=1; AJvYcCWDkYd4HOiwfqJ90VhPs+n+7l6suqpj4/VfhzJT7ydZ0gTOe/3Mi+OThAt/osihOSY5Rx21WZIFDHhIpWMkV8W4ViGXQdgQBiKXZGjy X-Gm-Message-State: AOJu0YyrNblaycBhmNEpQD4JsdLv6q8caCqOR2FzZhmLYDA8iRtKH6GK obKcz4xsfD8xM/sVEifKz5fU0v5XD09lx9nnP0k+xeXTfUxKibZpQK3aidtEjtliUuR+IUMlQpG Nyw== X-Google-Smtp-Source: AGHT+IEEzy9e5oCLBFWlB6Une2LnpsXUA9NbXktsIkQ6j7clNh0qpXI1n6gnG31DeYQV8W7REduewhCPvyA= X-Received: from edliaw.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:305d]) (user=edliaw job=sendgmr) by 2002:a17:90a:46c8:b0:2ab:e1ae:d4c4 with SMTP id x8-20020a17090a46c800b002abe1aed4c4mr2052pjg.5.1713482444029; Thu, 18 Apr 2024 16:20:44 -0700 (PDT) Date: Thu, 18 Apr 2024 23:19:51 +0000 In-Reply-To: <20240418232005.34244-1-edliaw@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <16430256912363@kroah.com> <20240418232005.34244-1-edliaw@google.com> X-Mailer: git-send-email 2.44.0.769.g3c40516874-goog Message-ID: <20240418232005.34244-6-edliaw@google.com> Subject: [PATCH 5.15.y v3 5/5] bpf: Fix ringbuf memory type confusion when passing to helpers From: Edward Liaw To: stable@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa Cc: bpf@vger.kernel.org, kernel-team@android.com, Edward Liaw , Yonghong Song , linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Daniel Borkmann The bpf_ringbuf_submit() and bpf_ringbuf_discard() have ARG_PTR_TO_ALLOC_MEM in their bpf_func_proto definition as their first argument, and thus both e= xpect the result from a prior bpf_ringbuf_reserve() call which has a return type = of RET_PTR_TO_ALLOC_MEM_OR_NULL. While the non-NULL memory from bpf_ringbuf_reserve() can be passed to other helpers, the two sinks (bpf_ringbuf_submit(), bpf_ringbuf_discard()) right = now only enforce a register type of PTR_TO_MEM. This can lead to potential type confusion since it would allow other PTR_TO= _MEM memory to be passed into the two sinks which did not come from bpf_ringbuf_= reserve(). Add a new MEM_ALLOC composable type attribute for PTR_TO_MEM, and enforce t= hat: - bpf_ringbuf_reserve() returns NULL or PTR_TO_MEM | MEM_ALLOC - bpf_ringbuf_submit() and bpf_ringbuf_discard() only take PTR_TO_MEM | ME= M_ALLOC but not plain PTR_TO_MEM arguments via ARG_PTR_TO_ALLOC_MEM - however, other helpers might treat PTR_TO_MEM | MEM_ALLOC as plain PTR_T= O_MEM to populate the memory area when they use ARG_PTR_TO_{UNINIT_,}MEM in th= eir func proto description Fixes: 457f44363a88 ("bpf: Implement BPF ring buffer and verifier support f= or it") Reported-by: Alexei Starovoitov Signed-off-by: Daniel Borkmann Acked-by: John Fastabend Acked-by: Alexei Starovoitov (cherry picked from commit a672b2e36a648afb04ad3bda93b6bda947a479a5) Signed-off-by: Edward Liaw --- include/linux/bpf.h | 9 +++++++-- kernel/bpf/verifier.c | 6 +++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index df15d4d445dd..74a26cabc084 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -321,7 +321,12 @@ enum bpf_type_flag { */ MEM_RDONLY =3D BIT(1 + BPF_BASE_TYPE_BITS), =20 - __BPF_TYPE_LAST_FLAG =3D MEM_RDONLY, + /* MEM was "allocated" from a different helper, and cannot be mixed + * with regular non-MEM_ALLOC'ed MEM types. + */ + MEM_ALLOC =3D BIT(2 + BPF_BASE_TYPE_BITS), + + __BPF_TYPE_LAST_FLAG =3D MEM_ALLOC, }; =20 /* Max number of base types. */ @@ -405,7 +410,7 @@ enum bpf_return_type { RET_PTR_TO_SOCKET_OR_NULL =3D PTR_MAYBE_NULL | RET_PTR_TO_SOCKET, RET_PTR_TO_TCP_SOCK_OR_NULL =3D PTR_MAYBE_NULL | RET_PTR_TO_TCP_SOCK, RET_PTR_TO_SOCK_COMMON_OR_NULL =3D PTR_MAYBE_NULL | RET_PTR_TO_SOCK_COMMO= N, - RET_PTR_TO_ALLOC_MEM_OR_NULL =3D PTR_MAYBE_NULL | RET_PTR_TO_ALLOC_MEM, + RET_PTR_TO_ALLOC_MEM_OR_NULL =3D PTR_MAYBE_NULL | MEM_ALLOC | RET_PTR_TO_= ALLOC_MEM, RET_PTR_TO_BTF_ID_OR_NULL =3D PTR_MAYBE_NULL | RET_PTR_TO_BTF_ID, =20 /* This must be the last entry. Its purpose is to ensure the enum is diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 33fb379b9f58..67b325427022 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -573,6 +573,8 @@ static const char *reg_type_str(struct bpf_verifier_env= *env, =20 if (type & MEM_RDONLY) strncpy(prefix, "rdonly_", 16); + if (type & MEM_ALLOC) + strncpy(prefix, "alloc_", 16); =20 snprintf(env->type_str_buf, TYPE_STR_BUF_LEN, "%s%s%s", prefix, str[base_type(type)], postfix); @@ -5157,6 +5159,7 @@ static const struct bpf_reg_types mem_types =3D { PTR_TO_MAP_KEY, PTR_TO_MAP_VALUE, PTR_TO_MEM, + PTR_TO_MEM | MEM_ALLOC, PTR_TO_BUF, }, }; @@ -5174,7 +5177,7 @@ static const struct bpf_reg_types int_ptr_types =3D { static const struct bpf_reg_types fullsock_types =3D { .types =3D { PTR_TO= _SOCKET } }; static const struct bpf_reg_types scalar_types =3D { .types =3D { SCALAR_V= ALUE } }; static const struct bpf_reg_types context_types =3D { .types =3D { PTR_TO_= CTX } }; -static const struct bpf_reg_types alloc_mem_types =3D { .types =3D { PTR_T= O_MEM } }; +static const struct bpf_reg_types alloc_mem_types =3D { .types =3D { PTR_T= O_MEM | MEM_ALLOC } }; static const struct bpf_reg_types const_map_ptr_types =3D { .types =3D { C= ONST_PTR_TO_MAP } }; static const struct bpf_reg_types btf_ptr_types =3D { .types =3D { PTR_TO_= BTF_ID } }; static const struct bpf_reg_types spin_lock_types =3D { .types =3D { PTR_T= O_MAP_VALUE } }; @@ -5337,6 +5340,7 @@ static int check_func_arg(struct bpf_verifier_env *en= v, u32 arg, case PTR_TO_MAP_VALUE: case PTR_TO_MEM: case PTR_TO_MEM | MEM_RDONLY: + case PTR_TO_MEM | MEM_ALLOC: case PTR_TO_BUF: case PTR_TO_BUF | MEM_RDONLY: case PTR_TO_STACK: --=20 2.44.0.769.g3c40516874-goog