Solution Discussion: kbleds.c exists Kernel Panic caused by NULL Pointer Dereference

Open
#391 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
1-2 days
Newbie friendliness
52/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
c, linux

Research direction

Start with kbleds.c, especially the dereference at line 64 in kbleds_init() and my_timer_func(). Reproduce the reported failures in the QEMU guest using the issue’s insmod steps, then inspect how the module accesses the TTY and invokes ioctl() from its timer callback. Done means the module handles unavailable TTYs safely and avoids the reported kernel panics.

Written by the indexing model from the issue text.

Description

I have found that kbleds.c exists NULL Pointer Dereference exception in current implementation on QEMU shell environment.
Although the test_modules.sh skipped kbleds.ko testing, I think that implementing additional check on tty pointer is mandatory.
Because it may take place on another platform which has no available tty.
The enhancement can also make this kernel module as a better design pattern.

Since there are multiple modification on kbleds.c, I would like to collect different opinion of solutions from this issue.
Once the proper solution is concluded, I will submit the pull request for fixing this bug.
Please feel free to leave your opinion or request for more information regarding the solution or material.

Proposed solution for kbleds.c
  1. Implement NULL pointer check directly, on init_module(), exit_module(), and timer_func()

Another solution is to leverage Linux kernel APIs, such as tty_port_tty_get().
Or, using Keyboard / Input / LED Subsystem to control keyboard LEDs in safer way.

  1. Current timer_func() is triggered by timer interrupt (SoftIRQ), which belongs to Interrupt Context.
    However, ioctl() may have long delay behavior such as blocking-wait for lock or I/O completion.
    Executing ioctl() in the Interrupt Context is not safe.

Since current ticking task for LED flashing does not require precise ticking, we can use queue_work()
for changing LED status in the Process Context.
Another possible solution is to use queue_delayed_work() with proper rescheduling to change LED status.
This solution is guaranteed to run on Process Context.

Tracing the place where the program dereferenced a NULL Pointer

RIP: 0010:kbleds_init+0x92/0xff0

steven@MSI:~/linux_2026/lkmpg/examples$ objdump -t kbleds.ko | grep 'kbleds_init'
0000000000000010 l     F .init.text     00000000000000e9 kbleds_init
0000000000000000 l     F .init.text     0000000000000010 __pfx_kbleds_init
steven@MSI:~/linux_2026/lkmpg/examples$ addr2line -e kbleds.ko -f -i 0xA2    # 0x10 + 0x92
kbleds_init
/home/steven/linux_2026/lkmpg/examples/kbleds.c:64
static int __init kbleds_init(void)
{
    int i;

    pr_info("kbleds: loading\n");
    pr_info("kbleds: fgconsole is %x\n", fg_console);
    for (i = 0; i < MAX_NR_CONSOLES; i++) {
        if (!vc_cons[i].d)
            break;
        pr_info("poet_atkm: console[%i/%i] #%i, tty %p\n", i, MAX_NR_CONSOLES,
                vc_cons[i].d->vc_num, (void *)vc_cons[i].d->port.tty);
    }
    pr_info("kbleds: finished scanning consoles\n");

    my_driver = vc_cons[fg_console].d->port.tty->driver;     // kbleds.c:64 is here.
    pr_info("kbleds: tty driver name %s\n", my_driver->driver_name);

    /* Set up the LED blink timer the first time. */
    timer_setup(&my_timer, my_timer_func, 0);
    my_timer.expires = jiffies + BLINK_DELAY;
Steps to reproduce the kernel panic on QEMU shell
  1. For init_module() issue of kbleds.c, execute insmod /mnt/lkmpg/examples/kbleds.c directly.
lkmpg:~# insmod /mnt/lkmpg/examples/kbleds.ko
[   21.013868] kbleds: loading out-of-tree module taints kernel.
[   21.019562] kbleds: loading
[   21.019755] kbleds: fgconsole is 0
[   21.020000] poet_atkm: console[0/63] #0, tty 0000000000000000
[   21.021153] kbleds: finished scanning consoles
[   21.022139] BUG: kernel NULL pointer dereference, address: 0000000000000010
[   21.022792] #PF: supervisor read access in kernel mode
[   21.023146] #PF: error_code(0x0000) - not-present page
[   21.023510] PGD 3d37067 P4D 3d37067 PUD 3d3f067 PMD 0 
[   21.024082] Oops: Oops: 0000 [#1] PREEMPT SMP NOPTI
[   21.028838] CPU: 0 UID: 0 PID: 65 Comm: insmod Tainted: G           O       6.12.6 #4
[   21.029763] Tainted: [O]=OOT_MODULE
[   21.030031] Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.4
[   21.030953] RIP: 0010:kbleds_init+0x92/0xff0 [kbleds]
[   21.031857] Code: 98 40 00 a0 e8 2f cd 0f e1 48 63 05 ec e3 fc e2 48 c7 c7 2e 40 00 a0 48 8d 04 80 1
[   21.032419] RSP: 0018:ffffc900001dfdd0 EFLAGS: 00000246
[   21.032562] RAX: 0000000000000000 RBX: 0000000000000001 RCX: 0000000000000000
[   21.032702] RDX: 0000000000000000 RSI: ffffc900001dfc88 RDI: ffffffffa000402e
[   21.032879] RBP: ffffffff82fd44a8 R08: 00000000ffffdfff R09: 0000000000000001
[   21.033217] R10: 00000000ffffdfff R11: ffffffff82658920 R12: 00007f3572a9da68
[   21.033404] R13: ffffc900001dfde8 R14: 00000000006281cf R15: ffffc900001dfe80
[   21.033597] FS:  00000000252fe3c0(0000) GS:ffff88801f400000(0000) knlGS:0000000000000000
[   21.033946] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   21.034260] CR2: 0000000000000010 CR3: 0000000004872000 CR4: 00000000000006f0
[   21.035050] Call Trace:
[   21.035883]  <TASK>
[   21.036131]  ? __die+0x1e/0x60
[   21.036476]  ? page_fault_oops+0x17b/0x4a0
[   21.036801]  ? __module_address+0x2e/0xb0
[   21.037249]  ? kbleds_init+0x92/0xff0 [kbleds]
[   21.037580]  ? search_module_extables+0x14/0x50
[   21.038046]  ? exc_page_fault+0x6b/0x150
[   21.038466]  ? asm_exc_page_fault+0x26/0x30
[   21.038776]  ? kbleds_init+0x92/0xff0 [kbleds]
[   21.039241]  ? __pfx_kbleds_init+0x10/0x10 [kbleds]
[   21.039644]  do_one_initcall+0x56/0x220
[   21.040079]  do_init_module+0x5b/0x200
[   21.040371]  __do_sys_init_module+0x152/0x180
[   21.040712]  ? __rseq_handle_notify_resume+0x297/0x4d0
[   21.041093]  do_syscall_64+0x9e/0x1a0
[   21.041518]  entry_SYSCALL_64_after_hwframe+0x77/0x7f
[   21.042094] RIP: 0033:0x48c51d
[   21.042501] Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 90 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 8
[   21.044232] RSP: 002b:00007ffdf631f728 EFLAGS: 00000246 ORIG_RAX: 00000000000000af
[   21.045297] RAX: ffffffffffffffda RBX: 00000000006281cf RCX: 000000000048c51d
[   21.045685] RDX: 00000000006281cf RSI: 000000000004fa58 RDI: 00007f3572a4e010
[   21.046407] RBP: 00007ffdf631fac0 R08: 0000000000000007 R09: 0000000000000000
[   21.046886] R10: 0000000000000000 R11: 0000000000000246 R12: 00007ffdf631fac8
[   21.047089] R13: 00007ffdf631fad0 R14: 00000000006281cf R15: 0000000000000001
[   21.047565]  </TASK>
[   21.047673] Modules linked in: kbleds(O+)
[   21.048276] CR2: 0000000000000010
[   21.048962] ---[ end trace 0000000000000000 ]---
[   21.050000] RIP: 0010:kbleds_init+0x92/0xff0 [kbleds]
[   21.050501] Code: 98 40 00 a0 e8 2f cd 0f e1 48 63 05 ec e3 fc e2 48 c7 c7 2e 40 00 a0 48 8d 04 80 1
[   21.053834] RSP: 0018:ffffc900001dfdd0 EFLAGS: 00000246
[   21.055108] RAX: 0000000000000000 RBX: 0000000000000001 RCX: 0000000000000000
[   21.057035] RDX: 0000000000000000 RSI: ffffc900001dfc88 RDI: ffffffffa000402e
[   21.058738] RBP: ffffffff82fd44a8 R08: 00000000ffffdfff R09: 0000000000000001
[   21.060728] R10: 00000000ffffdfff R11: ffffffff82658920 R12: 00007f3572a9da68
[   21.061162] R13: ffffc900001dfde8 R14: 00000000006281cf R15: ffffc900001dfe80
[   21.062177] FS:  00000000252fe3c0(0000) GS:ffff88801f400000(0000) knlGS:0000000000000000
[   21.064785] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   21.065170] CR2: 0000000000000010 CR3: 0000000004872000 CR4: 00000000000006f0
[   21.066881] insmod (65) used greatest stack depth: 13456 bytes left
Killed
lkmpg:~# 
  1. For my_timer_func() issue of kbleds.c, create a temporarily TTY device via QEMU guest shell and release it after insmod.

This operation may require branch qemu_support_jobctrl on #383 to correctly execute .

lkmpg:~# sh -c 'exec 3<> /dev/tty1; sleep 10' & insmod /mnt/lkmpg/examples/kbleds.ko 
[   45.501627] kbleds: loading out-of-tree module taints kernel.
[   45.509873] kbleds: loading
[   45.510202] kbleds: fgconsole is 0
[   45.511294] poet_atkm: console[0/63] #0, tty (____ptrval____)
[   45.512910] kbleds: finished scanning consoles
[   45.513212] kbleds: tty driver name (null)
lkmpg:~# [   55.493342] BUG: kernel NULL pointer dereference, address: 0000000000000248
[   55.493833] #PF: supervisor read access in kernel mode
[   55.494074] #PF: error_code(0x0000) - not-present page
[   55.494285] PGD 0 P4D 0 
[   55.494542] Oops: Oops: 0000 [#1] PREEMPT SMP NOPTI
[   55.495093] CPU: 1 UID: 0 PID: 0 Comm: swapper/1 Tainted: G           O       6.12.6 #4
[   55.495510] Tainted: [O]=OOT_MODULE
[   55.495647] Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.4
[   55.496242] RIP: 0010:vt_ioctl+0x21/0x1410
[   55.496785] Code: 90 90 90 90 90 90 90 90 90 f3 0f 1e fa 55 48 89 e5 41 57 49 89 ff 41 56 41 55 41 1
[   55.497608] RSP: 0018:ffffc900000d8e00 EFLAGS: 00000286
[   55.497746] RAX: ffffffff815f1da0 RBX: 0000000000004b32 RCX: ffff88801f51e140
[   55.498065] RDX: 00000000000000ff RSI: 0000000000004b32 RDI: 0000000000000000
[   55.498212] RBP: ffffc900000d8e98 R08: ffff88801f51e168 R09: ffffc900000d8ef8
[   55.498553] R10: 0000000000000001 R11: 0000000000048e43 R12: 00000000000000ff
[   55.498981] R13: ffffffffa0000010 R14: dead000000000122 R15: 0000000000000000
[   55.499512] FS:  0000000000000000(0000) GS:ffff88801f500000(0000) knlGS:0000000000000000
[   55.500180] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   55.500866] CR2: 0000000000000248 CR3: 000000000262e000 CR4: 00000000000006f0
[   55.501787] Call Trace:
[   55.502508]  <IRQ>
[   55.502790]  ? __die+0x1e/0x60
[   55.502996]  ? page_fault_oops+0x17b/0x4a0
[   55.503346]  ? search_extable+0x26/0x30
[   55.503747]  ? vt_ioctl+0x21/0x1410
[   55.503973]  ? search_module_extables+0x14/0x50
[   55.504363]  ? exc_page_fault+0x6b/0x150
[   55.504697]  ? asm_exc_page_fault+0x26/0x30
[   55.505124]  ? __pfx_my_timer_func+0x10/0x10 [kbleds]
[   55.505549]  ? __pfx_vt_ioctl+0x10/0x10
[   55.505803]  ? vt_ioctl+0x21/0x1410
[   55.505992]  ? rcu_sched_clock_irq+0x28d/0x1060
[   55.506341]  ? __pfx_my_timer_func+0x10/0x10 [kbleds]
[   55.506727]  ? __pfx_my_timer_func+0x10/0x10 [kbleds]
[   55.507051]  my_timer_func+0x57/0x80 [kbleds]
[   55.507398]  call_timer_fn+0x25/0x120
[   55.507659]  __run_timer_base.part.0+0x207/0x290
[   55.507942]  run_timer_softirq+0x6e/0xb0
[   55.508273]  handle_softirqs+0xc5/0x2b0
[   55.508533]  irq_exit_rcu+0x62/0x80
[   55.508798]  sysvec_apic_timer_interrupt+0x6b/0x80
[   55.509212]  </IRQ>
[   55.509500]  <TASK>
[   55.509747]  asm_sysvec_apic_timer_interrupt+0x1a/0x20
[   55.510377] RIP: 0010:default_idle+0xf/0x20
[   55.510599] Code: 4c 01 c7 4c 29 c2 e9 72 ff ff ff 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 0
[   55.511760] RSP: 0018:ffffc900000a3ee8 EFLAGS: 00000206
[   55.512123] RAX: ffff88801f500000 RBX: ffff8880035c9d00 RCX: 0000000000000000
[   55.512639] RDX: 0000000000000001 RSI: ffffffff8246657f RDI: 00000000000116c4
[   55.513321] RBP: 0000000000000001 R08: 00000000000116c4 R09: 0000000000100000
[   55.513899] R10: 00000000354c9d27 R11: 0000000000000001 R12: 0000000000000000
[   55.514427] R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000
[   55.514981]  default_idle_call+0x32/0xf0
[   55.515241]  do_idle+0x1b5/0x200
[   55.515438]  cpu_startup_entry+0x24/0x30
[   55.515753]  start_secondary+0xf7/0x100
[   55.515971]  common_startup_64+0x12c/0x138
[   55.516544]  </TASK>
[   55.516782] Modules linked in: kbleds(O)
[   55.517528] CR2: 0000000000000248
[   55.518036] ---[ end trace 0000000000000000 ]---
[   55.518594] RIP: 0010:vt_ioctl+0x21/0x1410
[   55.518993] Code: 90 90 90 90 90 90 90 90 90 f3 0f 1e fa 55 48 89 e5 41 57 49 89 ff 41 56 41 55 41 1
[   55.520336] RSP: 0018:ffffc900000d8e00 EFLAGS: 00000286
[   55.520746] RAX: ffffffff815f1da0 RBX: 0000000000004b32 RCX: ffff88801f51e140
[   55.521549] RDX: 00000000000000ff RSI: 0000000000004b32 RDI: 0000000000000000
[   55.522120] RBP: ffffc900000d8e98 R08: ffff88801f51e168 R09: ffffc900000d8ef8
[   55.522760] R10: 0000000000000001 R11: 0000000000048e43 R12: 00000000000000ff
[   55.523080] R13: ffffffffa0000010 R14: dead000000000122 R15: 0000000000000000
[   55.523540] FS:  0000000000000000(0000) GS:ffff88801f500000(0000) knlGS:0000000000000000
[   55.523931] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   55.524233] CR2: 0000000000000248 CR3: 000000000262e000 CR4: 00000000000006f0
[   55.524671] Kernel panic - not syncing: Fatal exception in interrupt
[   55.525763] Kernel Offset: disabled
[   55.526135] ---[ end Kernel panic - not syncing: Fatal exception in interrupt ]---
Dominant language
TeX
Stars
8.7k
Forks
625
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sysprog21/lkmpg

All issues in sysprog21/lkmpg

Similar issues

More Operating Systems issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.