From patchwork Wed Feb 21 07:52:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13565052 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f169.google.com (mail-yw1-f169.google.com [209.85.128.169]) (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 2332B3AC24 for ; Wed, 21 Feb 2024 07:52:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708501942; cv=none; b=izeBIjK2pxkwIaW6G58vocAG1Wt5Cc+UB/immYCxzlarmgXv4BWBwuAoCwxDT9meOhMyKwX61M7X6NxtIFe6t5nTOxdeige37QZprOy4/IKYx3DW9LyoYtDkAzrvl3x7Cflu3Adgq/zczmYKYmWMXmUY9SCvjsqDsMrcBk0Sp40= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708501942; c=relaxed/simple; bh=00z6bws04C82/2aSQhowJadToGiFWd0rd+rEp7aO6Us=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BZViNOHfYtTcpYLzqmDvaNgU37cTG2R2tXcz+cFR/XktWYrSQ8WavDJJX2AYw1SaxE+hmU+PeRSL1wTXRwgWnlHYl/YX0LE3ZrGA/KtYkrxjN0fGfJAGc6g3QP/TkJYBVXWTXS4Slnx8NWoFU+DqVuddc7U2B/U32Dwo5JeP1Uo= 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=FPP/f1JQ; arc=none smtp.client-ip=209.85.128.169 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="FPP/f1JQ" Received: by mail-yw1-f169.google.com with SMTP id 00721157ae682-60883345d73so2420017b3.1 for ; Tue, 20 Feb 2024 23:52:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708501939; x=1709106739; 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=X481Qy+H21udIbTR0RfLzvcKIxylLX8qewST0PUMQFw=; b=FPP/f1JQ2U/Eq5dZiDf3BG+4TPMiBiSifad/A0OjhApZdxD6vslThiYyquIVsrFqgA Q8Gwa6iSVaooKgKoLhNZENNHsTBDVcEu7q4xBbLEJM/2A2cbISEyc1mPso9fMim4Tbh4 EGUXF+GXpCKiikVHvA5LDFoTVKfhLue/ioqF9umQLbeAOJ/TC55zbmAynp5CHKhorW3+ vMaYtDE63LKozp1s0YGKaID+pfQePPGNQVpQQzybtP2LCWKZ7UUN1c6lSEH5NAdx0out 8wn1/cXslGZVZBuMv61HJt5HJeWeLu4l426O7R7j+SOcQ1IwqenJOokBAln0sjGObRxN FY7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708501939; x=1709106739; 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=X481Qy+H21udIbTR0RfLzvcKIxylLX8qewST0PUMQFw=; b=KOBuSNpNRHfyie1imfpKyn/JJ2KwPz++QP+1fm9T/WT5rRy4sELXV2V+LcJH6bBfZR wA5XxGAo41jJqRHx8hb/BTSD92fkkUR1lud7ZdPCYBkINxlxTDgZ2ZIPa/GrAopCZj+F hxCmjkP1ShxbWXoVQ+o9dBufFtMizh6KRLlqD1Nl6WLFrGOYLPOhIdKiRoQUHlSKvs5C rpwp0JbnFHNLZGiuDxFP3m81yl2SIXOLYp/4+9jeoCxXHKA59tCvBUH8RGovSwOFeL3z Un342Ps4dNYvFRmoCIwIQY6oOyHBGFAy/bXqthpXE2qoUMWavS+k/TUGf+DLzQsRM4pG /Qkw== X-Gm-Message-State: AOJu0YwASzdpov2Zk+hL8hQallcXojjErXs3EEmOwAUzNavZI2EEUSO0 Y62NvM++Wn7guMtTbLYrc7ksk2NnnhX0lMJZCObvi+a5sqx9kqX2apXLiiJM X-Google-Smtp-Source: AGHT+IFYxOgnpvOwdN6a1rpQX3diRrXw8IXBZ1IrauGsNiBEXMf9mwSlEu5OAzs3+sM5DkhuFbXLEA== X-Received: by 2002:a81:ae22:0:b0:607:7d2a:f654 with SMTP id m34-20020a81ae22000000b006077d2af654mr17522516ywh.19.1708501939209; Tue, 20 Feb 2024 23:52:19 -0800 (PST) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:26eb:2942:8151:a089]) by smtp.gmail.com with ESMTPSA id m205-20020a8171d6000000b006048e2331fcsm2488715ywc.91.2024.02.20.23.52.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 23:52:18 -0800 (PST) From: thinker.li@gmail.com To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v4 1/3] bpf, net: allow passing NULL prog to check_member. Date: Tue, 20 Feb 2024 23:52:11 -0800 Message-Id: <20240221075213.2071454-2-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240221075213.2071454-1-thinker.li@gmail.com> References: <20240221075213.2071454-1-thinker.li@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 From: Kui-Feng Lee To reuse the check_member function of the bpf_struct_ops structure for checking if an operator is supported, we permit passing a NULL value for the "prog" argument in check_member. The check_member function of the bpf_struct_ops structure will be utilized for checking cfi_stubs in a subsequent patch when registering a struct_ops type. netdev@vger.kernel.org Signed-off-by: Kui-Feng Lee --- net/bpf/bpf_dummy_struct_ops.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/bpf/bpf_dummy_struct_ops.c b/net/bpf/bpf_dummy_struct_ops.c index 02de71719aed..5fe5461d3173 100644 --- a/net/bpf/bpf_dummy_struct_ops.c +++ b/net/bpf/bpf_dummy_struct_ops.c @@ -178,7 +178,7 @@ static int bpf_dummy_ops_check_member(const struct btf_type *t, case offsetof(struct bpf_dummy_ops, test_sleepable): break; default: - if (prog->aux->sleepable) + if (prog && prog->aux->sleepable) return -EINVAL; } From patchwork Wed Feb 21 07:52:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13565053 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f182.google.com (mail-yw1-f182.google.com [209.85.128.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 292BF3A8C6 for ; Wed, 21 Feb 2024 07:52:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708501943; cv=none; b=ZuQb1R4NynSlBvjehX15gRNrfxC9OPjFPggheA8XKg0D2nqQVv84T1YC8qLkALAtaVww/UT+dlo1cLH/ImdKm5RhTvdoWEu+ZxuAcNlF4wW+ihxKkCPcv3v6cPAiNs3OuGAznZf2G5IoHIKauNRSiZnwSrYmIWfb2RcuZWpCEYg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708501943; c=relaxed/simple; bh=bv/1hRkM3Caeiw9XdPyvgeh3USah0jq4oHlt2TAEppU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JuLf5alao5lEXky6W+MzibcHuekrREckqZOh8uMmER45naNkgSFGeYcAUQUKbE7Y70av3Ainu8wPAXzrB6nOvzIg9ebIwODHDH+EnH8pnIRzQG5Ve5r5FrkADvGTsLz+k9152EOJX+mzklyEI5MB+Q/eUbCrJRmYtsnPIihV0Og= 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=KFzaKRXk; arc=none smtp.client-ip=209.85.128.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="KFzaKRXk" Received: by mail-yw1-f182.google.com with SMTP id 00721157ae682-60837b7a8ddso32380287b3.3 for ; Tue, 20 Feb 2024 23:52:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708501940; x=1709106740; 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=vMO9W8z6RNVcVp/YfmN+vB/3ruzOzR+od+1NltACVoU=; b=KFzaKRXkP2HbERPCJSQeDtAM7AaZzF+0l/haprkq+EDIMr+FiTnO9zzhCMpUmLo8jn YrSrfGQqx4kX2WT2AUZkAOGTpJIY2CJeaPsPV5WJ9YULc/JxZWuqLOoXcDX2llTomeM0 Pd5CVzG4VBjizl2xFZA/C/0eal4oz2+IzB3JNJFLQ3ZxppV3p7JacZtGDRGIERjTQHFK QC8x1/tcB9E/nccvPvihNz5Q8UwhdhLAWY1rkRqkRbR9oGsAwpbHGxg3rLxfAgTGteeH oX/m+QYyufYWU1u6A4JTObQYmB5AiIiBCmZIEyLxVTfGiCrq+r0MoWoXV4P7PzkW3Cu/ skRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708501940; x=1709106740; 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=vMO9W8z6RNVcVp/YfmN+vB/3ruzOzR+od+1NltACVoU=; b=WGWyr7e+x5JzZGhXxB+K0ya/RP7CPRZSDT0dFWstR2eRw9T4XhO3U6Ra40fspoG1K0 ZKVfXv4df31hLfsQAgjdiV6hncYOkhxFUBI9EXH39RJoEyVKeltBfHUu31mHhIEmbLI/ qAPhIGUgMJsg5RP3D0i3SHXWHwBu/P0K+r5ECzkmqziqfo3W1ayGeJVL3A5bsWmS8CYm uz5iCHmXhTlcORY1dqo7tlPTYS/Jnpz312ieqX7kVFYY8Z3pGih9h9QFbNcKwtUody9H Wyfw3Du9wB54SUhGJeGJJNcKuKBWRvEwpv18aEoA04ROXaX3I7KVKyMbwjZaIpdSrga7 9z8g== X-Gm-Message-State: AOJu0YwOg7g5JEkAZSMwJKCv42h/RGiiw6ZsMUHMH7+O7cPkRNBoAaTr yP0FHA4Xjpmt4Cm/allekYpTQ1l2n2X0xhKQIRAIaq8Q/8mrcI5pC3o7IBpn X-Google-Smtp-Source: AGHT+IEMxgWNLbWlIuyAXEGIusaKmPyJn/3493UN4o94oXzPEhUJzBDKCysyleYM+9f2/Q9VZTem5Q== X-Received: by 2002:a05:690c:3609:b0:608:2513:64ab with SMTP id ft9-20020a05690c360900b00608251364abmr10923191ywb.8.1708501940523; Tue, 20 Feb 2024 23:52:20 -0800 (PST) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:26eb:2942:8151:a089]) by smtp.gmail.com with ESMTPSA id m205-20020a8171d6000000b006048e2331fcsm2488715ywc.91.2024.02.20.23.52.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 23:52:20 -0800 (PST) From: thinker.li@gmail.com To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v4 2/3] bpf: Check cfi_stubs before registering a struct_ops type. Date: Tue, 20 Feb 2024 23:52:12 -0800 Message-Id: <20240221075213.2071454-3-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240221075213.2071454-1-thinker.li@gmail.com> References: <20240221075213.2071454-1-thinker.li@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 From: Kui-Feng Lee Recently, cfi_stubs were introduced. However, existing struct_ops types that are not in the upstream may not be aware of this, resulting in kernel crashes. By rejecting struct_ops types that do not provide cfi_stubs during registration, these crashes can be avoided. Signed-off-by: Kui-Feng Lee --- kernel/bpf/bpf_struct_ops.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c index 0d7be97a2411..c1c502caae08 100644 --- a/kernel/bpf/bpf_struct_ops.c +++ b/kernel/bpf/bpf_struct_ops.c @@ -302,6 +302,11 @@ int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc, } sprintf(value_name, "%s%s", VALUE_PREFIX, st_ops->name); + if (!st_ops->cfi_stubs) { + pr_warn("struct %s has no cfi_stubs\n", st_ops->name); + return -EINVAL; + } + type_id = btf_find_by_name_kind(btf, st_ops->name, BTF_KIND_STRUCT); if (type_id < 0) { @@ -339,6 +344,7 @@ int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc, for_each_member(i, t, member) { const struct btf_type *func_proto; + u32 moff; mname = btf_name_by_offset(btf, member->name_off); if (!*mname) { @@ -361,6 +367,17 @@ int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc, if (!func_proto) continue; + moff = __btf_member_bit_offset(t, member) / 8; + err = st_ops->check_member ? + st_ops->check_member(t, member, NULL) : 0; + + if (!err && !*(void **)(st_ops->cfi_stubs + moff)) { + pr_warn("member %s in struct %s has no cfi stub function\n", + mname, st_ops->name); + err = -EINVAL; + goto errout; + } + if (btf_distill_func_proto(log, btf, func_proto, mname, &st_ops->func_models[i])) { From patchwork Wed Feb 21 07:52:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13565054 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f174.google.com (mail-yw1-f174.google.com [209.85.128.174]) (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 335D43AC24 for ; Wed, 21 Feb 2024 07:52:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708501945; cv=none; b=eI9kMOXKF51rTZNGFYuUClKrjqP7zsE9nrTrtOHse/amwGYQFi+fuI1F6AIeLMq9J+3rlCP0fW0TRZwDz2T9qkZqr+HwoRx1fDSm1QZVT/pR2s7XxGH2kcIavh4inPbu11mcaBZymod0oMXSXv/ZkZy//64On33bcERkZKSobmU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708501945; c=relaxed/simple; bh=Dw4CJaNHKe+pg7XSzegbfn6FDwV+t1G1PwNSdjdIsdc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KbFR2Eyti+4TIP2HSvrMO8z+yet+fo2udBj+3CFBonSOljFbhlJUTsBc3L7MvJLIYFKyUeo2lcNCM8S7m8+b7H0ZW16T0SUJPmHkCfDfo1a8QioAMEPVPdIyqdMlkQFmobbg9y3p0yeiQse0u4rAScFam5mEbZ4cDoK6tPQoIVQ= 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=Q4qb/jrQ; arc=none smtp.client-ip=209.85.128.174 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="Q4qb/jrQ" Received: by mail-yw1-f174.google.com with SMTP id 00721157ae682-607f94d0b7cso53340257b3.3 for ; Tue, 20 Feb 2024 23:52:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708501942; x=1709106742; 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=JWynwO0I8208AcDlFWz1kBnWyZmLTlcYH9g+FM611g8=; b=Q4qb/jrQIXcmy5rxTGV4+N0SqlOb77JSesFRFNTjp4dxwOWeRWzKQ0mrnYwhdgvwLT Wp+ZHEeE5GCUrse7KNFE7obsPZjZk+CHuWxAvSJ5/AhspylLIUIr+BCGRrrVYKOYugDY F7H3ryop6GOH0phGffxZpp+mBg48SLgMaPxldlINi0H6gsPndNKuE9HrrVOrlE8h9KFM HVT1hY7t6f4Fe54ckIbAcAwVgts1qw5M5qTCPJ1h1pQ8RAiDfgBeAGVneFcH1Q9SKgbn JSH3wmdJch2pe6TIVBC5if86PVbb9JuGyZREH+0Ggv07XY+jskGHOt1d33azIjRMiVkH R9/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708501942; x=1709106742; 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=JWynwO0I8208AcDlFWz1kBnWyZmLTlcYH9g+FM611g8=; b=KYSvBp70/hw80/Pvsk8X8Rk1b8Olu+VyfpGsOQ/VqFJdBkn78bci2y21aLvpA4fU3G 8eHj7yv+MR4vBNVct8Ef5WG1aBGKrzfMGgGpjjkKaEkCT0KhrvGlyT2naXE7HVsSt7bL IE7+Uzio9dU0nytEw1P+UNeZqkef+LPpn+ickgMTVmQdjc/tBoKbcsTwwsvLS9+0/PE5 IF3ANMBgKRf2sCRNG/lIn3bgpQxbB+sYYk7jf9cK9GvVIQmyZ01DUDXSerxK3VudU6Yb va/slALc863SZj0kK3CA0BkqTz28VhJf6HgKJHvUdbBJFR19vuF04CpsYDGKf7WyKU+O JdpQ== X-Gm-Message-State: AOJu0YxU9ND4BVa60CNCIScefzTH85AFuiHn7WtzIgs1Ygw+uXzs4uj/ PcP6V+3Czmgwj0iAnCw5AmSotLIGp1ZexGRFwzx7FKFUN/9bAvodGo39uMQy X-Google-Smtp-Source: AGHT+IFEoD20cdseNyJB1Ku6rcJoEL+vP3FBn32iU/BPRyzxqWWAhMuiwMj3j3JMMdmNEkrdIadMOg== X-Received: by 2002:a0d:f5c1:0:b0:608:2ad1:bac6 with SMTP id e184-20020a0df5c1000000b006082ad1bac6mr8318678ywf.27.1708501941697; Tue, 20 Feb 2024 23:52:21 -0800 (PST) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:26eb:2942:8151:a089]) by smtp.gmail.com with ESMTPSA id m205-20020a8171d6000000b006048e2331fcsm2488715ywc.91.2024.02.20.23.52.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 23:52:21 -0800 (PST) From: thinker.li@gmail.com To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v4 3/3] selftests/bpf: Test case for lacking CFI stub functions. Date: Tue, 20 Feb 2024 23:52:13 -0800 Message-Id: <20240221075213.2071454-4-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240221075213.2071454-1-thinker.li@gmail.com> References: <20240221075213.2071454-1-thinker.li@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 From: Kui-Feng Lee Ensure struct_ops rejects the registration of struct_ops types without proper CFI stub functions. bpf_test_no_cfi.ko is a module that attempts to register a struct_ops type called "bpf_test_no_cfi_ops" with varying levels of cfi_stubs. It starts with a NULL cfi_stub and ends with a fully complete cfi_stub. Only the fully complete cfi_stub should be accepted by struct_ops. The module can only be loaded successfully if these registrations yield the expected results. Signed-off-by: Kui-Feng Lee --- tools/testing/selftests/bpf/Makefile | 10 +- .../selftests/bpf/bpf_test_no_cfi/Makefile | 19 ++++ .../bpf/bpf_test_no_cfi/bpf_test_no_cfi.c | 93 +++++++++++++++++++ .../bpf/prog_tests/test_struct_ops_no_cfi.c | 38 ++++++++ tools/testing/selftests/bpf/testing_helpers.c | 4 +- tools/testing/selftests/bpf/testing_helpers.h | 2 + 6 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/bpf/bpf_test_no_cfi/Makefile create mode 100644 tools/testing/selftests/bpf/bpf_test_no_cfi/bpf_test_no_cfi.c create mode 100644 tools/testing/selftests/bpf/prog_tests/test_struct_ops_no_cfi.c diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index dbb8c5f94f34..c219da5e60e6 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -132,7 +132,7 @@ TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \ flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \ test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko \ xskxceiver xdp_redirect_multi xdp_synproxy veristat xdp_hw_metadata \ - xdp_features + xdp_features bpf_test_no_cfi.ko TEST_GEN_FILES += liburandom_read.so urandom_read sign-file uprobe_multi @@ -254,6 +254,12 @@ $(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard bpf_testmo $(Q)$(MAKE) $(submake_extras) RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) -C bpf_testmod $(Q)cp bpf_testmod/bpf_testmod.ko $@ +$(OUTPUT)/bpf_test_no_cfi.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard bpf_test_no_cfi/Makefile bpf_test_no_cfi/*.[ch]) + $(call msg,MOD,,$@) + $(Q)$(RM) bpf_test_no_cfi/bpf_test_no_cfi.ko # force re-compilation + $(Q)$(MAKE) $(submake_extras) RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) -C bpf_test_no_cfi + $(Q)cp bpf_test_no_cfi/bpf_test_no_cfi.ko $@ + DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool ifneq ($(CROSS_COMPILE),) CROSS_BPFTOOL := $(SCRATCH_DIR)/sbin/bpftool @@ -628,6 +634,7 @@ TRUNNER_EXTRA_SOURCES := test_progs.c \ flow_dissector_load.h \ ip_check_defrag_frags.h TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko \ + $(OUTPUT)/bpf_test_no_cfi.ko \ $(OUTPUT)/liburandom_read.so \ $(OUTPUT)/xdp_synproxy \ $(OUTPUT)/sign-file \ @@ -756,6 +763,7 @@ EXTRA_CLEAN := $(SCRATCH_DIR) $(HOST_SCRATCH_DIR) \ feature bpftool \ $(addprefix $(OUTPUT)/,*.o *.skel.h *.lskel.h *.subskel.h \ no_alu32 cpuv4 bpf_gcc bpf_testmod.ko \ + bpf_test_no_cfi.ko \ liburandom_read.so) .PHONY: docs docs-clean diff --git a/tools/testing/selftests/bpf/bpf_test_no_cfi/Makefile b/tools/testing/selftests/bpf/bpf_test_no_cfi/Makefile new file mode 100644 index 000000000000..ed5143b79edf --- /dev/null +++ b/tools/testing/selftests/bpf/bpf_test_no_cfi/Makefile @@ -0,0 +1,19 @@ +BPF_TEST_NO_CFI_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST))))) +KDIR ?= $(abspath $(BPF_TEST_NO_CFI_DIR)/../../../../..) + +ifeq ($(V),1) +Q = +else +Q = @ +endif + +MODULES = bpf_test_no_cfi.ko + +obj-m += bpf_test_no_cfi.o + +all: + +$(Q)make -C $(KDIR) M=$(BPF_TEST_NO_CFI_DIR) modules + +clean: + +$(Q)make -C $(KDIR) M=$(BPF_TEST_NO_CFI_DIR) clean + diff --git a/tools/testing/selftests/bpf/bpf_test_no_cfi/bpf_test_no_cfi.c b/tools/testing/selftests/bpf/bpf_test_no_cfi/bpf_test_no_cfi.c new file mode 100644 index 000000000000..0fb63feecb31 --- /dev/null +++ b/tools/testing/selftests/bpf/bpf_test_no_cfi/bpf_test_no_cfi.c @@ -0,0 +1,93 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ +#include +#include +#include +#include + +struct bpf_test_no_cfi_ops { + void (*fn_1)(void); + void (*fn_2)(void); +}; + +static int dummy_init(struct btf *btf) +{ + return 0; +} + +static int dummy_init_member(const struct btf_type *t, + const struct btf_member *member, + void *kdata, const void *udata) +{ + return 0; +} + +static int dummy_reg(void *kdata) +{ + return 0; +} + +static void dummy_unreg(void *kdata) +{ +} + +static const struct bpf_verifier_ops dummy_verifier_ops; + +static void bpf_test_no_cfi_ops__fn_1(void) +{ +} + +static void bpf_test_no_cfi_ops__fn_2(void) +{ +} + +static struct bpf_test_no_cfi_ops __bpf_test_no_cfi_ops; + +static struct bpf_struct_ops bpf_bpf_test_no_cif_ops = { + .verifier_ops = &dummy_verifier_ops, + .init = dummy_init, + .init_member = dummy_init_member, + .reg = dummy_reg, + .unreg = dummy_unreg, + .name = "bpf_test_no_cfi_ops", + .owner = THIS_MODULE, +}; + +static int bpf_test_no_cfi_init(void) +{ + int ret; + + ret = register_bpf_struct_ops(&bpf_bpf_test_no_cif_ops, + bpf_test_no_cfi_ops); + if (!ret) + return -EINVAL; + + bpf_bpf_test_no_cif_ops.cfi_stubs = &__bpf_test_no_cfi_ops; + ret = register_bpf_struct_ops(&bpf_bpf_test_no_cif_ops, + bpf_test_no_cfi_ops); + if (!ret) + return -EINVAL; + + __bpf_test_no_cfi_ops.fn_1 = bpf_test_no_cfi_ops__fn_1; + ret = register_bpf_struct_ops(&bpf_bpf_test_no_cif_ops, + bpf_test_no_cfi_ops); + if (!ret) + return -EINVAL; + + __bpf_test_no_cfi_ops.fn_2 = bpf_test_no_cfi_ops__fn_2; + ret = register_bpf_struct_ops(&bpf_bpf_test_no_cif_ops, + bpf_test_no_cfi_ops); + return ret; +} + +static void bpf_test_no_cfi_exit(void) +{ +} + +module_init(bpf_test_no_cfi_init); +module_exit(bpf_test_no_cfi_exit); + +MODULE_AUTHOR("Kuifeng Lee"); +MODULE_DESCRIPTION("BPF no cfi_stubs test module"); +MODULE_LICENSE("Dual BSD/GPL"); + diff --git a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_no_cfi.c b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_no_cfi.c new file mode 100644 index 000000000000..19703e250549 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_no_cfi.c @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ +#include +#include + +static void load_bpf_test_no_cfi(void) +{ + int fd; + int err; + + fd = open("bpf_test_no_cfi.ko", O_RDONLY); + if (!ASSERT_GT(fd, 0, "open")) { + close(fd); + return; + } + + /* The module will try to register a struct_ops type with + * no cfi_stubs, incomplete cfi_stubs, and full cfi_stubs. + * + * Only full cfi_stubs should be allowed. The module will be loaded + * successfully if the result of the registration is as expected, + * or it fails. + */ + err = finit_module(fd, "", 0); + close(fd); + if (!ASSERT_OK(err, "finit_module")) + return; + + err = delete_module("bpf_test_no_cfi", 0); + ASSERT_OK(err, "delete_module"); +} + +void test_struct_ops_no_cfi(void) +{ + if (test__start_subtest("load_bpf_test_no_cfi")) + load_bpf_test_no_cfi(); +} + diff --git a/tools/testing/selftests/bpf/testing_helpers.c b/tools/testing/selftests/bpf/testing_helpers.c index a59e56d804ee..28b6646662af 100644 --- a/tools/testing/selftests/bpf/testing_helpers.c +++ b/tools/testing/selftests/bpf/testing_helpers.c @@ -356,12 +356,12 @@ __u64 read_perf_max_sample_freq(void) return sample_freq; } -static int finit_module(int fd, const char *param_values, int flags) +int finit_module(int fd, const char *param_values, int flags) { return syscall(__NR_finit_module, fd, param_values, flags); } -static int delete_module(const char *name, int flags) +int delete_module(const char *name, int flags) { return syscall(__NR_delete_module, name, flags); } diff --git a/tools/testing/selftests/bpf/testing_helpers.h b/tools/testing/selftests/bpf/testing_helpers.h index d14de81727e6..d55f6ab12433 100644 --- a/tools/testing/selftests/bpf/testing_helpers.h +++ b/tools/testing/selftests/bpf/testing_helpers.h @@ -36,6 +36,8 @@ __u64 read_perf_max_sample_freq(void); int load_bpf_testmod(bool verbose); int unload_bpf_testmod(bool verbose); int kern_sync_rcu(void); +int finit_module(int fd, const char *param_values, int flags); +int delete_module(const char *name, int flags); static inline __u64 get_time_ns(void) {