From patchwork Sat Feb 17 11:41:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13561341 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (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 CA1DD1B7F6 for ; Sat, 17 Feb 2024 11:42:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708170128; cv=none; b=ROymFF/xzX1Q2jX2XfXrCqUcRmxpGeqBNUXVQynCbxcMFpe0x/KgfwFG6w8DHxvm/V/H4jOpxbAtkI0cZvo9OrrCmA/Cv3QEhj3MktrL/IqBmvqfTC1dZSezpVGwmFfkevp9zZ8wGnCPPI5sYqKA8AiZC2NoG1UovAtloZsXN7k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708170128; c=relaxed/simple; bh=UYaKBGdBoCemQFpWQtWxvuILlxsTdGNWtG/C/2IhZIs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UBAOnOyEPVuGAwA72I8JhKX6Ec/b4OLjszGuZncNau+XMB0I+Q0dKlYduzUEWnWMQtFtXP8EFU80fYDbO7tsC0T21h9UG+n/sXbXlcDjjbgF9I8rsTeZEEr0LNfGE+HCjaikhKMkFBWnFw1bF9BcCGXBbCmwyXI3Pe+tA/wyO8s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=USFtXrxr; arc=none smtp.client-ip=209.85.214.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="USFtXrxr" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1d73066880eso27945875ad.3 for ; Sat, 17 Feb 2024 03:42:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708170126; x=1708774926; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ZHWPcVTnDwtDUKMBTkT9ukaWA+mMoJGUQ8CGQfJH/kg=; b=USFtXrxropiTaLcDNOpxgxOVCTLkhyCZNgbxdsxVYCGJvrUUnh9brz8cgGKWD3xX3j LpcGvxqGpn/vMxIOpEqlyXtxrv/VxFVJiFBU2l6QgxS0duFQ4pmxqnRUcD7UpOQi3sOO uP4bOUa7LGxP7fwnly9zzamjt9wRnvpIvX4hZrWobKgZqA/le9KYNcz6PYos36RzbD3A RMR/x8MwHu3elyDkYTaA/qL7aYrUQ0WDl5Yuxmke2leoZgE7EIPFmS+NWc+Hj5a267bX eoFu/M2tL+VUsIGsc8YCpznnBF6ZdN35v6xiFb3aI7oXHn9yYuU2TZ4ompbZsnD80EWp erog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708170126; x=1708774926; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZHWPcVTnDwtDUKMBTkT9ukaWA+mMoJGUQ8CGQfJH/kg=; b=R1RUBFIvNtuNG2lfxDzqyV1KQH5llfRT1OcsNRcRPKIA+wzjWNiqvqZlZFHyYmK0ri v6clz6M2LNQCETWec/CPK0a0fwrT47S/6X5HyaELcfii1F6t3tbbtpTDYCwXVVEDNNAF uACJpqhXJ8Rm5g1urfjqolb/TEFH9NOiBhj6w2KYU5mQgC+S5WCkPhzwH+GWq00OxJl9 LQ2lL0CzupFKoMWhMcNDbxi2nCLsIhrJ5/0hZt45xowvFIBGpHx8th/sN3JmJ/1klUmJ yPapaxGChjN5JFrmQfuMcVqkD5Nd8PXN5bkAOAha0YTTPQAQ6w3HI9obGB5/Qs/R+H4a EsXA== X-Gm-Message-State: AOJu0YzjEikyMagYiXgEDcJ6tbElNpdOjrT0yv2PqSaVVJBAjoQrwrzU InW0PhscTxcTRLi4TA+e0R5ei70No8BzXRo8JX+Lex8Axie4zbLt X-Google-Smtp-Source: AGHT+IFlbRB9CvsUTlr8pgfVc/mCY0lIkPk0Ug+AHxDf+DoboXeN6KYWrQAIVTU7mfmZ/ugwn7iziA== X-Received: by 2002:a17:903:2a90:b0:1da:933:fb39 with SMTP id lv16-20020a1709032a9000b001da0933fb39mr9415242plb.39.1708170126192; Sat, 17 Feb 2024 03:42:06 -0800 (PST) Received: from localhost.localdomain ([39.144.45.19]) by smtp.gmail.com with ESMTPSA id m20-20020a170902f21400b001d8f6ae51aasm1307201plc.64.2024.02.17.03.42.01 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 17 Feb 2024 03:42:05 -0800 (PST) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, Yafang Shao , Chuyi Zhou , Oleg Nesterov Subject: [PATCH v2 bpf-next 1/2] bpf: Fix an issue due to uninitialized bpf_iter_task Date: Sat, 17 Feb 2024 19:41:51 +0800 Message-Id: <20240217114152.1623-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240217114152.1623-1-laoar.shao@gmail.com> References: <20240217114152.1623-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Failure to initialize it->pos, coupled with the presence of an invalid value in the flags variable, can lead to it->pos referencing an invalid task, potentially resulting in a kernel panic. To mitigate this risk, it's crucial to ensure proper initialization of it->pos to NULL. Fixes: ac8148d957f5 ("bpf: bpf_iter_task_next: use next_task(kit->task) rather than next_task(kit->pos)") Signed-off-by: Yafang Shao Acked-by: Yonghong Song Cc: Chuyi Zhou Cc: Oleg Nesterov Acked-by: Oleg Nesterov --- kernel/bpf/task_iter.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c index e5c3500443c6..ec4e97c61eef 100644 --- a/kernel/bpf/task_iter.c +++ b/kernel/bpf/task_iter.c @@ -978,6 +978,8 @@ __bpf_kfunc int bpf_iter_task_new(struct bpf_iter_task *it, BUILD_BUG_ON(__alignof__(struct bpf_iter_task_kern) != __alignof__(struct bpf_iter_task)); + kit->pos = NULL; + switch (flags) { case BPF_TASK_ITER_ALL_THREADS: case BPF_TASK_ITER_ALL_PROCS: From patchwork Sat Feb 17 11:41:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13561342 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) (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 6B5F663407 for ; Sat, 17 Feb 2024 11:42:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708170134; cv=none; b=Tc0QLhqN+grD4LAcCvnSReZkOlMUr2xBg4xABzcujDYIFgbWqUB+OtyhU5hEKgDY63v/AdCPDYDc4pcsvtLgJ3M/f5GAPVlvAFiE/VjAlM/c5EqzMg4Y9tcKl1U/OFM/u/xUY4OtnLke+hY9KOk9DoetQtae0IKrNbNuI1vGiq0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708170134; c=relaxed/simple; bh=UgSHNW8lssCl1G9AZjbvSGCwOQyWHiD28zM9BeFYB5k=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ltlHnqORNbmEi3btT2I+xlnvZidhgWotkysTNfV7j2kf3ngV4Kvfb+ZKYFRUiNvF5T/6R9YSJdyy51gNQiNRAdt36ef59FW8l2MuMDx99na2VI3xYwdZj4G7LWJZKZRKhRQ4EXC4t/MvFwF9/rlZO4JCBA9p1KQD9VpT697fhqI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=l+xMLNTQ; arc=none smtp.client-ip=209.85.215.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="l+xMLNTQ" Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-5d3912c9a83so2135626a12.3 for ; Sat, 17 Feb 2024 03:42:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708170132; x=1708774932; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YloBOO91rudoAeMOs6kzXhUv6qEy0P5+n2VgX0+i3jU=; b=l+xMLNTQKWv65yX26EJu0H3Aiq85/tZE2IR1Amq6LGZqh1BikQ/oaZ/lrqoQ0jmxAt KRBVO+tgZiaekcywa6QupM2b3dwSXvvVVOAhQOEpL2IZhtGYs4PbxZU9Bx05YdPQvuG3 q/boZqzZERi0QoG4MHeOySPszW2ddy2bmYH5yBW10kHrikA/UooS3av/ReHm5S+R3d8K Bo2OrNstgPKJH3uoP7vaEQXVpCjFZRR0cjt0Yi/LPKMF6MD9/13G+CrBOZOER0bu3cb1 JdpoFc0nUYX023p0hwazoTKHDdP73fwVjLvN/UAID52SbiEYYkE0sDubPxA8tEYvJZk+ OSXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708170132; x=1708774932; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YloBOO91rudoAeMOs6kzXhUv6qEy0P5+n2VgX0+i3jU=; b=MOnTKORYIZhiThcPBbCTA8E8W152neAOo7afxz+Aalf1jMlYec4dqtxWwx9XkzENqz RZQl3GbIIRYp+ito5Ei4LFAn2gdNZkkARlGHvtK698sRiYJ6ZNL5FShA+cKBtwwGI7RS k1WiHehKG/jxIgnh1Kc4JChHUbRT1RtmFx9F2W/JNq901DCD0/z+ym21fLg9BF2BsS4u ilLmQzLoDcpvFgsOs11KzkXiG84Kya5K997j7tEMDE6wvQeZjdzSkoboOSi8SM/elCmb nhzVxp2n5olIAg999WCXk5GSXNQdFl3QdHTSrewu9XMz0XxdT8PmpLkVfsK1BCGVmx8B hZOw== X-Gm-Message-State: AOJu0Yy6tJJEdBy0/Q+EAkiq4FdShS4AYwKhRIbHgAd5bvCCwoB9kwHb PBXt84tb644SDCx78O9oPt75qvKsr159r29daJ8CyB+hx6akXFxw X-Google-Smtp-Source: AGHT+IF/yoqSp/2RH0+qFwygpin4L5kwHoBYuTDn4Av3jI5DpmmcXUAOYAlaypFCt0gkKWi9JbHe8Q== X-Received: by 2002:a17:902:c949:b0:1db:d13d:6bf3 with SMTP id i9-20020a170902c94900b001dbd13d6bf3mr607597pla.62.1708170132631; Sat, 17 Feb 2024 03:42:12 -0800 (PST) Received: from localhost.localdomain ([39.144.45.19]) by smtp.gmail.com with ESMTPSA id m20-20020a170902f21400b001d8f6ae51aasm1307201plc.64.2024.02.17.03.42.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 17 Feb 2024 03:42:12 -0800 (PST) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, Yafang Shao , Chuyi Zhou Subject: [PATCH v2 bpf-next 2/2] selftests/bpf: Add negtive test cases for task iter Date: Sat, 17 Feb 2024 19:41:52 +0800 Message-Id: <20240217114152.1623-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240217114152.1623-1-laoar.shao@gmail.com> References: <20240217114152.1623-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Incorporate a test case to assess the handling of invalid flags or task__nullable parameters passed to bpf_iter_task_new(). Prior to the preceding commit, this scenario could potentially trigger a kernel panic. However, with the previous commit, this test case is expected to function correctly. Signed-off-by: Yafang Shao Cc: Chuyi Zhou --- tools/testing/selftests/bpf/prog_tests/iters.c | 1 + tools/testing/selftests/bpf/progs/iters_task.c | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/prog_tests/iters.c b/tools/testing/selftests/bpf/prog_tests/iters.c index bf84d4a1d9ae..3c440370c1f0 100644 --- a/tools/testing/selftests/bpf/prog_tests/iters.c +++ b/tools/testing/selftests/bpf/prog_tests/iters.c @@ -193,6 +193,7 @@ static void subtest_task_iters(void) ASSERT_EQ(skel->bss->procs_cnt, 1, "procs_cnt"); ASSERT_EQ(skel->bss->threads_cnt, thread_num + 1, "threads_cnt"); ASSERT_EQ(skel->bss->proc_threads_cnt, thread_num + 1, "proc_threads_cnt"); + ASSERT_EQ(skel->bss->invalid_cnt, 0, "invalid_cnt"); pthread_mutex_unlock(&do_nothing_mutex); for (int i = 0; i < thread_num; i++) ASSERT_OK(pthread_join(thread_ids[i], &ret), "pthread_join"); diff --git a/tools/testing/selftests/bpf/progs/iters_task.c b/tools/testing/selftests/bpf/progs/iters_task.c index c9b4055cd410..e4d53e40ff20 100644 --- a/tools/testing/selftests/bpf/progs/iters_task.c +++ b/tools/testing/selftests/bpf/progs/iters_task.c @@ -10,7 +10,7 @@ char _license[] SEC("license") = "GPL"; pid_t target_pid; -int procs_cnt, threads_cnt, proc_threads_cnt; +int procs_cnt, threads_cnt, proc_threads_cnt, invalid_cnt; void bpf_rcu_read_lock(void) __ksym; void bpf_rcu_read_unlock(void) __ksym; @@ -26,6 +26,16 @@ int iter_task_for_each_sleep(void *ctx) procs_cnt = threads_cnt = proc_threads_cnt = 0; bpf_rcu_read_lock(); + bpf_for_each(task, pos, NULL, ~0U) { + /* Below instructions shouldn't be executed for invalid flags */ + invalid_cnt++; + } + + bpf_for_each(task, pos, NULL, BPF_TASK_ITER_PROC_THREADS) { + /* Below instructions shouldn't be executed for invalid task__nullable */ + invalid_cnt++; + } + bpf_for_each(task, pos, NULL, BPF_TASK_ITER_ALL_PROCS) if (pos->pid == target_pid) procs_cnt++;