Live Patching
 help / color / mirror / Atom feed
From: Yafang Shao <laoar.shao@gmail.com>
To: jpoimboe@kernel.org, mbenes@suse.cz, pmladek@suse.com,
	joe.lawrence@redhat.com, song@kernel.org
Cc: live-patching@vger.kernel.org, Yafang Shao <laoar.shao@gmail.com>
Subject: [PATCH 1/2] livepatch: reject nop flag set by a patch
Date: Mon, 16 Nov 2026 21:04:01 +0800	[thread overview]
Message-ID: <20261116130402.76743-2-laoar.shao@gmail.com> (raw)
In-Reply-To: <20261116130402.76743-1-laoar.shao@gmail.com>

A patch module can incorrectly set .nop = true in its own struct
klp_func:

  static struct klp_func funcs[] = {
      {
          .old_name = "cmdline_proc_show",
	      .nop = true,
      },
      { }
      };

.nop is internal to livepatch: it is set only by klp_alloc_func_nop(),
for functions that the patch does not replace.  When the patch module is
removed, klp_kobj_release_func() trusts the flag and hands any func that
has it set to klp_free_func_nop():

  klp_kobj_release_func
  + if (func->nop)
        klp_free_func_nop(func);

For a func that was never allocated by klp_alloc_func_nop(), the free
operates on memory that livepatch does not own, and the kernel warns:

[  679.530851] ------------[ cut here ]------------
[  679.530853] WARNING: mm/slub.c:6855 at free_large_kmalloc+0xc0/0xe0, CPU#55: kworker/55:1/794
[  679.531695] Modules linked in: test_klp_nop_bad(OK) vfat fat intel_rapl_msr intel_rapl_common kvm_amd ccp iTCO_wdt kvm virtio_gpu virtio_dma_buf drm_shmem_helper i2c_i801 drm_client_lib i2c_mux irqbypass i2c_smbus drm_kms_helper pcspkr lpc_ich virtio_balloon input_leds sch_fq_codel drm fuse xfs ahci libahci virtio_net aesni_intel gf128mul net_failover libata virtio_rng virtio_blk virtio_console failover serio_raw sunrpc dm_mirror dm_region_hash dm_log dm_mod qemu_fw_cfg
[  679.533370] CPU: 55 UID: 0 PID: 794 Comm: kworker/55:1 Kdump: loaded Tainted: G           O  K     7.4.0-rc1+ #270 PREEMPT(full)
[  679.533841] Tainted: [O]=OOT_MODULE, [K]=LIVEPATCH
[  679.534047] Hardware name: Red Hat KVM/RHEL, BIOS 1.16.3-5.el9 11/05/2023
[  679.534320] Workqueue: events klp_transition_work_fn
[  679.534530] RIP: 0010:free_large_kmalloc+0xc0/0xe0
[  679.534729] Code: ed 80 3d d4 23 d8 01 00 ba 00 f0 ff ff 75 9d 48 c7 c7 62 a7 94 b3 89 55 ec c6 05 bc 23 d8 01 01 e8 b5 8e cf ff 8b 55 ec eb 82 <0f> 0b 48 c7 c6 49 a7 94 b3 4c 89 e7 e8 1f 41 fa ff 48 83 c4 08 41
[  679.535478] RSP: 0018:ff7be23881c47cb0 EFLAGS: 00010202
[  679.535697] RAX: 00000000000000ff RBX: ffffffffc0e50280 RCX: 0000000000000001
[  679.535999] RDX: 0000000000000000 RSI: ffffffffc0e53054 RDI: ffb8a30e57d794c0
[  679.536287] RBP: ff7be23881c47cc8 R08: ff3c2d170ff94e58 R09: ff3c2d17008b7210
[  679.536574] R10: ff7be23881c47c98 R11: ff7be23881c47ca0 R12: ffb8a30e57d794c0
[  679.536865] R13: ffb8a30e57d794c0 R14: ffffffffc0e50138 R15: ffffffffc0e50260
[  679.537163] FS:  0000000000000000(0000) GS:ff3c2d3649d15000(0000) knlGS:0000000000000000
[  679.537490] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  679.537723] CR2: 000055eb5f6a5158 CR3: 00000015e9020004 CR4: 0000000000771ef0
[  679.538023] PKRU: 55555554
[  679.538139] Call Trace:
[  679.538249]  <TASK>
[  679.538348]  kfree+0x171/0x3a0
[  679.538483]  ? srso_alias_return_thunk+0x5/0xfbef5
[  679.538681]  ? kernfs_put.part.0+0xcf/0x150
[  679.538869]  klp_kobj_release_func+0x35/0x60
[  679.539061]  kobject_cleanup+0x44/0x1b0
[  679.539236]  kobject_put+0x40/0x70
[  679.539383]  __klp_free_objects+0xaa/0x160
[  679.539556]  klp_free_patch_async+0x52/0x80
[  679.539730]  klp_try_complete_transition+0x2dc/0x310
[  679.539941]  klp_transition_work_fn+0x24/0x40
[  679.540131]  process_one_work+0x1db/0x410
[  679.540306]  worker_thread+0x1bc/0x330
[  679.540466]  kthread+0xfd/0x140
[  679.540603]  ? __pfx_worker_thread+0x10/0x10
[  679.540783]  ? __pfx_kthread+0x10/0x10
[  679.540947]  ret_from_fork+0x178/0x250
[  679.541115]  ? __pfx_kthread+0x10/0x10
[  679.541273]  ret_from_fork_asm+0x1a/0x30
[  679.541445]  </TASK>
[  679.541542] ---[ end trace 0000000000000000 ]---
[  679.541738] page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x15f5e53
[  679.541744] flags: 0x17ffffc0000000(node=0|zone=2|lastcpupid=0x1fffff)
[  679.541747] raw: 0017ffffc0000000 ffb8a30e57d794c8 ffb8a30e57d794c8 0000000000000000
[  679.541749] raw: 0000000000000000 0000000000000000 00000000ffffffff 0000000000000000
[  679.541750] page dumped because: Not a kmalloc allocation
[  679.541756] page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x15f5e50
[  679.541758] flags: 0x17ffffc0000000(node=0|zone=2|lastcpupid=0x1fffff)
[  679.541759] raw: 0017ffffc0000000 ffb8a30e57d79408 ffb8a30e57d79408 0000000000000000
[  679.541760] raw: 0000000000000000 0000000000000000 00000000ffffffff 0000000000000000
[  679.541761] page dumped because: Not a kmalloc allocation

A simple reproducer:

  #include <linux/module.h>
  #include <linux/kernel.h>
  #include <linux/livepatch.h>

  static struct klp_func funcs[] = {
          {
                  .old_name = "cmdline_proc_show",
                  .nop = true,
          },
          { }
  };

  static struct klp_object objs[] = {
          { .funcs = funcs, },
          { }
  };

  static struct klp_patch patch = {
          .mod = THIS_MODULE,
          .objs = objs,
  };

  static int test_klp_nop_bad_init(void)
  {
          return klp_enable_patch(&patch);
  }

  static void test_klp_nop_bad_exit(void)
  {
  }

  module_init(test_klp_nop_bad_init);
  module_exit(test_klp_nop_bad_exit);

  MODULE_LICENSE("GPL");
  MODULE_DESCRIPTION("livepatch reproducer: patch-supplied nop");
  MODULE_INFO(livepatch, "Y");

Fix this by rejecting such patches at initialization time, so a patch can
no longer reach klp_kobj_release_func() with a bogus .nop.

After this fix, the result is:

[   47.563594] livepatch: test_klp_nop_bad: func 'cmdline_proc_show' must not set the internal nop flag

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
 include/linux/livepatch.h | 1 +
 kernel/livepatch/core.c   | 5 +++++
 2 files changed, 6 insertions(+)

diff --git a/include/linux/livepatch.h b/include/linux/livepatch.h
index 5f74f79c22b4..76ba613830a6 100644
--- a/include/linux/livepatch.h
+++ b/include/linux/livepatch.h
@@ -37,6 +37,7 @@
  * @old_size:	size of the old function
  * @new_size:	size of the new function
  * @nop:        temporary patch to use the original code again; dyn. allocated
+ *              internal only, must not be set by a patch
  * @patched:	the func has been added to the klp_ops list
  * @transition:	the func is currently being applied or reverted
  *
diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c
index 517fe427ff92..ba09894eec2c 100644
--- a/kernel/livepatch/core.c
+++ b/kernel/livepatch/core.c
@@ -1113,6 +1113,11 @@ static int klp_check_patch(struct klp_patch *patch)
 		klp_for_each_func_static(obj, func) {
 			if (!func->old_name)
 				return -EINVAL;
+			if (func->nop) {
+				pr_err("%s: func '%s' must not set the internal nop flag\n",
+					patch->mod->name, func->old_name);
+				return -EINVAL;
+			}
 		}
 	}
 	return 0;
-- 
2.52.0


  reply	other threads:[~2026-11-16 13:04 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 13:04 [PATCH 0/2] livepatch: reject internal flags set by a patch Yafang Shao
2026-11-16 13:04 ` Yafang Shao [this message]
2026-11-16 13:13   ` [PATCH 1/2] livepatch: reject nop flag " sashiko-bot
2026-11-16 13:04 ` [PATCH 2/2] livepatch: reject dynamic " Yafang Shao
2026-11-16 13:18 ` [PATCH 0/2] livepatch: reject internal flags " Miroslav Benes
2026-11-16 13:37   ` Yafang Shao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261116130402.76743-2-laoar.shao@gmail.com \
    --to=laoar.shao@gmail.com \
    --cc=joe.lawrence@redhat.com \
    --cc=jpoimboe@kernel.org \
    --cc=live-patching@vger.kernel.org \
    --cc=mbenes@suse.cz \
    --cc=pmladek@suse.com \
    --cc=song@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox