Received: by 2002:a05:6500:1b45:b0:1f5:f2ab:c469 with SMTP id cz5csp994643lqb; Wed, 17 Apr 2024 18:09:25 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWbltm6aGlzWRXzCEGZ3S/EFpuqd4uBFm7E79IJy0njqDHm1hjPCrxFt6TKF6lujEFf2f25rIr1KswRCNVVfLfq2mxpmhv+LX3WV6cGfA== X-Google-Smtp-Source: AGHT+IFxN5iAVVPcblep5PAPsbfTeRFOhSPwEJzOygiQeaUDZMpC0Jm752rbWJl6D+Zm+9e2sT+M X-Received: by 2002:a05:622a:54e:b0:434:edcc:1500 with SMTP id m14-20020a05622a054e00b00434edcc1500mr1469845qtx.4.1713402564628; Wed, 17 Apr 2024 18:09:24 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713402564; cv=pass; d=google.com; s=arc-20160816; b=SOBQEf1qq8pEBB0O9FmkFddUkrAxKcSRGsbnYO9jYSEO8UkaCyDGAJuWE2bGEc22ah SaTtKYgfSDaJxdQ/FtO9g9oxCoxrvDF84i/Zpt4Tzbf+E/FMCQLUV1F5c8Phgkelrsc2 w2GBT0H0VtLy2VDoCggEg7X4uXm8ILxO3Kt4G1wtLhRsILD3nPeD1C/hFd5ug3bc2sBg dtvwB2hQ8jdcVhdlXy0LfwRU4TY1wUwgTJgW48b4tC/x8cJyT1bLbCG0MSsuSZGVpQFr UlPCdsDWIEUS9Ke+RzD1iJ0wii5+DvKboN3pfjz/+0qKTV+bG6lqMwDtNqWwJ+nkjjOi zqSw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=cuQpI8q1wJxBD6X4kJwl22uAmYchmA+XE2ed6Sx/XW8=; fh=9RYRIVTK2rFlJwyzBvBYVe+2n8gz9xF1xggANVk9a4w=; b=ZtkeFQ18yLEVSQFFUZEE26zBfrUTYEvDAz3tnGdksIhZo9PdyYuUsn11lkm2Ms3bcQ vEWULWXWtUh4cFRiv/cCaVZ0AlF7W5quyiwncuSeddXUyWJ+O/tK3NVJraUX1Llk3l2H sRmndlrCYx91XuedjcINjXKRqYBPU96hWjTRfXjwHb4XDOBlHOjGdddnNDzjv7LpSWph PqNj7s1ppF18rqGlnDqAljCbU8PlEjq0RAHU0dGLWocsjVpjXYq7fKhQWLOIcSRv2vt7 e+xTKhqqzgjFgrNXyqADewd+HHJPgamgRTCRy7zVjmwl428uxvzCGtcWDBT5cCMenJNW CDEw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=UHH1q+NA; arc=pass (i=1 spf=pass spfdomain=flex--edliaw.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-149380-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-149380-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id ay32-20020a05622a22a000b004379be37878si336885qtb.70.2024.04.17.18.09.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Apr 2024 18:09:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-149380-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=UHH1q+NA; arc=pass (i=1 spf=pass spfdomain=flex--edliaw.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-149380-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-149380-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 26D501C21089 for ; Thu, 18 Apr 2024 01:09:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2BF3925570; Thu, 18 Apr 2024 01:07:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="UHH1q+NA" Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 6CCDF5CB0 for ; Thu, 18 Apr 2024 01:07:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713402468; cv=none; b=XTDH/QcTa1fN2Da+zkZYOCwAzPSoe1CEasuq5HaUfdtHoRcK9uB5shJCaI/3X3unvnwdxULA0T/e1Cr3spHwC3in0apG/Lq4NjPwPch6caSNOaqremYm2a6P3R0cv4kCqMRMuoeJeLQtpdALjYVVqyEVPUgp43JF6MKAWUuSap8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713402468; c=relaxed/simple; bh=vjRJ1U7DKOKEv8zqHNl/1MSWTfJ+WAHC2urS1kF08ZE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mYqYShhf/jcVHCfE/51JcSjV7OBohafPXtSibPDjOGh8XLBrJkDPZJdiSHmLOSljE8StilWEOhYbzTC/artu0tUwptlNik1uNxC8fbddv/fnpF/XzQq9/xK6YOTPVws/tJeaiOuTMHgjfwPZuZ2mLixioy1hGGmWNRKT1WrQ4+o= 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=UHH1q+NA; arc=none smtp.client-ip=209.85.216.73 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 Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2abc5b4f3c7so217590a91.0 for ; Wed, 17 Apr 2024 18:07:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1713402465; x=1714007265; 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=cuQpI8q1wJxBD6X4kJwl22uAmYchmA+XE2ed6Sx/XW8=; b=UHH1q+NAnkAIYmnfTLZbtjKDEB1MeEw/V2YGrHVRgkHNgViYvAcYhGbanCgx1+KyUa mSFsJiFUEggS3i6hBRDxT46S58f8JmbGkcG2+T6NZgnCroX2zcULVWzY3/li6YgWQ88c i9wVom7OjPQSWru/F65Mz3S5CRweovRex77GgbLdeL8p8NR+IzqCAXXPifjv2N1DsmJ4 1xInCgPaK2AApo/85qAuAntj6NYPfJevmg8A88Nbb0i5eTA/i9VXByge+sJ6GkNCmjKR aE3YIbAzCIcSVa4vBW805BJ2xJzSPFdm4WKMv37NanPYvfCqHK9FPMLPFlhb4ZIMvA/+ oyFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713402465; x=1714007265; 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=cuQpI8q1wJxBD6X4kJwl22uAmYchmA+XE2ed6Sx/XW8=; b=ZRxkbQuoRbMANT0wATmWBUCIdD4pkN0rIr4EBPEUPEve7DVlUI+d/mpuaYx8wrRJBK klaCvMN71ro2m+FOXqfEP2+vFwV0Teub9WDIAyO1hkAh+2UjeR+7206uCuBuocyzer0X n0cASJoUiWkYifP0st/obSo79fOjDdI/fJq7ga+M0B4MpdAPinQ+WAFcDuo/xhf4ByPE GCKwdlDaoFtIM8UAHuXZS3Tx9jTOO3lhxxl3A2YJdgNlhkQggVeXdgCd4dnaiYtwDRPG e54SONtFZe+fnfUHGPXnMMeh5GelntpjGzq5eFv3pAdrsQpTfTNMpOFmpJmk0ll6pSbq RJjA== X-Forwarded-Encrypted: i=1; AJvYcCVW/bgWDjH5Mt9kNUIKsQ6FBkChnUFh0GoFZPP/0ejr6sHE5Xg9FZH8uUool6vGoqS1WktdTbKDASDbMBFW6e9tdVT+BFlOE6fxj9Tn X-Gm-Message-State: AOJu0YxS6O0uKv9UEwEeVZKwZ6T/9mPDaLwFl+YLy2RvuA3AOmEkvsOt 3gK1W4717BOAPo8DEyRfxByCB1PmEh9sVbp7eRpczOaHqNCYT1wqWPuEenlOECdLvsEl0gh2WOT lAQ== X-Received: from edliaw.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:305d]) (user=edliaw job=sendgmr) by 2002:a17:90a:d081:b0:2a2:5697:f90d with SMTP id k1-20020a17090ad08100b002a25697f90dmr2975pju.6.1713402463538; Wed, 17 Apr 2024 18:07:43 -0700 (PDT) Date: Thu, 18 Apr 2024 01:07:14 +0000 In-Reply-To: <20240418010723.3069001-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> <20240418010723.3069001-1-edliaw@google.com> X-Mailer: git-send-email 2.44.0.769.g3c40516874-goog Message-ID: <20240418010723.3069001-6-edliaw@google.com> Subject: [PATCH 5.15.y v2 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 , Song Liu , Yonghong Song , John Fastabend , KP Singh , Hao Luo Cc: bpf@vger.kernel.org, kernel-team@android.com, Edward Liaw , netdev@vger.kernel.org, linux-kernel@vger.kernel.org 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 expect 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 that: - bpf_ringbuf_reserve() returns NULL or PTR_TO_MEM | MEM_ALLOC - bpf_ringbuf_submit() and bpf_ringbuf_discard() only take PTR_TO_MEM | MEM_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_TO_MEM to populate the memory area when they use ARG_PTR_TO_{UNINIT_,}MEM in their func proto description Fixes: 457f44363a88 ("bpf: Implement BPF ring buffer and verifier support for 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 84efd8dd139d..96b2aa567d23 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -312,7 +312,12 @@ enum bpf_type_flag { */ MEM_RDONLY = BIT(1 + BPF_BASE_TYPE_BITS), - __BPF_TYPE_LAST_FLAG = MEM_RDONLY, + /* MEM was "allocated" from a different helper, and cannot be mixed + * with regular non-MEM_ALLOC'ed MEM types. + */ + MEM_ALLOC = BIT(2 + BPF_BASE_TYPE_BITS), + + __BPF_TYPE_LAST_FLAG = MEM_ALLOC, }; /* Max number of base types. */ @@ -396,7 +401,7 @@ enum bpf_return_type { RET_PTR_TO_SOCKET_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_SOCKET, RET_PTR_TO_TCP_SOCK_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_TCP_SOCK, RET_PTR_TO_SOCK_COMMON_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_SOCK_COMMON, - RET_PTR_TO_ALLOC_MEM_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_ALLOC_MEM, + RET_PTR_TO_ALLOC_MEM_OR_NULL = PTR_MAYBE_NULL | MEM_ALLOC | RET_PTR_TO_ALLOC_MEM, RET_PTR_TO_BTF_ID_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_BTF_ID, /* 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 3dfc45ed428a..6162ba31a89e 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -567,6 +567,8 @@ static const char *reg_type_str(struct bpf_verifier_env *env, if (type & MEM_RDONLY) strncpy(prefix, "rdonly_", 16); + if (type & MEM_ALLOC) + strncpy(prefix, "alloc_", 16); snprintf(env->type_str_buf, TYPE_STR_BUF_LEN, "%s%s%s", prefix, str[base_type(type)], postfix); @@ -4970,6 +4972,7 @@ static const struct bpf_reg_types mem_types = { PTR_TO_MAP_KEY, PTR_TO_MAP_VALUE, PTR_TO_MEM, + PTR_TO_MEM | MEM_ALLOC, PTR_TO_BUF, }, }; @@ -4987,7 +4990,7 @@ static const struct bpf_reg_types int_ptr_types = { static const struct bpf_reg_types fullsock_types = { .types = { PTR_TO_SOCKET } }; static const struct bpf_reg_types scalar_types = { .types = { SCALAR_VALUE } }; static const struct bpf_reg_types context_types = { .types = { PTR_TO_CTX } }; -static const struct bpf_reg_types alloc_mem_types = { .types = { PTR_TO_MEM } }; +static const struct bpf_reg_types alloc_mem_types = { .types = { PTR_TO_MEM | MEM_ALLOC } }; static const struct bpf_reg_types const_map_ptr_types = { .types = { CONST_PTR_TO_MAP } }; static const struct bpf_reg_types btf_ptr_types = { .types = { PTR_TO_BTF_ID } }; static const struct bpf_reg_types spin_lock_types = { .types = { PTR_TO_MAP_VALUE } }; @@ -5150,6 +5153,7 @@ static int check_func_arg(struct bpf_verifier_env *env, 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: -- 2.44.0.769.g3c40516874-goog