From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796345480; b=SVh7txGby2IOIF6FjpFUD8wK6IngzcahZ0DgP3zZKcbHxY9JQiDILmmqGd+9UaQTqCx2 glAKSMfFiuAFozN7v1waRIjvmWwxt1e9ko2MbkoqFaj6gBOT/rDosGTnlEylbJucJyUCh wDKejy0LTfWuU2BbQNh/egXbB/hvTEn1aGxeaFtkVtVa5RBVzdCQsKkdI1VzZcXR+mjiX 8x4qiG/NmJoCcloThR+zDWr5LDznwhiogKI7g1pmM8NgeKo4CSi9gcVoSzFYXE0snzSJl +wq2YywZ8BSUITHSJOk/v3Vq3NV6YMhrvg7gZz8M74R6gm7DTHGj9i8NLqOQt0wMNsQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796345480; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=MzvuEo7awDgdq9nurS8NeNNm5+USqR3zqeUSIAVrsN8=; b=38lxQzo3DrXM8ZFSqRVkrgqhzNfHG0CxDqmhHYO7Kclr5tD6CVbl4gNo/s0UBjz3fI7a z5ja34RVKhkQ03F/CIkqmSzryDWaqpdSUbmiwPlUtmr5/OHoSmpo0M9g4u/X7Uut7rR7v sKa00wOkKlH7BxoWsp+8Evh8e98lwEVvJ/vSmaIv3+ora1w/HQ0ZFg4IxDzX+XgL41swT efiiynKYuf0+yctX+BDq90lTFoskACet4tITz6qXNR/r4gIwER9WpWhOTMiQXR1LP6Rgg M6l5XgFyOV9zaFPy/o6RDLCmyi2S1hSFWbfopXcUirWOzNrvtRVC2Fl1FpEfSRVxc4g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=OH84aAWF; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3539F606A322 for ; Fri, 4 Dec 2026 00:51:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=OH84aAWF Received: by smtp.kernel.org (Postfix) with ESMTPSA id 722311F000C2; Fri, 4 Dec 2026 00:51:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796345479; bh=MzvuEo7awDgdq9nurS8NeNNm5+USqR3zqeUSIAVrsN8=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=OH84aAWFujk+W+vdPlqwbOZu/qdwtPJHcyusr16Xp//zeHphCTAdFTh4wgldr3N8O kTh7KB+minlxnf9MGgFWh/gPSkZtMrWYNyFg3iJvJgfyd+0+/YO4L12IQMGE6oSkZO Oz1VA1bxsxIAquqLMFoyCBg/aZmS5HyH2K8wLT/766YXHUMaJc9pk6W7TyKVbY3Ta9 I8TT/+Bu1CFL1lM/0r3yJQPIj+X9xkGk3PxiFkk/8DkUTkZ54sWrVOk/LJ7qjR4Z8J AOB5FD644lPoUyR+eZY0KkUeTt22B+GZ4gaTNVvhzV6476VKWYDIrvvmaWhueCY54V DlqeuqiEZVttA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH 1/2] livepatch: look up klp_ops in a hash table Reply-To: sashiko-reviews@lists.linux.dev To: =?utf-8?b?Wmh1LCBZaQ==?= , "Josh Poimboeuf" , "Miroslav Benes" , "Petr Mladek" , "Joe Lawrence" , "Song Liu" , "Wilson Felipe Pereira" , "Dylan Hatch" Cc: live-patching@vger.kernel.org In-Reply-To: <3f1027446e0fb57f76fca545840f99f19fa0237c.1796344045.git.chuyee@google.com> References: <3f1027446e0fb57f76fca545840f99f19fa0237c.1796344045.git.chuyee@google.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 00:51:19 +0000 X-Mailing-List: live-patching@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] livepatch: NULL deref in klp_check_stack_func() during klp_unpatch_func() - [Medium] livepatch: missing obj->patched ordering before klp_find_ops() in stack check -- commit cf2b103b9d30e9b6b5eff152bfd055a1002ecd44 Author: Zhu, Yi livepatch: look up klp_ops in a hash table This commit replaces the global klp_ops linked list with an RCU-protected hash table keyed on old_func to speed up klp_find_ops() lookups, moving entry publication after ftrace registration and unpublishing before ftrace unregistration with kfree_rcu(). > diff --git a/kernel/livepatch/patch.c b/kernel/livepatch/patch.c > index 3f54a017bbf69..e93105187bc43 100644 > --- a/kernel/livepatch/patch.c > +++ b/kernel/livepatch/patch.c [ ... ] > @@ -144,12 +150,17 @@ static void klp_unpatch_func(struct klp_func *func) > if (WARN_ON(!ftrace_loc)) > return; > =20 > + /* > + * Unpublish ops before tearing it down. Lockless > + * klp_find_ops() readers may still see it until a grace > + * period has elapsed, hence kfree_rcu(). > + */ > + hash_del_rcu(&ops->node); > WARN_ON(unregister_ftrace_function(&ops->fops)); > WARN_ON(ftrace_set_filter_ip(&ops->fops, ftrace_loc, 1, 0)); > =20 > list_del_rcu(&func->stack_node); > - list_del(&ops->node); > - kfree(ops); > + kfree_rcu(ops, rcu); > } else { > list_del_rcu(&func->stack_node); > } [Severity: High] Can unpublishing ops before unregister_ftrace_function() cause a NULL pointer dereference in klp_check_stack_func()? When a target module is unloaded during an active transition, __klp_unpatch_object() leaves obj->patched true until every klp_unpatch_func() call finishes. For FTRACE_OPS_FL_DYNAMIC livepatch ops, unregister_ftrace_function() blocks across synchronize_rcu_tasks_rude() and synchronize_rcu_tasks(), keeping ops unpublished for milliseconds while tasks schedule on other CPUs: CPU0 CPU1 klp_module_going() ... klp_unpatch_object(obj) klp_unpatch_func(func) hash_del_rcu(&ops->node) unregister_ftrace_function(...) __schedule() __klp_sched_try_switch() klp_check_stack() /* obj->patched is still true */ klp_check_stack_func() ops =3D klp_find_ops(...) /* NULL */ list_is_singular(&ops->func_stack) <-- NULL pointer dereference Even if klp_find_ops() returns ops just before hash_del_rcu(), list_del_rcu(&func->stack_node) can empty ops->func_stack before the reader checks list_is_singular(), sending klp_check_stack_func() into the else branch with a type-confused list_next_entry(func, stack_node). [ ... ] > @@ -211,7 +221,12 @@ static int klp_patch_func(struct klp_func *func) > goto err; > } > =20 > - > + /* > + * Publish ops for lockless klp_find_ops() readers only once it > + * is fully set up, so that the error path above can free it > + * right away. > + */ > + hash_add_rcu(klp_ops_table, &ops->node, (unsigned long)ops->old_func); > } else { > list_add_rcu(&func->stack_node, &ops->func_stack); > } [Severity: Medium] On weakly-ordered architectures, can obj->patched become visible to klp_check_stack() before the hash_add_rcu() bucket update? Moving hash_add_rcu() after register_ftrace_function() leaves no barrier ordering it before klp_patch_object() sets obj->patched =3D true, because hash_add_rcu()'s store-release only orders prior stores before the bucket write. In klp_check_stack(), the plain load of obj->patched has only a control dependency before klp_find_ops() reads klp_ops_table: CPU0 CPU1 klp_module_coming() klp_patch_object(obj) klp_patch_func(func) hash_add_rcu(...) obj->patched =3D true __klp_sched_try_switch() klp_check_stack() if (!obj->patched) /* sees true */ klp_check_stack_func() klp_find_ops(...) /* sees NULL */ list_is_singular(&ops->func_stack) <-- NULL pointer dereference Publishing obj->patched with smp_store_release() paired with smp_load_acquire() in klp_check_stack() would order the hash table update before the reader's lookup. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/cover.1796344045.gi= t.chuyee@google.com?part=3D1