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 2/2] livepatch: reject dynamic flag set by a patch
Date: Mon, 16 Nov 2026 21:04:02 +0800	[thread overview]
Message-ID: <20261116130402.76743-3-laoar.shao@gmail.com> (raw)
In-Reply-To: <20261116130402.76743-1-laoar.shao@gmail.com>

A patch module may set the internal "dynamic" flag on its own klp_object:

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

klp_check_patch() does not validate it. klp_kobj_release_object() however
uses the flag to decide whether the object was allocated by the core:

  klp_kobj_release_object
  + if (obj->dynamic)
        klp_free_object_dynamic(obj);

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

[   51.132051] test_klp_obj_dynamic: loading out-of-tree module taints kernel.
[   51.132057] test_klp_obj_dynamic: tainting kernel with TAINT_LIVEPATCH
[   51.136303] livepatch: symbol 'this_symbol_does_not_exist' not found in symbol table
[   51.136352] ------------[ cut here ]------------
[   51.136354] WARNING: mm/slub.c:6855 at free_large_kmalloc+0xc0/0xe0, CPU#21: insmod/2189
[   51.137260] Modules linked in: test_klp_obj_dynamic(OK+) vfat fat intel_rapl_msr intel_rapl_common kvm_amd ccp iTCO_wdt virtio_gpu kvm virtio_dma_buf drm_shmem_helper i2c_i801 drm_client_lib i2c_mux irqbypass drm_kms_helper i2c_smbus pcspkr virtio_balloon lpc_ich input_leds sch_fq_codel drm fuse xfs ahci libahci virtio_net aesni_intel net_failover gf128mul libata virtio_rng virtio_blk virtio_console failover serio_raw sunrpc dm_mirror dm_region_hash dm_log dm_mod qemu_fw_cfg
[   51.138982] CPU: 21 UID: 0 PID: 2189 Comm: insmod Kdump: loaded Tainted: G           O  K     7.4.0-rc1+ #271 PREEMPT(full)
[   51.139431] Tainted: [O]=OOT_MODULE, [K]=LIVEPATCH
[   51.139636] Hardware name: Red Hat KVM/RHEL, BIOS 1.16.3-5.el9 11/05/2023
[   51.139928] RIP: 0010:free_large_kmalloc+0xc0/0xe0
[   51.140129] Code: ed 80 3d 94 23 d8 01 00 ba 00 f0 ff ff 75 9d 48 c7 c7 c2 a7 94 97 89 55 ec c6 05 7c 23 d8 01 01 e8 75 8e cf ff 8b 55 ec eb 82 <0f> 0b 48 c7 c6 a9 a7 94 97 4c 89 e7 e8 1f 41 fa ff 48 83 c4 08 41
[   51.140888] RSP: 0018:ff5331d9031c3aa0 EFLAGS: 00010202
[   51.141105] RAX: 00000000000000ff RBX: ffffffffc0bfb138 RCX: 000000000000000a
[   51.141397] RDX: 0000000000000000 RSI: ffffffffc0bfb100 RDI: ffa280331686fec0
[   51.141707] RBP: ff5331d9031c3ab8 R08: ff22d0898472eaa0 R09: ff22d089808b7210
[   51.141999] R10: ff5331d9031c3a88 R11: ff5331d9031c3a90 R12: ffa280331686fec0
[   51.142288] R13: ffa280331686fec0 R14: ffffffffc0bfb050 R15: ffffffffc0bfb118
[   51.142584] FS:  00007f55e9bed740(0000) GS:ff22d0a8e5495000(0000) knlGS:0000000000000000
[   51.142925] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   51.143161] CR2: 00005611a26eee48 CR3: 00000001021db005 CR4: 0000000000771ef0
[   51.143450] PKRU: 55555554
[   51.143572] Call Trace:
[   51.143693]  <TASK>
[   51.143792]  kfree+0x171/0x3a0
[   51.143927]  ? srso_alias_return_thunk+0x5/0xfbef5
[   51.144128]  ? kernfs_put.part.0+0xcf/0x150
[   51.144314]  klp_kobj_release_object+0x3e/0x60
[   51.144511]  kobject_cleanup+0x44/0x1b0
[   51.144700]  kobject_put+0x40/0x70
[   51.144848]  __klp_free_objects+0x114/0x160
[   51.145028]  klp_enable_patch.part.0+0x29e/0x4f0
[   51.145227]  klp_enable_patch+0xb4/0xf0
[   51.145390]  ? __pfx_init_module+0x10/0x10 [test_klp_obj_dynamic]
[   51.145645]  test_klp_obj_dynamic_init+0x15/0x20 [test_klp_obj_dynamic]
[   51.145929]  do_one_initcall+0x45/0x300
[   51.146092]  ? do_init_module+0x28/0x2a0
[   51.146262]  do_init_module+0x6a/0x2a0
[   51.146422]  load_module+0x861/0x980
[   51.146581]  ? srso_alias_return_thunk+0x5/0xfbef5
[   51.146794]  ? kernel_read_file+0x23c/0x2c0
[   51.146976]  init_module_from_file+0xdf/0x100
[   51.147163]  idempotent_init_module+0x170/0x270
[   51.147355]  __x64_sys_finit_module+0x73/0xf0
[   51.147544]  x64_sys_call+0x2043/0x21d0
[   51.147723]  do_syscall_64+0x97/0x460
[   51.147884]  entry_SYSCALL_64_after_hwframe+0x76/0x7e
[   51.148095] RIP: 0033:0x7f55e93089fd
[   51.148250] Code: 5b 41 5c c3 66 0f 1f 84 00 00 00 00 00 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d eb 23 0f 00 f7 d8 64 89 01 48
[   51.149005] RSP: 002b:00007ffcb754a8f8 EFLAGS: 00000246 ORIG_RAX: 0000000000000139
[   51.149313] RAX: ffffffffffffffda RBX: 00005611a26ec7d0 RCX: 00007f55e93089fd
[   51.149608] RDX: 0000000000000000 RSI: 000056117e373962 RDI: 0000000000000003
[   51.149912] RBP: 0000000000000000 R08: 0000000000000000 R09: 0000000000000000
[   51.150202] R10: 0000000000000003 R11: 0000000000000246 R12: 000056117e373962
[   51.150493] R13: 00005611a26ec770 R14: 000056117e372550 R15: 00005611a26ec8f0
[   51.150801]  </TASK>
[   51.150900] ---[ end trace 0000000000000000 ]---
[   51.151101] page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x15a1bfb
[   51.151107] flags: 0x17ffffc0000000(node=0|zone=2|lastcpupid=0x1fffff)
[   51.151111] raw: 0017ffffc0000000 ffa280331686fec8 ffa280331686fec8 0000000000000000
[   51.151113] raw: 0000000000000000 0000000000000000 00000000ffffffff 0000000000000000
[   51.151113] page dumped because: Not a kmalloc allocation

A simple reproducer:

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

  static void dummy_new_func(void)
  {
  }

  static struct klp_func funcs[] = {
          {
                  .old_name = "this_symbol_does_not_exist",
                  .new_func = dummy_new_func,
          },
          { }
  };

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

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

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

  static void test_klp_obj_dynamic_exit(void)
  {
  }

  module_init(test_klp_obj_dynamic_init);
  module_exit(test_klp_obj_dynamic_exit);

  MODULE_LICENSE("GPL");
  MODULE_DESCRIPTION("livepatch reproducer: object dynamic flag");
  MODULE_INFO(livepatch, "Y");

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

After this fix, the result is:

[   60.029559] test_klp_obj_dynamic: loading out-of-tree module taints kernel.
[   60.029565] test_klp_obj_dynamic: tainting kernel with TAINT_LIVEPATCH
[   60.034073] livepatch: test_klp_obj_dynamic: object vmlinux must not set the internal dynamic flag

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

diff --git a/include/linux/livepatch.h b/include/linux/livepatch.h
index 76ba613830a6..859466f08d95 100644
--- a/include/linux/livepatch.h
+++ b/include/linux/livepatch.h
@@ -93,6 +93,7 @@ struct klp_func {
  * @mod:	kernel module associated with the patched object
  *		(NULL for vmlinux)
  * @dynamic:    temporary object for nop functions; dynamically allocated
+ *              internal only, must not be set by a patch
  * @patched:	the object's funcs have been added to the klp_ops list
  */
 struct klp_object {
diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c
index ba09894eec2c..162f4cc6e2c0 100644
--- a/kernel/livepatch/core.c
+++ b/kernel/livepatch/core.c
@@ -1110,6 +1110,13 @@ static int klp_check_patch(struct klp_patch *patch)
 	klp_for_each_object_static(patch, obj) {
 		if (!obj->funcs)
 			return -EINVAL;
+		if (obj->dynamic) {
+			pr_err("%s: object %s must not set the internal dynamic flag\n",
+				patch->mod->name,
+				klp_is_module(obj) ? obj->name : "vmlinux");
+			return -EINVAL;
+		}
+
 		klp_for_each_func_static(obj, func) {
 			if (!func->old_name)
 				return -EINVAL;
-- 
2.52.0


  parent 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 ` [PATCH 1/2] livepatch: reject nop flag " Yafang Shao
2026-11-16 13:13   ` sashiko-bot
2026-11-16 13:04 ` Yafang Shao [this message]
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-3-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