From patchwork Sat Dec 30 20:06:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Markus Elfring X-Patchwork-Id: 13507043 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mout.web.de (mout.web.de [212.227.15.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 40785BE68; Sat, 30 Dec 2023 20:06:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=web.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=web.de header.i=markus.elfring@web.de header.b="fn3gQFbK" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=s29768273; t=1703966781; x=1704571581; i=markus.elfring@web.de; bh=D2US4ZEXMGNvCB3ybkBudZXkGlLbPNb81iXdW55uLKs=; h=X-UI-Sender-Class:Date:Subject:From:To:Cc:References: In-Reply-To; b=fn3gQFbKxRl8mXMrDVsEWJx2c89ER+Ze0ltH72Ev/IMvYr6GsW5Rq68QP1sOcX/o sBt65kduKqN8ItflfgT2s+aJSq2b07FlX/TNYcbguI1RgRjQ2tBSu+Gzg0wxZWipv PPz/rv+bLwSy13jrOVgiNaUG4nd94NQnHyg+2Tn5MEViAC4lcisSKggmDwiq+dOXz riHdtobzV5na9Id4yT2Iwf06qraQgLs1fCVFylsaumdSoDy0pvUBDJmniJ7BCiwgD FnNPSqzGouviFjrD6xYNHr+tYGOPTklMrbgdtqD5YICb/efTAi25dba9hmbrXmAfa t9jSwzq9bOAV2Yi7dA== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.21] ([94.31.87.95]) by smtp.web.de (mrweb006 [213.165.67.108]) with ESMTPSA (Nemesis) id 1MSZI5-1rmQse0L4T-00Ss84; Sat, 30 Dec 2023 21:06:21 +0100 Message-ID: <2c4ef323-4ed7-4b43-8dfe-b55aeecaa3bf@web.de> Date: Sat, 30 Dec 2023 21:06:18 +0100 Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH 1/5] bpf: Improve exception handling in bpf_struct_ops_link_create() Content-Language: en-GB From: Markus Elfring To: bpf@vger.kernel.org, netdev@vger.kernel.org, kernel-janitors@vger.kernel.org, Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Hao Luo , Jiri Olsa , John Fastabend , KP Singh , Martin KaFai Lau , Song Liu , Stanislav Fomichev , Yonghong Song Cc: LKML References: <7011cdcc-4287-4e63-8bfa-f08710f670b1@web.de> In-Reply-To: <7011cdcc-4287-4e63-8bfa-f08710f670b1@web.de> X-Provags-ID: V03:K1:QUFRAMK31tz+n4I7fbLISjWjYnW9MXfTu8MQzaa7dzNHG4lyZE9 hPxewfn30rrz6QwVKBfATU6jer4OQYLjoEp378zCcClBtSZXKtjPzGiOZ/EDARroYPzqeCD fILh7XzcmKpu8WyrVUOgLnEUEa3y6Imf5a7fbxgAHjX3/SDQvv6xMWdsug5E/4Mm33z+Bz3 t9mZsI5CijtdmA9xcFYvw== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:juW6K/s+4Cg=;f9KejanXXCgaKD/YMUAkjb+PKOh NZ1KABzA54jNWyTHEcMkbriJRgAfvb2Mu8Vm5g31J7ttikxn+VVDnVZHrfsVE2sb974NcyoPB 1XZsJlrk/sm8lSg+7aCcqnQ+5ydNQB/qtTDOIK6PCs1rVqBbSbUWak8o8CI6fcqUcH5rnEnF6 e/yI1VbyDK6CCCurwwr+1rShIAxhHvqdtdO+eI2XI1TQZJqIaXMNdzfetVyPdUG6okIQmIeAh T1NbB4EHV8/qgYgNnn63zUbY5E9v5BMg24b36wMSyAdsw/5qoMFpsUgtfkGhiFJ74b1gPP0h0 7RWCHVN9mlO5JUwcvySwz4wxC8ccWtFE1Q3W4njASSruuxHb8SHNbez5MAXJbDrh3B84ayA2j PqrfEeAnI9jnTA7nr40QjSn+4XOFz0toulO4iJIEKAg/BxMKmDyJGhPtedl0WNVDC3fS7YH7y tW8PitrX+XZYNikB59EgFgg/YRI5GN7U5xC8Dm8lC2aR61up2A0d9scwbw+hGsPkoi0IVcoFS 4AnY5WyGAL8v+qXvr/cPHbvDVkAdFlXOa3CXeQVmEeobOIo7gn8Wf8Bf0ZhGuIYJZkr72IIzZ rKknj9AO44Y3E3cnrSdr4EtOviVIF2/kJcucQeJmcQHMpTr/gfKnbBBepN01zDjttz7Clh4aZ 3dzCV0CLCd7yegKbpRR1x3rjtXbNkLyb6F0NpF75mg+DIreeXuZLpq0Jjv6XJ7ZnoAq837TVS O7gFffOgxcxqsXv8d5/Y3rEAhuFxnH6W+7rDVXwWvzeNWaHtxmH6aJbVX6sq9Fe5cZZaoYNZJ fkYzMxU3iglc7D8PpIDVwA346seOePP1otkraudIPvggQ4V6L9i0B3PTpFLnxkYmLoP78BYZw MUrPaYzS62uCWzY2mqda6lY06EkatAZHuTDF8UJMXqtmD71UqKRu+QG69mzGJW4Wx4D41w+DF EGIBPw== X-Patchwork-Delegate: bpf@iogearbox.net From: Markus Elfring Date: Sat, 30 Dec 2023 18:50:45 +0100 The kfree() function was called in two cases by the bpf_struct_ops_link_create() function during error handling even if the passed variable contained a null pointer. This issue was detected by using the Coccinelle software. * Thus use another label. * Reorder function calls at the end. * Delete an initialisation (for the variable “link”) which became unnecessary with this refactoring. Signed-off-by: Markus Elfring --- kernel/bpf/bpf_struct_ops.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) -- 2.43.0 diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c index 02068bd0e4d9..b49ea460d616 100644 --- a/kernel/bpf/bpf_struct_ops.c +++ b/kernel/bpf/bpf_struct_ops.c @@ -888,7 +888,7 @@ static const struct bpf_link_ops bpf_struct_ops_map_lops = { int bpf_struct_ops_link_create(union bpf_attr *attr) { - struct bpf_struct_ops_link *link = NULL; + struct bpf_struct_ops_link *link; struct bpf_link_primer link_primer; struct bpf_struct_ops_map *st_map; struct bpf_map *map; @@ -902,13 +902,13 @@ int bpf_struct_ops_link_create(union bpf_attr *attr) if (!bpf_struct_ops_valid_to_reg(map)) { err = -EINVAL; - goto err_out; + goto put_map; } link = kzalloc(sizeof(*link), GFP_USER); if (!link) { err = -ENOMEM; - goto err_out; + goto put_map; } bpf_link_init(&link->link, BPF_LINK_TYPE_STRUCT_OPS, &bpf_struct_ops_map_lops, NULL); @@ -927,7 +927,8 @@ int bpf_struct_ops_link_create(union bpf_attr *attr) return bpf_link_settle(&link_primer); err_out: - bpf_map_put(map); kfree(link); +put_map: + bpf_map_put(map); return err; } From patchwork Sat Dec 30 20:08:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Markus Elfring X-Patchwork-Id: 13507044 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mout.web.de (mout.web.de [212.227.15.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D781CBE5D; Sat, 30 Dec 2023 20:08:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=web.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=web.de header.i=markus.elfring@web.de header.b="t9apMdl9" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=s29768273; t=1703966896; x=1704571696; i=markus.elfring@web.de; bh=rAdygzKsqo2awjxq9Z7K0OqKNlPxCRegPbkMKgPEzqA=; h=X-UI-Sender-Class:Date:Subject:From:To:Cc:References: In-Reply-To; b=t9apMdl9JHUgfJkl67EqSzX4k1VZQa4CSAeNzR14gTH2CKJBT0LBvm/Cv4ehVUaF rznikftrvSK3bYtsk/87aOgV7KmN6aW2L+fuBVyi9oj9sFpcBjQaDQUVz21/0P4Ci nZvcT/CdWkfS6O7w0xx5ELH/NiALWkeUtminTScIMRNjUWYIaR9+0A0O97pTRuXTi eGM99TEg0A4/tbZnUHfXTLumoIUzasWfVuoO/OkLCHsWNp+O/IpyeCjbFAcS9dLe7 Ot0tX2euN1YAWtndcN5Nj/OzNtarfODNsVQGi8Q+w98KqXoNu/f9dBwFP11JaMu3P owbuV95QnS+YRWWy8Q== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.21] ([94.31.87.95]) by smtp.web.de (mrweb005 [213.165.67.108]) with ESMTPSA (Nemesis) id 1MTfon-1rnXNV2aKL-00TvHq; Sat, 30 Dec 2023 21:08:16 +0100 Message-ID: Date: Sat, 30 Dec 2023 21:08:15 +0100 Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: =?utf-8?q?=5BPATCH_2/5=5D_bpf=3A_Move_an_assignment_for_the_variabl?= =?utf-8?q?e_=E2=80=9Cst=5Fmap=E2=80=9D_in_bpf=5Fstruct=5Fops=5Flink=5Fcreat?= =?utf-8?q?e=28=29?= Content-Language: en-GB From: Markus Elfring To: bpf@vger.kernel.org, netdev@vger.kernel.org, kernel-janitors@vger.kernel.org, Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Hao Luo , Jiri Olsa , John Fastabend , KP Singh , Martin KaFai Lau , Song Liu , Stanislav Fomichev , Yonghong Song Cc: LKML References: <7011cdcc-4287-4e63-8bfa-f08710f670b1@web.de> In-Reply-To: <7011cdcc-4287-4e63-8bfa-f08710f670b1@web.de> X-Provags-ID: V03:K1:18qx8X9R9dIRWlO2/jLbUn6fKchkG9q9s5s9lZu63vS38Eo/5ev RnmbniKYVw/tMozG3vCSqLSGPjlaFDksCSEyEFK8nKfoIyBun8gZQBBNtpgbPJr6Wp3YViV n/AltZPgSrTzW3zwsNWLIEZy7Th93dxW2YnmsPbqFlw5ZWVV0+0eiGedZ7RXxjpv4Mrceyq dIy6B/+jmbxElD6HkJX8Q== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:i0un/BHbWps=;QKSAScUuoXyGC47nAxt4n0/zVM5 wpBfYIQsN/kjWMzBtX+Ek+R15mw4eH8Lzju2aQ+ejfnuZqNMDZxPuc2RqwZe4Ov7cNb6WuQ2c Lu+Eogw6bSRQABNBrv/RkXVWtZTtPcXQEOmyn6SM8UK9qorbPJZ0CMw0ILegPwkhLq6tK/1pz mNCjRqv/aHhpWXzFiQYzf8hRjWxlQNpy7CnfG3fT+PTmN/tzyrb31W5TZ/4yHgMJhmB4oavlA 2lNArTKwkfmfO0mGBeOj9XPi/8ApRYC2Z+9Qc/K5XG2xzxueKU5MSFq8M6fyfJaaUmSANJUXW R9t0qlVB/g+vAOrSok7rv7r94FAO26vrQttz53agoQW9vN3tK9Gk55cq3CKtvn2J7iSQi+YPN cLEoc7+t0Z9ZTcHE0nNwmcshOthzEq2Bt5PhABxjSV532O8yZO8EKxEymAHrAipbPVcCRFQZJ JpmUD2PnpI/RRQpDCORvCrepVECBSfT1gQhWGS0JaCAOEDlUpYb6ziYJj6E3hryx048Swz9Aj 5T8vAb3BZGuxCN3QQrVw4TaZWc+6IWmdpJjF8iTqFIlNE2vCScb0KF2hYSU8ygNFL5SleIGx6 008CdapbEA8tjCiJ/sF9xZWNB0JrMUgVM5l61wtOmEJi2UB/z7gSGRo48pSqVk6i47ccjnPxX i6iPqRaExLPU7R0RDS5K0ZKr8pLfAvAOtyjPlSPZDl1VjRAn6mlWSlfDMT2kBFgke5vOVvlYP JizTrINS7kElSNl/6eP+6g84nLKTTqjyLJ8tLXOvxR9d6TRhD5e9Wthjou87KkCLoq778nad/ ZofMjzuEBHro5p/JLiDA3e8fU7LOsmLy/FIyNE5nHVX293+trIAyFfG0wphkn5EO+8nkhAH25 xG6OmtE5mWbO9Nq9ZVO9pDLHjST4b1XPiJUqZvj65ThGwj7s69i8Yq62BsUyrHZkeC2FuIXmN x5010zC6Cb/rzRRyGeL71QRvjZ8= From: Markus Elfring Date: Sat, 30 Dec 2023 19:00:12 +0100 Move one assignment for the variable “st_map” closer to the place where this pointer is used. Signed-off-by: Markus Elfring --- kernel/bpf/bpf_struct_ops.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) -- 2.43.0 diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c index b49ea460d616..4133d65c2a28 100644 --- a/kernel/bpf/bpf_struct_ops.c +++ b/kernel/bpf/bpf_struct_ops.c @@ -898,8 +898,6 @@ int bpf_struct_ops_link_create(union bpf_attr *attr) if (IS_ERR(map)) return PTR_ERR(map); - st_map = (struct bpf_struct_ops_map *)map; - if (!bpf_struct_ops_valid_to_reg(map)) { err = -EINVAL; goto put_map; @@ -916,6 +914,7 @@ int bpf_struct_ops_link_create(union bpf_attr *attr) if (err) goto err_out; + st_map = (struct bpf_struct_ops_map *)map; err = st_map->st_ops->reg(st_map->kvalue.data); if (err) { bpf_link_cleanup(&link_primer); From patchwork Sat Dec 30 20:10:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Elfring X-Patchwork-Id: 13507045 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mout.web.de (mout.web.de [212.227.15.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A7499BE68; Sat, 30 Dec 2023 20:10:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=web.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=web.de header.i=markus.elfring@web.de header.b="Udxj252e" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=s29768273; t=1703967021; x=1704571821; i=markus.elfring@web.de; bh=4EGdtn/m/ZrZmRoMNxzHS5SQcI8bELVK6JHcnpRyjtU=; h=X-UI-Sender-Class:Date:Subject:From:To:Cc:References: In-Reply-To; b=Udxj252ekX00qz62jI/RwQoCSKYD3xKScCO9Ws3j3K1qwYb+H+8WV2s3D22UEalw 74SOXcZf+MJS2TQRlnZbotcbNcqt63ZkxINz91eM17FvtIl3N/wB0W+ZSpqKTFXNk +2jl85kHgymWUVcyjiIttjnkPZko6rS+ng9Vd9lF4qgjHzV3q4xnHXm/bQ2G36Ea1 EZHsHpKJbenkXnnKikGo9RbRtMRW4iItSeWOmPSDgcDgOBCGUGOoAHA+eiTvTRNm5 +2Ip49WJN40g0vmrmgjusIXvn6sU0l2usg3cnAs+zmdOw72xt6G7a4qbw8d17MYu+ 3FVllP56s5KfPlRzAw== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.21] ([94.31.87.95]) by smtp.web.de (mrweb006 [213.165.67.108]) with ESMTPSA (Nemesis) id 1MrOdf-1qqtDO2Pp9-00o5BS; Sat, 30 Dec 2023 21:10:21 +0100 Message-ID: <02500028-e67a-4298-abb5-ff4fd66de044@web.de> Date: Sat, 30 Dec 2023 21:10:20 +0100 Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH 3/5] bpf: Improve exception handling in bpf_core_apply() Content-Language: en-GB From: Markus Elfring To: bpf@vger.kernel.org, netdev@vger.kernel.org, kernel-janitors@vger.kernel.org, Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Hao Luo , Jiri Olsa , John Fastabend , KP Singh , Martin KaFai Lau , Song Liu , Stanislav Fomichev , Yonghong Song Cc: LKML References: <7011cdcc-4287-4e63-8bfa-f08710f670b1@web.de> In-Reply-To: <7011cdcc-4287-4e63-8bfa-f08710f670b1@web.de> X-Provags-ID: V03:K1:Jj/BVAH9nKTRpviGnzkgzmM2W0xH7P05KPv2jiu6fqetx/OXSEp ALiC5Rp54eVC+xnBvwgTjDoULtRrdQWpaymrbxq2nxo+hwv28pGNc/YFquDDFomaPMiMLtH 8f5lcrR9IqJsIQgKfSZh2TyQihHe8/GwHyZ6rDNmr5b9YwEAltQG4y7ZimQlwaDEPPefdDY GtbL8uAxWRuS/uQm4wgzg== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:chwhZ8dfETk=;CrwEg3QAt7JxpYRcCuvDrbxBfpf lPiCQqRGohBNgMMVrYYWtu2mvZVdNhiFR3810Rm+8NcfiuFcb2PQ7SzxHrnnGiAMtQ4OUFi9D v0hYNgd3F+sgVkxqXWnOsrNhWEB6Cq8rq6aZW9RgtnhQLLACvC062LK5eN1hcEPjMR0HIewiV g3WD75Dg8zxLWwYZrW36dMqqaub6aI9waLTH5Ppj1kz3eFZhkVDEHPmR4j1+Rg4YXZzpkK8xE /nsEPtOtNg4GrRjcogLHOiWNMbs9KJNiC0Sma2KBoWXUPhqvEDkGI4fJI5boGj/0yJzCNydIh EsF4AJT4ARnUnS7lz/MXgYdjEdkLeCRq8vo/BjKKM2rqrXDd2G7bXkSO/KDDKmRQGpZyoog7F 1tXxZRIDyEh37bCHtiLxegQh4THnI4s59aU4Gc6CD2yLYH6LbFe9x6OcFjfBQB7xQdrqdYWdr CU2Jdzz6Mb89ieSdgS5dDyrGsQkM5lxB7hL7oDz/KpKUs97W3qj66jHUV/XLfFyGMO5X+ZH0o qN7AWjodlPCk/gXBugt24LUJ6Z9RZFYlDQMq/uHAmbHqT7NWhUk0OPr3T8PUBx0j1BByTL5OQ ac1PchRLGJp0tcO8SnQjE1Yi4CHpvgof9pFhbruh+F7/hnFu40F3zDp27QSxMz+N6MowdAxeH jFLH2FHIJJ/SrypDLZzlT5R69JSgFNReu5qoYmiybtECDWjnXbkGf6/3+vnGzF6CVf1zn4D8s dULGIN/IXB4AY8kILRdI7oakBlzceTLtVJlspWkThxDD1Kxwg48RN7e6zxGgsZx31y9k9Fkdv pjCMfpUHJggnTC69TxNooT34leVXi10OtIFtm7ST+sGEiRRdwMWrxS8fDTHKod1yuaKZxkzyC uEdCo7FtGxmNsIi7RTJ0ZdH9Krlt6MV18oxFcnblAQT6XNJLsVffsEU6cnVy/qKiLpSZwfcSf 9nby4g== X-Patchwork-Delegate: bpf@iogearbox.net From: Markus Elfring Date: Sat, 30 Dec 2023 19:28:25 +0100 The kfree() function was called in two cases by the bpf_core_apply() function during error handling even if the passed data structure member contained a null pointer. This issue was detected by using the Coccinelle software. * Thus use another label. * Reorder function calls at the end. Signed-off-by: Markus Elfring --- kernel/bpf/btf.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) -- 2.43.0 diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 51e8b4bee0c8..e8391025d408 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -8322,13 +8322,13 @@ int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo, bpf_log(ctx->log, "target candidate search failed for %d\n", relo->type_id); err = PTR_ERR(cc); - goto out; + goto unlock_mutex; } if (cc->cnt) { cands.cands = kcalloc(cc->cnt, sizeof(*cands.cands), GFP_KERNEL); if (!cands.cands) { err = -ENOMEM; - goto out; + goto unlock_mutex; } } for (i = 0; i < cc->cnt; i++) { @@ -8355,13 +8355,15 @@ int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo, &targ_res); out: - kfree(specs); if (need_cands) { kfree(cands.cands); +unlock_mutex: mutex_unlock(&cand_cache_mutex); if (ctx->log->level & BPF_LOG_LEVEL2) print_cand_cache(ctx->log); } + + kfree(specs); return err; } From patchwork Sat Dec 30 20:12:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Markus Elfring X-Patchwork-Id: 13507046 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mout.web.de (mout.web.de [212.227.15.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 315F8C8E0; Sat, 30 Dec 2023 20:12:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=web.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=web.de header.i=markus.elfring@web.de header.b="PKzhjnSI" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=s29768273; t=1703967145; x=1704571945; i=markus.elfring@web.de; bh=K6NQxGj4hjvTJqhKo6zca8RxH2GLf14mMfJnmGs1PZ4=; h=X-UI-Sender-Class:Date:Subject:From:To:Cc:References: In-Reply-To; b=PKzhjnSIfb5qnv45Iy5h8oQjavLuoskbDGK8zV+/1SCUw4NpUPo5pcK1EWtM3Hoq fX8AHsUeQNSwrSV8zA2U4SGSm8b6RBhTbYhm8nOXP+Nc7wpd5vbmuOr9Ia8V+QZyS GNghKMOMJjy9FRR/APcSw0yrpWXEauB7mN36tk3B07AR++xB/iIr/rX5gn75MfjYJ sB+gJrV/m7GuV37ySnNU4QzOsMjvUGJvNVoL4EovIhc7lcLG0NX2NUWk3cgMhH5dq 3avfpC3um3UZOpbUd08zlCkWjaWo4TnVIdxFnFJaWNMx+n+vLsgEYhCIV3YB1QqNy e8PcuSFOekyDSN/e7A== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.21] ([94.31.87.95]) by smtp.web.de (mrweb005 [213.165.67.108]) with ESMTPSA (Nemesis) id 1MjBVh-1qhZWe0I9V-00emtj; Sat, 30 Dec 2023 21:12:25 +0100 Message-ID: <996d5970-3295-493f-b144-99a0ff771576@web.de> Date: Sat, 30 Dec 2023 21:12:22 +0100 Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH 4/5] bpf: Return directly after a failed bpf_map_kmalloc_node() in bpf_cgroup_storage_alloc() Content-Language: en-GB From: Markus Elfring To: bpf@vger.kernel.org, netdev@vger.kernel.org, kernel-janitors@vger.kernel.org, Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Hao Luo , Jiri Olsa , John Fastabend , KP Singh , Martin KaFai Lau , Song Liu , Stanislav Fomichev , Yonghong Song Cc: LKML References: <7011cdcc-4287-4e63-8bfa-f08710f670b1@web.de> In-Reply-To: <7011cdcc-4287-4e63-8bfa-f08710f670b1@web.de> X-Provags-ID: V03:K1:Bzd7uR+QKaeuLp/OaokKWTTVdIx1ZBWHfLlJFsxem5Z06wv8etG f3A528Ziitxub2/6Djz3VLqtG5s7YYTc71g6LoHzXsF2qQw99GBF/iB+pXJ9et/1V3Zu2HY OxA7outD4cGk/rO94yv7wfn7uywF7I7neFKN2plTMYR9GIIu/xIi2ENIOji9NxLuep7DLHv gWEdB/v9gQVUtxnDS9oTQ== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:h3eYr8fwcAM=;ke5sU6HYS0rZSEQXBHFFa/fgy1V cmawg2hCsSZ/DcNDXX6Gxgdt5bnRjCmATpmuj/J6gQrP3yFA8l1D+DWe9qLuqA8363Ue9N07u BAGPE5piSufMyUasFwTGEcibF4zVGMXI7J0rq71nU8tsniaJ3w4nhZWwSa/xXrXR4o7zz+ft2 XO7E9XqAEVahNXNnwll5QHzTe6/faChZ+VwM00iHVEVwMR+lFxQgpaJpZrijFhi3SlP4iniRl 3KI45dlUgfo4fyx/CxVyqYYdq2eze7FLf5+Lobkf7rO3dluUrJL05VZWknH79oJWNQqrd+qtl 4iSLyaEb/9gnBSoRKMdYyWETe4H5/qsnWH8+ldW1e+/ykDpgJGQ7TIA3aKZKxka10CCbGgLie NNQPDcVH9DeyY5szX5k8W5jcoO3L00sPnR/TDJSEL1Hw/RQv0JD/vCs6E5Uicn0ekUPQdd4WI Lsu2OrzECb6drkUBewtbhP+JNMzg1zZlQEAcmNPbct7RNw3ApvgVvFZqRKPGVB+YKRYI+EfNm O8nDaa50Tnsw71MP6lHfsUzorp+Ve3bEU6DMc/q2HiGcwOmQxiJOSW8rTIu31t8GmP1+8TEoa aiuimsPn5wSSRedxOiO88AXIS3sdx7AeeEFBmLKTYJeKs6GPDyWYRLSqUSDQ1tlbv42TIW1xg YTqeyTVlHJWlrvrdEBIGT/aAmUPDcZOzY5N8ySk+1+ucfAdzsRB8EctrWzicIIdzskhDzB9h5 F5/7SiPFFovNDhOCL4G1oI3oAqIviRAP2xC7BnsRCsLWnjMMQaJrrh4N4M3MKhnfCfzivKUH4 bN6VgsArqPI0bLRvpsrKUVGgXs+cIoU6w3yJKXnubngoOWaolCeCtZWevKpI5/G8xkrqolQdb m7yx684r1ag62i/Ixg0+rEFkrfPq2XSiLCCVRcHfGiEmz5H3x4AH3s6FNq0iZPqmV+gkbhQXU RVmAUnF0OBshMw5rgmO29/IeonY= X-Patchwork-Delegate: bpf@iogearbox.net From: Markus Elfring Date: Sat, 30 Dec 2023 20:06:02 +0100 The kfree() function was called in one case by the bpf_cgroup_storage_alloc() function during error handling even if the passed variable contained a null pointer. This issue was detected by using the Coccinelle software. Thus return directly after a call of the function “bpf_map_kmalloc_node” failed at the beginning. Signed-off-by: Markus Elfring --- kernel/bpf/local_storage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) -- 2.43.0 diff --git a/kernel/bpf/local_storage.c b/kernel/bpf/local_storage.c index a04f505aefe9..e16a80c93cd7 100644 --- a/kernel/bpf/local_storage.c +++ b/kernel/bpf/local_storage.c @@ -514,7 +514,7 @@ struct bpf_cgroup_storage *bpf_cgroup_storage_alloc(struct bpf_prog *prog, storage = bpf_map_kmalloc_node(map, sizeof(struct bpf_cgroup_storage), gfp, map->numa_node); if (!storage) - goto enomem; + return ERR_PTR(-ENOMEM); if (stype == BPF_CGROUP_STORAGE_SHARED) { storage->buf = bpf_map_kmalloc_node(map, size, gfp, From patchwork Sat Dec 30 20:14:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Markus Elfring X-Patchwork-Id: 13507047 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mout.web.de (mout.web.de [212.227.15.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4F3ABC13D; Sat, 30 Dec 2023 20:14:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=web.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=web.de header.i=markus.elfring@web.de header.b="GvYLS2xc" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=s29768273; t=1703967254; x=1704572054; i=markus.elfring@web.de; bh=kXNvk+63Yu+KOv3cgshkVkUjGMpZfjy8lTJZ4d+G7Uc=; h=X-UI-Sender-Class:Date:Subject:From:To:Cc:References: In-Reply-To; b=GvYLS2xcMFqOVDgt4xlnovtDxtv2WddWSOgAdS/+dvOOF6ICdHIfdMBrEjlE57Mu HJCdQ9ZEmyIifeESoHyStlP3GSV3ICZq6m6Kw+hzlvvlCKhUq/0XWMFCHmrQCtwGi 2Ap8/vG5GXheEaUjgXJEocEDQ1syWkhvpFnAWMIR3rnvsjt1pmFHrnMAol2ripcp8 6XhKIIpXOPtGvdl5yKHj8SoB4/fj1UDb9wKGjHLBDt3cEDTQBLg4ZXcnYVEsM7joh G49qBbGnymZEf97vkCx4YuNubMlz8jovYL6C3ZkWcRnYdPgmxIU4vguYrkLfTepsv 3w+q4jAawarodsC7Pg== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.21] ([94.31.87.95]) by smtp.web.de (mrweb006 [213.165.67.108]) with ESMTPSA (Nemesis) id 1M2Phi-1rNW0Z3wPm-004I7J; Sat, 30 Dec 2023 21:14:14 +0100 Message-ID: Date: Sat, 30 Dec 2023 21:14:13 +0100 Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH 5/5] bpf: Improve exception handling in trie_update_elem() Content-Language: en-GB From: Markus Elfring To: bpf@vger.kernel.org, netdev@vger.kernel.org, kernel-janitors@vger.kernel.org, Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Hao Luo , Jiri Olsa , John Fastabend , KP Singh , Martin KaFai Lau , Song Liu , Stanislav Fomichev , Yonghong Song Cc: LKML References: <7011cdcc-4287-4e63-8bfa-f08710f670b1@web.de> In-Reply-To: <7011cdcc-4287-4e63-8bfa-f08710f670b1@web.de> X-Provags-ID: V03:K1:nOmhdZE/XojShpXxlNPMot4rOnwZaU6Vbm8qG7aVBHLczhSGTff oFY53srdHKkx2klXiV3k5r75jE14PrauuNDQQk0A0DEiUvC0kxHEHKGSxYULfA4cAXnr8Ca VS0JMBi8p5FvKtCzxRECuWcWJUgstQ+HeYbnKodFbBLbrXX5YT20nECbObq69mM/nOsBx7v oQaZ3vJIUG/8P657HrAEw== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:JBDSu+CLZj8=;ozXJh2b3Fcm8mfSjPb/HtbG+Ahn D4GS/b+J0uR4HQTJ0BdEtU3MWAzWWTF7PTyvZ+9eHr9V1WUdECGLSPYwkX+GmuHNxnal0w5Kt pZ0PDZ7GQdyxMeQcdlKXY+YRdgLA0hRq6waIX14BJPiE28ET5dNQbbrA0rAhIJ8Yq8Hgplr8o TAZB3kAApCJjz+QP3hLmgAV+e/9T8JQHdNbAMj5R7mSrpfvT22rdADfb63F5qCuFmtgp/vjlt TGADwmvzR3ygwV0zsAAzD5R5ASISGlp4COEHGX0vuJAx4v34arHbM8V4FRS2CTf2k3/0uRcJX Rgu5nzu7DoiqETbUzr8O77gvlqECQV7IZ5asijUxrg4tEdB/t5r4fbng922H+CuT3FFTqWQ0i Ns9FaBc9JBQfGIf6LADdC5hJWax6w4/ZMLxK8l3WISQpQDLV1aU7NvDr3tgEnGwbUUCEWlkoS MYVDbKh6XogZZ4SuVu3TRRRvHdFH0Rtx6G+clJyTleKXqg1MZXHBI5pMNliRHknmn3Wn6PaTn LbpFBfHhvrabrL+DLRoURyEVarQYnvKZ7k40RslsZEVdTYVzkS2jTq4fZBbRzDwG8ku3gA8X4 NhQiXJbGFCcHIAy5OH72hYYijt3Vb06uEKBqtygRAIqDnxJLXBDK5hMCYOl80Y9djLiojujis NUipm0hynEQozyn1enwGSFMdmUsIw3Qd4JG04v5V0GBOLjSZx2JwsHXCt0Bt+52S7bOdHkZLV yBx+WswKZebQGbJ4ubZmsSXJOPq73zkSyejUpYBLelPfn1o7o5K7Iy53mBrI73s+472kxWjoL W+FLnUmJBse3wG6eELOas91R7dwHk3V/KeyBpz3OqtYawGCxcTf0F4TPXBvHfKbAV8JAJ5qLK kf0SDsGg/WvYyd3TIr+7ZWK6nytMZMuh9ibVhqFvPjTAGusTqBFVnDUbx4xIYI4MrzFPKWMf+ f+bj+Q== X-Patchwork-Delegate: bpf@iogearbox.net From: Markus Elfring Date: Sat, 30 Dec 2023 20:28:11 +0100 The kfree() function was called in some cases by the trie_update_elem() function during error handling even if the passed variable contained a null pointer. This issue was detected by using the Coccinelle software. * Thus adjust jump targets. * Reorder data processing steps at the end. * Delete an initialisation (for the variable “new_node”) and a repeated pointer check which became unnecessary with this refactoring. Signed-off-by: Markus Elfring --- kernel/bpf/lpm_trie.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) -- 2.43.0 diff --git a/kernel/bpf/lpm_trie.c b/kernel/bpf/lpm_trie.c index b32be680da6c..6c372d831d0f 100644 --- a/kernel/bpf/lpm_trie.c +++ b/kernel/bpf/lpm_trie.c @@ -307,7 +307,7 @@ static long trie_update_elem(struct bpf_map *map, void *_key, void *value, u64 flags) { struct lpm_trie *trie = container_of(map, struct lpm_trie, map); - struct lpm_trie_node *node, *im_node = NULL, *new_node = NULL; + struct lpm_trie_node *node, *im_node = NULL, *new_node; struct lpm_trie_node __rcu **slot; struct bpf_lpm_trie_key *key = _key; unsigned long irq_flags; @@ -327,13 +327,13 @@ static long trie_update_elem(struct bpf_map *map, if (trie->n_entries == trie->map.max_entries) { ret = -ENOSPC; - goto out; + goto unlock; } new_node = lpm_trie_node_alloc(trie, value); if (!new_node) { ret = -ENOMEM; - goto out; + goto unlock; } trie->n_entries++; @@ -368,7 +368,7 @@ static long trie_update_elem(struct bpf_map *map, */ if (!node) { rcu_assign_pointer(*slot, new_node); - goto out; + goto decrement_counter; } /* If the slot we picked already exists, replace it with @new_node @@ -384,7 +384,7 @@ static long trie_update_elem(struct bpf_map *map, rcu_assign_pointer(*slot, new_node); kfree_rcu(node, rcu); - goto out; + goto decrement_counter; } /* If the new node matches the prefix completely, it must be inserted @@ -394,13 +394,13 @@ static long trie_update_elem(struct bpf_map *map, next_bit = extract_bit(node->data, matchlen); rcu_assign_pointer(new_node->child[next_bit], node); rcu_assign_pointer(*slot, new_node); - goto out; + goto decrement_counter; } im_node = lpm_trie_node_alloc(trie, NULL); if (!im_node) { ret = -ENOMEM; - goto out; + goto decrement_counter; } im_node->prefixlen = matchlen; @@ -419,15 +419,13 @@ static long trie_update_elem(struct bpf_map *map, /* Finally, assign the intermediate node to the determined slot */ rcu_assign_pointer(*slot, im_node); -out: if (ret) { - if (new_node) - trie->n_entries--; - - kfree(new_node); kfree(im_node); +decrement_counter: + trie->n_entries--; + kfree(new_node); } - +unlock: spin_unlock_irqrestore(&trie->lock, irq_flags); return ret;