From mboxrd@z Thu Jan 1 00:00:00 1970 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 6F3DB176FDF for ; Wed, 17 Apr 2024 23:36:11 +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=1713396972; cv=none; b=LFPvY0d8I/DFmdDOBdHYxosXJLeqkxULeP8CLaOvHKdf+bkQ8MDNtpq6TPP7toMC/pHKfTjidBxK04L/JEhpKsncu4uADisaKOQxEsDkFIaKZ56jZGvFvzCmchet/Xe8zsukNRu0XCJIchOtblSUuE8gjyxMwG9wyH3wAg/6lyM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713396972; c=relaxed/simple; bh=Gy+v3kHc6gUJFOVz0cecKLWhrUu0SiEkWzcElEXVN2Y=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=S+pWK4AS1qzrh6xrEW4OYMSxCGSDhLM4cIJj1tIMr9S4TtcWBJwQRWZhruZFg7O3pVRRP3wYq0f8XAQJ/nEPq9QNphWir1SDvYVqxN2R3GUTJ4q7WmCQ/dJdR3RH9ME18EPmgl3QgAVn9NLX1cnsFGsX93nJstQLUlRyI+6V9Zk= 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=yC6nk6Zd; 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="yC6nk6Zd" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-de45c577ca9so824268276.0 for ; Wed, 17 Apr 2024 16:36:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1713396970; x=1714001770; 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=Cs92mryat0OzgsnGDmpmqjunv7BtT58JcaK8X9mxrOg=; b=yC6nk6Zd3WcMYzL6BE7wfY5N2nO70MIxTp0qTwezhzjc98+/K2D3s7Y5FKdaIDjG9l YW37oVlf4klsdOaf4YF5+PVBnEYdLvlQnYcTKX+1IVcHnuwbSmMoWRiIwDCe4xtMDoL9 sJHJ38IBY2uLwtGtXtZzj88xQBqY7Rj4tduJflbpxT+Jed4xtirWTDNlcyp6DKjsDLfG A2lES1+5OCo2yVJ4f1pTfXIRDJrsSSU34BAqlpzWoFLS21Oy2dClCfbJ8iVq5qO+U6YJ 9rBdSYwGdK2OaE8VZNQxKAIwr2dmvAYfZJemOTpuuvrLJi7CL1BDkm0L9IK6cfCsAygD a57w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713396970; x=1714001770; 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=Cs92mryat0OzgsnGDmpmqjunv7BtT58JcaK8X9mxrOg=; b=m8ZR+xBlU3zYxih8WW4qhlxr/d+Z0xuy/KMtzyipjFO6c+FQk+NiB5nNXP0R6NDEry 66vKCGYLMmbOni8jpj+f5i5hOeMfPmsMMcChNOL4pIWxsUuhEcha9HViH4831B/k5dys DvrdJardZZWYR3CJ+k1KW3G81M76Ap88j/SgZHI36MQw0YnrkzbZZxg5u8ucQFYOpLwd kB6+l8oMw82r/pQzXrWo4URnfScjlTIw2V4zTrAWT3Yl87HS+yVI5vMA0jXbeLqWoupF OpS4R1oIPaDlAmqbbUNOfSE02Tf8B47pbjCDCwSnnGm8vcR1Rtzuo/KGSgQkzj7+d2L7 BZiQ== X-Gm-Message-State: AOJu0Yyro1M6fvAQHveDz4uO4AVTuC0jMx9XeSBI1VEG3ylIXiIxcR96 EOSqI1WnGNnzj7wWYtoo1ZqCbSBjE6ocfsFO3ksDeVUZPCFjn5kyg5c+UWka8j3ozswEKwNYq7S ViqMlOfrfxWVvSkd4EL+BR88TSiWUO8FGvI47hzcJyfFjycG6CkFw3JqybNTNoVajk5dbaBJtHg h2fkEzOh4EeXh5A4TjTrbOOWfkAN2tOeU6 X-Google-Smtp-Source: AGHT+IHEhF7lDtoLx34mV+jWNMhTS0HLGFkeJKmUJfFO1OhU3vwakHQ1bDQ0bBfedBtzlbUtNTylxJhjpPg= X-Received: from edliaw.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:305d]) (user=edliaw job=sendgmr) by 2002:a05:6902:1883:b0:dd9:2782:d1c6 with SMTP id cj3-20020a056902188300b00dd92782d1c6mr173619ybb.1.1713396970482; Wed, 17 Apr 2024 16:36:10 -0700 (PDT) Date: Wed, 17 Apr 2024 23:35:06 +0000 In-Reply-To: <20240417233517.3044316-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> <20240417233517.3044316-1-edliaw@google.com> X-Mailer: git-send-email 2.44.0.769.g3c40516874-goog Message-ID: <20240417233517.3044316-5-edliaw@google.com> Subject: [PATCH 5.15.y 4/5] bpf: Fix out of bounds access for ringbuf 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, tr3e.wang@gmail.com Content-Type: text/plain; charset="UTF-8" From: Daniel Borkmann Both bpf_ringbuf_submit() and bpf_ringbuf_discard() have ARG_PTR_TO_ALLOC_MEM in their bpf_func_proto definition as their first argument. They both expect the result from a prior bpf_ringbuf_reserve() call which has a return type of RET_PTR_TO_ALLOC_MEM_OR_NULL. Meaning, after a NULL check in the code, the verifier will promote the register type in the non-NULL branch to a PTR_TO_MEM and in the NULL branch to a known zero scalar. Generally, pointer arithmetic on PTR_TO_MEM is allowed, so the latter could have an offset. The ARG_PTR_TO_ALLOC_MEM expects a PTR_TO_MEM register type. However, the non- zero result from bpf_ringbuf_reserve() must be fed into either bpf_ringbuf_submit() or bpf_ringbuf_discard() but with the original offset given it will then read out the struct bpf_ringbuf_hdr mapping. The verifier missed to enforce a zero offset, so that out of bounds access can be triggered which could be used to escalate privileges if unprivileged BPF was enabled (disabled by default in kernel). Fixes: 457f44363a88 ("bpf: Implement BPF ring buffer and verifier support for it") Reported-by: (SecCoder Security Lab) Signed-off-by: Daniel Borkmann Acked-by: John Fastabend Acked-by: Alexei Starovoitov (cherry picked from commit 64620e0a1e712a778095bd35cbb277dc2259281f) Signed-off-by: Edward Liaw --- kernel/bpf/verifier.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 14813fbebc9f..3dfc45ed428a 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5153,9 +5153,15 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, case PTR_TO_BUF: case PTR_TO_BUF | MEM_RDONLY: case PTR_TO_STACK: + /* Some of the argument types nevertheless require a + * zero register offset. + */ + if (arg_type == ARG_PTR_TO_ALLOC_MEM) + goto force_off_check; break; /* All the rest must be rejected: */ default: +force_off_check: err = __check_ptr_off_reg(env, reg, regno, type == PTR_TO_BTF_ID); if (err < 0) -- 2.44.0.769.g3c40516874-goog