From nobody Mon Feb 9 15:26:42 2026 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 15E22175552 for ; Wed, 17 Apr 2024 23:36:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713396969; cv=none; b=QvZJufQtsgnlft4OkW1AFaZEOA/ZsUMyFSuCg2LW5HaWwd36IGZDBssNBEpCXeitAAA8dtfoYkvaPVqBa0OZho3gl1cTXQwEEaJEhDWJT1EKIGDZroVebpumBuZ7UoMKq2+qI+vpPPvzWnkqkqVZVEgpY7ujeS2Z/LKqDBppDmc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713396969; c=relaxed/simple; bh=MbU7YSyF5IXinkJkjcfT0u+2sLXGMeKghessXGBFpX0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=i2X2eX/gRsxJ3qkgOx74RKp6S8MTeYb8cCjikq9xq7LzhmJXjghilg21pYwmBl6TQeRcFq5KyQJFTbGUKR0vsPH2Nj21RSufFmXEHnO12IQaCR8ubeQpfykGIkqdt4WI8WcFMBgAaOaPje6/wQo1UyslgiGnU/m4QYI6fNJKXKE= 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=n8cnXciS; arc=none smtp.client-ip=209.85.219.201 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="n8cnXciS" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-de465caa098so249264276.2 for ; Wed, 17 Apr 2024 16:36:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1713396967; x=1714001767; 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=ftfV9j6OBLIDvLqAVZnPAyLe0oFUXv4e5neRndjdTGc=; b=n8cnXciS0NWXEmuCHwCVNG0JcHytbOjl5JtXjLPkRNiaEoPXSDlAxoe0akxxOEH/Ks q17AUtn0CDdaSWdiDK3tnqeI1OE6PBEITBu4/GDqBIceIDWdLGVY6EBbcix3EhjONEMM DjTGPekHN98uCcfkGlQ+Fk1J/8fIS7meNXSFvT8Qxi28eLPn1AtcnaiGJ+EgOBTqlFQj Ms7gZBWl6r6ybvtm2ShbMQqUdhnDIDcyPdaemt3h7BzsGD5gDvRvaiSs+ii8hzZZhmKb UGpH0W6Yd23cRPTqEaB3Migs4KLSQVzW6+XhE/9UfByRCtVlgkbnAShSlyIuCv7FzIyR bUNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713396967; x=1714001767; 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=ftfV9j6OBLIDvLqAVZnPAyLe0oFUXv4e5neRndjdTGc=; b=fmzZTHRbRyKhtzwTWgsHplFcAH8qOckNqwlAn0niNLx7/a0pOWUjEWmRuVCFEBqKA2 VqG8h7GRMQNqHrERz5rDLjilOS9wVygXMKGzqECGQZ0STm9RD0zVOgtfZ3PWglIZUcGx 2SA0/wJ6ry+fIVZfGx0lwEEGj/iCJUrdUV+p/XBv7wNy+chwLTls0bAzcOQJnHgZVNTN Axb8lp8dRTxVflKtuKTRo+zlnilHQPpAUZnRVLjZR8InsRaJnpYUILsuR+LirY0ZBSGp d3kflhqvAmlW81ppjX5Xnj6J/3+moNqlR+41w+YvQNYYzMk3b3Bim09sA4cWiaA4e/sM FUvg== X-Forwarded-Encrypted: i=1; AJvYcCVl8v6HyBHYPduqonYbKbRczNT5Fr9hyK13LU+h8QSoedCvynpYijXHvn+HBVc82W/0wOKQAutv/cUqb+eMFLi35IdgW6OnDhayUST6 X-Gm-Message-State: AOJu0YxTaJl7Mh/RmqE/cwSEo/8KeXOjakyDshlw3hC5p9VeWhPyrnPG 3/hlOhViQE4OuhHyvxjgi3wv1OqdmAEnB0Whowp6tLcaCe2kzPyBHY748DGv0xm8Bkbjb5JLSv8 olg== X-Google-Smtp-Source: AGHT+IEn/8F93q0gEr21PW94bWzVTKkwmkezMWmYKeDuPJa/ADXWCDOC22uKKgLsxTzcprae7vj45cH0Sdo= X-Received: from edliaw.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:305d]) (user=edliaw job=sendgmr) by 2002:a05:6902:1543:b0:dcc:8927:7496 with SMTP id r3-20020a056902154300b00dcc89277496mr99387ybu.5.1713396967138; Wed, 17 Apr 2024 16:36:07 -0700 (PDT) Date: Wed, 17 Apr 2024 23:35:05 +0000 In-Reply-To: <20240417233517.3044316-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> <20240417233517.3044316-1-edliaw@google.com> X-Mailer: git-send-email 2.44.0.769.g3c40516874-goog Message-ID: <20240417233517.3044316-4-edliaw@google.com> Subject: [PATCH 5.15.y 3/5] bpf: Generally fix helper register offset check 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Daniel Borkmann Right now the assertion on check_ptr_off_reg() is only enforced for register types PTR_TO_CTX (and open coded also for PTR_TO_BTF_ID), however, this is insufficient since many other PTR_TO_* register types such as PTR_TO_FUNC do not handle/expect register offsets when passed to helper functions. Given this can slip-through easily when adding new types, make this an expl= icit allow-list and reject all other current and future types by default if this= is encountered. Also, extend check_ptr_off_reg() to handle PTR_TO_BTF_ID as well instead of duplicating it. For PTR_TO_BTF_ID, reg->off is used for BTF to match expect= ed BTF ids if struct offset is used. This part still needs to be allowed, but = the dynamic off from the tnum must be rejected. Fixes: 69c087ba6225 ("bpf: Add bpf_for_each_map_elem() helper") Fixes: eaa6bcb71ef6 ("bpf: Introduce bpf_per_cpu_ptr()") Signed-off-by: Daniel Borkmann Acked-by: John Fastabend Acked-by: Alexei Starovoitov (cherry picked from commit 6788ab23508bddb0a9d88e104284922cb2c22b77) Signed-off-by: Edward Liaw --- kernel/bpf/verifier.c | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 61b8a9c69b1c..14813fbebc9f 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -3792,14 +3792,15 @@ static int get_callee_stack_depth(struct bpf_verifi= er_env *env, } #endif =20 -int check_ptr_off_reg(struct bpf_verifier_env *env, - const struct bpf_reg_state *reg, int regno) +static int __check_ptr_off_reg(struct bpf_verifier_env *env, + const struct bpf_reg_state *reg, int regno, + bool fixed_off_ok) { /* Access to this pointer-typed register or passing it to a helper * is only allowed in its original, unmodified form. */ =20 - if (reg->off) { + if (!fixed_off_ok && reg->off) { verbose(env, "dereference of modified %s ptr R%d off=3D%d disallowed\n", reg_type_str(env, reg->type), regno, reg->off); return -EACCES; @@ -3817,6 +3818,12 @@ int check_ptr_off_reg(struct bpf_verifier_env *env, return 0; } =20 +int check_ptr_off_reg(struct bpf_verifier_env *env, + const struct bpf_reg_state *reg, int regno) +{ + return __check_ptr_off_reg(env, reg, regno, false); +} + static int __check_buffer_access(struct bpf_verifier_env *env, const char *buf_info, const struct bpf_reg_state *reg, @@ -5080,12 +5087,6 @@ static int check_reg_type(struct bpf_verifier_env *e= nv, u32 regno, kernel_type_name(btf_vmlinux, *arg_btf_id)); return -EACCES; } - - if (!tnum_is_const(reg->var_off) || reg->var_off.value) { - verbose(env, "R%d is a pointer to in-kernel struct with non-zero offset= \n", - regno); - return -EACCES; - } } =20 return 0; @@ -5140,10 +5141,26 @@ static int check_func_arg(struct bpf_verifier_env *= env, u32 arg, if (err) return err; =20 - if (type =3D=3D PTR_TO_CTX) { - err =3D check_ptr_off_reg(env, reg, regno); + switch ((u32)type) { + case SCALAR_VALUE: + /* Pointer types where reg offset is explicitly allowed: */ + case PTR_TO_PACKET: + case PTR_TO_PACKET_META: + case PTR_TO_MAP_KEY: + case PTR_TO_MAP_VALUE: + case PTR_TO_MEM: + case PTR_TO_MEM | MEM_RDONLY: + case PTR_TO_BUF: + case PTR_TO_BUF | MEM_RDONLY: + case PTR_TO_STACK: + break; + /* All the rest must be rejected: */ + default: + err =3D __check_ptr_off_reg(env, reg, regno, + type =3D=3D PTR_TO_BTF_ID); if (err < 0) return err; + break; } =20 skip_type_check: --=20 2.44.0.769.g3c40516874-goog