From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.69 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796359904; b=V2LYfk7GiCb/c1j6hKvLjFSE7zqn2wXnvpc/ehp28AdXFJ/IwCUEDiguhRhGPj9iSM1B lKDOQ7W+71xLKWl/hSQKr3ELRun3nDUuMOt9630lWA9jDKGlm8hVfhgQrsBVkgnxTlWlm faXGZZ2b3U939DF/8L7fze7Xs/xckgO1W1pgPXnA/6ljmZUCWqV2CgECuRc0/HshPHPkS ypVPSGmSFHSOkufgYqeuL9Bi6EPSHBhFtekKCowFwDmzQhrdiiqk+7+PeTpa7B9ADNliE dciAEbhIPdFL+WB3zOZw827KykgVfOJ4HnCCZMWPMOtxuumX9OFsBQcYptPvMPvddcw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796359904; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=wGKFZhqprk215hqYU7Cjsj3HIVfgl43xMt4wc5IR5iI=; b=VFiRcHTgsWz/OrsD3RR907mHy8vAp8YndPXcQt/7UXuouyrUuwBvd13Y2RunACPtWO7Z uzF01LEPwJxDT8YLbzDeM3pjJQ1XgD4UErUIgR8eikUOBPVtrlYsinQbazftQfMZnhQgx pcsfJWR8lXuXf7cZkMoa9WU1dgvjXgVRvmHiNijYb8YGVIEjAITjt4ARJJGaLKHWxA0+v TZTP/v5SiIFhWzZIQtpfCEwVanbLxqEnYrhbz0TZhA1NaWZxBcJ6N+luYOCK2jVFzsi1h T8gM9nkpQckqzTqFRP71q9XLWdz86r83rBOlI7419vo3xlmedIRUpQvyEwRFZx2WugQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--chuyee.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=QeSstuxP; arc=none smtp.remote-ip=74.125.82.69 Received: from mail-dl1-f69.google.com (mail-dl1-f69.google.com [74.125.82.69]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 56A83606A332 for ; Fri, 4 Dec 2026 04:51:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 56A83606A332 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--chuyee.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=QeSstuxP Received: by mail-dl1-f69.google.com with SMTP id a92af1059eb24-1b5d5e46459so1121378c88.0 for ; Thu, 03 Dec 2026 20:51:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796359903; x=1796964703; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=wGKFZhqprk215hqYU7Cjsj3HIVfgl43xMt4wc5IR5iI=; b=QeSstuxPRAn16tdsM1VUM3yU5V2TM0SfPLA8LyFQIpv3NRYoRVPyPzvl7Nh0UHI6oD CxdUWQMMxxsr7dQE9h3BKQ5aSAZ7imc5XVMli5+DTZ/gk6LWuTYvmMLOefhmyNiINLhx isXQ9hkIiC2Aqs9js/xTm6AYgCDDh6I+80vMZaf8YuDhFcMDVrXsHR03NfKs4UiOAgWG TsZ6a3uAi+qT4C3gzNAHZ6YsAwbNfvVtRTY/4+sOPXkS5TtdMGzAA37TKXH5p/+wpOX1 A7uELvIyrEkA9EAnzzxp6aTVG4HDHhrwns6OmYJ+ScpUalsxEMlWAN83Rcb/g0yfsdCv JfHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796359903; x=1796964703; h=content-type: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:content-type; bh=wGKFZhqprk215hqYU7Cjsj3HIVfgl43xMt4wc5IR5iI=; b=xx7+kv23WSl3422IHM/S1wam2k29e6e7+P4pwCQNjjMNAGc+h/LddZXGGE4b3Dztgx JIHzzsUr1RGdsTzMdyTzXf3J7j9UzutQRlqno1y16DI0l6UZPr51Z8/bkNjBQVg/tLho ud5EnLeEeVE16PFTn/bfQtjmtFvAy/3HSbsw6JGi8+wem8IZzcF1B3Ab9MW7uFg9LIX5 hA4vQFtR+dWcCh6eZXiA3qmU47HeLvTGBlWVf/Nf/IECFpf7PgqgH1WZFSndUR5GO57+ DuDZ3+5MiHAOFMiMJ6Y5etoZs5HDW130p3KgTQ+O2wVpbzbxHQBOKIzFNVeeD48P9Ugh RiSQ== X-Forwarded-Encrypted: i=1; AL+hErF+l+gWo4kTt9tR1/btfy4CSmBQJngk14PB1kzC65PKFV0leR2JfUKrQbf8lNdmKdIb16zWPTGkK2+b8tA=@vger.kernel.org X-Gm-Message-State: APuU4Z9q9l7/8BJ/I0Pd1r2+lAY29ephyneDPAJJTKY0OF8iFo8+KFwm /8P3z7Rl2THUF+ElPr3h46wjOPgEH1hjMiiPqlas6XKjkFMDPlewbb2KQ9Ef8okhLKnyxBRuTHC OCcgmjQ== X-Received: from dlgp34-n1.prod.google.com ([2002:a05:7022:b162:10b0:1b5:b713:6bba]) (user=chuyee job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7023:e41:10b0:1ac:1022:b60c with SMTP id a92af1059eb24-1b5d7063c6bmr858738c88.1.1796359903158; Thu, 03 Dec 2026 20:51:43 -0800 (PST) Date: Fri, 4 Dec 2026 04:51:39 +0000 In-Reply-To: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.1.987.g2ec0ab4260-goog Message-ID: <2c9187f276d603835bf98ab8c60374e68deb1c03.1796357900.git.chuyee@google.com> Subject: [PATCH v2 1/3] livepatch: fix racy klp_ops lookup in klp_check_stack_func() From: "Zhu, Yi" To: Josh Poimboeuf , Miroslav Benes , Petr Mladek , Joe Lawrence , Song Liu , Wilson Felipe Pereira , Dylan Hatch Cc: live-patching@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Since commit e3ff7c609f39 ("livepatch,sched: Add livepatch task switching to cond_resched()"), klp_check_stack() can run from __klp_sched_try_switch() with only preemption disabled and without klp_mutex. klp_module_coming() and klp_module_going() can patch and unpatch an object of the transition patch at the same time, under klp_mutex, and the stack check's klp_find_ops() lookup isn't safe against that: - klp_find_ops() walks the global klp_ops list with plain list primitives while klp_patch_func() adds to it and klp_unpatch_func() removes from it and frees the klp_ops with kfree() right away. - klp_unpatch_object() only clears obj->patched after all funcs are unpatched, and unregister_ftrace_function() sleeps for each of them. Meanwhile the stack check still visits the object, and klp_find_ops() returns NULL for funcs that are already unpatched, which klp_check_stack_func() dereferences. - Removing the last func from a func_stack leaves it empty, so both klp_find_ops() and the list_is_singular()/list_next_entry() logic in klp_check_stack_func() can treat the list head as a klp_func. - klp_patch_object() sets obj->patched without ordering it after the func_stack and klp_ops updates. Store old_func in struct klp_ops and match on it, so a lookup doesn't depend on the func_stack contents. Add and remove klp_ops with the RCU list primitives, publish one only once its ftrace ops is registered, unpublish it before emptying its func_stack and free it with kfree_rcu(). Readers already run with preemption disabled. In klp_check_stack_func(), skip a func whose klp_ops is gone, and find the previous function through func->stack_node.next, which list_del_rcu() leaves intact. Such a func belongs to a module that is coming or going, so none of its code can be on a stack, and the core doesn't check stacks when it patches or unpatches such an object anyway. Set obj->patched with smp_store_release() and read it with smp_load_acquire() in klp_check_stack(). klp_free_object_loaded() also clears func->old_func and, for NOPs, func->new_func right after unpatching, so mark those accesses with WRITE_ONCE()/READ_ONCE() and use the immutable ops->old_func once the lookup succeeds. Fixes: e3ff7c609f39 ("livepatch,sched: Add livepatch task switching to cond_resched()") Signed-off-by: "Zhu, Yi" --- kernel/livepatch/core.c | 5 ++-- kernel/livepatch/patch.c | 43 ++++++++++++++++++++++++----------- kernel/livepatch/patch.h | 4 ++++ kernel/livepatch/transition.c | 37 ++++++++++++++++++++++++------ 4 files changed, 67 insertions(+), 22 deletions(-) diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c index b83715272e8c..92fbb524165a 100644 --- a/kernel/livepatch/core.c +++ b/kernel/livepatch/core.c @@ -724,11 +724,12 @@ static void klp_free_object_loaded(struct klp_object *obj) obj->mod = NULL; + /* klp_check_stack() may still read these locklessly. */ klp_for_each_func(obj, func) { - func->old_func = NULL; + WRITE_ONCE(func->old_func, NULL); if (func->nop) - func->new_func = NULL; + WRITE_ONCE(func->new_func, NULL); } } diff --git a/kernel/livepatch/patch.c b/kernel/livepatch/patch.c index 3f54a017bbf6..7330a189d6d0 100644 --- a/kernel/livepatch/patch.c +++ b/kernel/livepatch/patch.c @@ -20,17 +20,21 @@ #include "patch.h" #include "transition.h" +/* + * All klp_ops. Updates are serialized by klp_mutex. Lookups normally hold + * klp_mutex too, but the stack check can also run from __klp_sched_try_switch() + * with only preemption disabled, so entries are added and removed with the RCU + * list primitives. + */ static LIST_HEAD(klp_ops); struct klp_ops *klp_find_ops(void *old_func) { struct klp_ops *ops; - struct klp_func *func; - list_for_each_entry(ops, &klp_ops, node) { - func = list_first_entry(&ops->func_stack, struct klp_func, - stack_node); - if (func->old_func == old_func) + list_for_each_entry_rcu(ops, &klp_ops, node, + lockdep_is_held(&klp_mutex)) { + if (ops->old_func == old_func) return ops; } @@ -147,9 +151,14 @@ static void klp_unpatch_func(struct klp_func *func) WARN_ON(unregister_ftrace_function(&ops->fops)); WARN_ON(ftrace_set_filter_ip(&ops->fops, ftrace_loc, 1, 0)); + /* + * Unpublish ops before emptying its func_stack. Lockless + * klp_find_ops() readers may still see it until a grace + * period has elapsed, hence kfree_rcu(). + */ + list_del_rcu(&ops->node); list_del_rcu(&func->stack_node); - list_del(&ops->node); - kfree(ops); + kfree_rcu(ops, rcu); } else { list_del_rcu(&func->stack_node); } @@ -191,8 +200,7 @@ static int klp_patch_func(struct klp_func *func) FTRACE_OPS_FL_IPMODIFY | FTRACE_OPS_FL_PERMANENT; - list_add(&ops->node, &klp_ops); - + ops->old_func = func->old_func; INIT_LIST_HEAD(&ops->func_stack); list_add_rcu(&func->stack_node, &ops->func_stack); @@ -211,7 +219,12 @@ static int klp_patch_func(struct klp_func *func) goto err; } - + /* + * 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. + */ + list_add_rcu(&ops->node, &klp_ops); } else { list_add_rcu(&func->stack_node, &ops->func_stack); } @@ -222,7 +235,6 @@ static int klp_patch_func(struct klp_func *func) err: list_del_rcu(&func->stack_node); - list_del(&ops->node); kfree(ops); return ret; } @@ -240,7 +252,7 @@ static void __klp_unpatch_object(struct klp_object *obj, bool nops_only) } if (obj->dynamic || !nops_only) - obj->patched = false; + WRITE_ONCE(obj->patched, false); } @@ -264,7 +276,12 @@ int klp_patch_object(struct klp_object *obj) return ret; } } - obj->patched = true; + + /* + * Publish the object to klp_check_stack() only after all of its funcs + * are on their func_stacks. Pairs with smp_load_acquire() there. + */ + smp_store_release(&obj->patched, true); return 0; } diff --git a/kernel/livepatch/patch.h b/kernel/livepatch/patch.h index d5f2fbe373e0..bf3b9017910a 100644 --- a/kernel/livepatch/patch.h +++ b/kernel/livepatch/patch.h @@ -16,13 +16,17 @@ * func_stack (front of the list). * * @node: node for the global klp_ops list + * @old_func: the patched function shared by all klp_func's on @func_stack * @func_stack: list head for the stack of klp_func's (active func is on top) * @fops: registered ftrace ops struct + * @rcu: for freeing after lockless klp_find_ops() readers are done */ struct klp_ops { struct list_head node; + void *old_func; struct list_head func_stack; struct ftrace_ops fops; + struct rcu_head rcu; }; struct klp_ops *klp_find_ops(void *old_func); diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c index ef70ff582da1..07e786c2d812 100644 --- a/kernel/livepatch/transition.c +++ b/kernel/livepatch/transition.c @@ -214,25 +214,47 @@ static int klp_check_stack_func(struct klp_func *func, unsigned long *entries, * Check for the to-be-unpatched function * (the func itself). */ - func_addr = (unsigned long)func->new_func; + func_addr = (unsigned long)READ_ONCE(func->new_func); func_size = func->new_size; } else { + struct list_head *next; + /* * Check for the to-be-patched function * (the previous func). */ - ops = klp_find_ops(func->old_func); + ops = klp_find_ops(READ_ONCE(func->old_func)); + + /* + * klp_module_coming() and klp_module_going() can patch and + * unpatch an object of the transition patch while this runs + * from __klp_sched_try_switch() without klp_mutex. Then ops + * may already be gone, or func may already be off its + * func_stack. The module's code can't be on any stack at that + * point, and the core doesn't check stacks when it patches or + * unpatches such an object anyway, so there is nothing to check. + * For the same reason, func->old_func and func->new_func may be + * cleared by klp_free_object_loaded() meanwhile; use the + * immutable ops->old_func once ops is found. + * + * list_del_rcu() leaves func->stack_node.next pointing to the + * previous func or to the func_stack head, so use it rather than + * list_is_singular(), which an emptied func_stack would defeat. + */ + if (!ops) + return 0; - if (list_is_singular(&ops->func_stack)) { + next = READ_ONCE(func->stack_node.next); + if (next == &ops->func_stack) { /* original function */ - func_addr = (unsigned long)func->old_func; + func_addr = (unsigned long)ops->old_func; func_size = func->old_size; } else { /* previously patched function */ struct klp_func *prev; - prev = list_next_entry(func, stack_node); - func_addr = (unsigned long)prev->new_func; + prev = list_entry(next, struct klp_func, stack_node); + func_addr = (unsigned long)READ_ONCE(prev->new_func); func_size = prev->new_size; } } @@ -267,7 +289,8 @@ static int klp_check_stack(struct task_struct *task, const char **oldname) nr_entries = ret; klp_for_each_object(klp_transition_patch, obj) { - if (!obj->patched) + /* Pairs with smp_store_release() in klp_patch_object(). */ + if (!smp_load_acquire(&obj->patched)) continue; klp_for_each_func(obj, func) { ret = klp_check_stack_func(func, entries, nr_entries); -- 2.56.1.987.g2ec0ab4260-goog