From mboxrd@z Thu Jan 1 00:00:00 1970 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 AE7D6C139 for ; Thu, 18 Apr 2024 01:07:46 +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=mATrJh2YWjEGUK6z8qVXMFQiOevfsbVjHN2EiOMLrE8+mUkNIlh/bQM7Zppgs5SyJprsimFadF4Wz98z2iCFrERRlEvIs9kzmE2VwY45C1VAp+4aLaoNimNiceZlPPjsTh7lKJSGfJU/eU7hCMI+guviPfGxzhHcOfa4kmfkQuc= 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=3zlUL53G; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="3zlUL53G" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2a2fdf6eb3bso432753a91.1 for ; Wed, 17 Apr 2024 18:07:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1713402466; x=1714007266; 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=3zlUL53GYrQQVBA8OYZL9i3FfPwxwKewyXimi9tOZ96GONwRJDuKPMQ00Z2alMSTSI EE4WPfCJeEgEa3cacElVP7F562Vi0/MWmc9unN+epmhgSihCcq/ZQe4L33BtK/1bh7Kn vJEwCNpq6cqtd8qKr00ttZg92oYKFUwmg/IrlCLjmt6RAZHP8sLRSKTaUXH/Mr1BtJEU dE+WBVNC8lh8D+KHwNns2gQBzUo8R+LHcLKAV2H3YW0VOhLyazVeoUBs8rVKQFLW9mlf +w6DqGw/hwXujXKJnzCnHh7iLrYVLaISDVUsteVa+wnzhJq07xcdH6Yv7gvCYbZeREN/ KTaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713402466; x=1714007266; 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=qkw8ivjyRw2b7M8NZYCsdr2DKTkAwWtCRokfTxm7cK9jJ8hjyjbXhPtM7LY/iTkMYQ VP9BBBVUYwPeLFmLO7WrhM7VtPhLJVMTCYzAHFRT0NBT383fG2kRjrnQyNN2nl+a8SIe GeDYRo2JFyWDK2pZvlNl3UtWXZz1hD4cs3s9Q95P4tdSKMs1MWX+sHjmL4MMJFcuCgBE htGqT5WhLX6q8Ij3rIJjWUDx1zx+//n+WC1bynLUKr3Evq4wgfSkQ9+KF89BSwITR8hy fnA9hbOOZsa5ueFJaP5LaCLz5UYRSXpT1RB4JCKNDkO+MnSGK1WaHoscJyodwhdmrDbi YQ+A== X-Gm-Message-State: AOJu0YzTAIki6GQsf7+/wSP6wptOhM8Ahq73p8ZB5FYXZ/I2orbReMEq wc6s6gfOhW5wEv1OxT8j+nk8673ODm23V56inxJHVT5X7a/xhPAs0e7eDZBCJE2FHarlpzni/Mm l+/d8dznR9zxkpxELjerycc1RGdfX3+yPmOXGhPsBt8dWYqs9TsBpWPwRXlYi+PaarRTf4BZH3f y0OV7l7bPMJnzLvm8Lf1WnJWHblIlAe3b2 X-Google-Smtp-Source: AGHT+IHGZwUXxnIi8umgtcmk1AYnoU9sLsjsWPwJKD9HL4ufshZxqeVmZB8Cu9P+R7pMCrRGbgVEpjMIq+c= 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: stable@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