Skip to content

lua-lsm: fix AF_UNIX sockaddr marshalling lengths - #12

Open
chenzongyao200127 wants to merge 110 commits into
openanolis:lua-lsmfrom
chenzongyao200127:lua-lsm-b13d9e8-af-unix-sockaddr-len
Open

lua-lsm: fix AF_UNIX sockaddr marshalling lengths#12
chenzongyao200127 wants to merge 110 commits into
openanolis:lua-lsmfrom
chenzongyao200127:lua-lsm-b13d9e8-af-unix-sockaddr-len

Conversation

@chenzongyao200127

Copy link
Copy Markdown
Collaborator

AF_UNIX socket addresses are not C strings in all cases. Abstract socket names may contain embedded NUL bytes and their usable length comes from the sockaddr length passed by the hook.

Carry the sockaddr length together with the pointer exposed to Lua and use that length when formatting AF_UNIX paths. This preserves abstract socket names and avoids reading past the supplied sockaddr.

Validation:

  • ./scripts/checkpatch.pl --git origin/lua-lsm..lua-lsm-b13d9e8-af-unix-sockaddr-len
  • git diff --check origin/lua-lsm..lua-lsm-b13d9e8-af-unix-sockaddr-len

Signed-off-by: Zongyao Chen ZongYao.Chen@linux.alibaba.com

uudiin added 30 commits March 24, 2026 14:12
The folling is main instructions:

  export PATH="$(brew --prefix make)/libexec/gnubin:$PATH"
  export PATH="$(brew --prefix llvm)/bin:$PATH"
  export PATH="$(brew --prefix lld)/bin:$PATH"

  # Eliminate compile errors of scripts/mod/file2alias.c
  export HOSTCFLAGS="-D_UUID_T -D__GETHOSTUUID_H"

  # Maybe the symlink is missed on macOS
  #ln -s ../../../scripts/syscall.tbl arch/arm64/tools/syscall_64.tbl
  #ln -s qcom,sm8550-dispcc.h include/dt-bindings/clock/qcom,sm8650-dispcc.h

  #make ARCH=arm64 LLVM=1

  #make LLVM=1 menuconfig
  #make LLVM=1 -j8

Signed-off-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
When developing a dedicated LSM module, we need to operate on the
file object within the LSM function, such as retrieving the path.
However, in `security_file_alloc()`, the passed-in `filp` is
only a valid pointer; the content of `filp` is completely
uninitialized and entirely random, which confuses the LSM function.

Therefore, it is necessary to call `security_file_alloc()` only
after the main fields of the `filp` object have been initialized.
This patch only moves the call to `security_file_alloc()` to the
end of the `init_file()` function.

Signed-off-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
Standard C provides basic library functions for jumping between
functions. They were introduced to support the upcoming Lua
language interpreter, which uses the setjmp/longjmp functions
to implement exception handling.

Signed-off-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
Standard C provides basic library functions for jumping between
functions. They were introduced to support the upcoming Lua
language interpreter, which uses the setjmp/longjmp functions
to implement exception handling.

Signed-off-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
Lua is an extension programming language designed to support general
procedural programming with data description facilities. It also offers
good support for object-oriented programming, functional programming,
and data-driven programming. Lua is intended to be used as a powerful,
light-weight scripting language for any program that needs one. Lua is
implemented as a library, written in clean C (that is, in the common
subset of ANSI C and C++).

Lua is embedded in the kernel. Leveraging Lua's simplicity and powerful
metaprogramming capabilities, it allows the use of scripting languages
to develop complex security access control policies.

The reason for choosing version 5.1 is that Lua has been mature enough
since version 5.1, and secondly, LuaJIT may be introduced in the future
for JIT acceleration. Currently, LuaJIT is compatible with version 5.1.

Signed-off-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
queue.h and tree.h are borrowed from freebsd
d342ae67192f ("uath: add support for GCMP-128 encryption")

bitmap.h is borrowed from bitops of latest netbsd.

Signed-off-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
Signed-off-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
Signed-off-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
Co-developed-by: Zongyao Chen <ZongYao.Chen@linux.alibaba.com>
Signed-off-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
Signed-off-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
Signed-off-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
Signed-off-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
Signed-off-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
This module leverages Lua's metaprogramming capabilities to develop
complex access control logic and policies in Lua. Let us using a
programmatic approach close to natural language, significantly lowers
the barrier to entry for developing LSM access control.

This module has the following notable features:
* Supports developing LSM policies as mini-programs or LSM plugins.
  Multiple mini-programs or LSM plugins can be loaded simultaneously,
  and dynamic, on-demand uninstallation is supported.
* Each mini-program runs in a secure sandbox environment. Script errors
  will not trigger kernel panics, and mini-programs are isolated from
  each other, ensuring no impact.
* Mini-programs interact with the kernel through a specific, limited
  API, ensuring kernel security.
* The LSM module features deep integration with the Lua language,
  enabling the most natural way to share data and set kernel object
  attributes.

Signed-off-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
Co-developed-by: Zongyao Chen <ZongYao.Chen@linux.alibaba.com>
The reason is that the modules_lock is held for a period of time during
module registration. A softirq occurs within this critical section, and
the softirq handler reenters the LSM function, causing a deadlock.

This issue can be resolved by disabling softirq while holding the
modules_lock lock during module registration.

Some logs are as follows:

[28031.294632] rcu: INFO: rcu_preempt self-detected stall on CPU
[28031.294801] rcu: 	2-...!: (886811 ticks this GP) idle=ea9c/1/0x4000000000000000 softirq=243287/243287 fqs=0
[28031.295043] rcu: 	(t=887418 jiffies g=263373 q=454 ncpus=4)
[28031.295183] rcu: rcu_preempt kthread starved for 887418 jiffies! g263373 f0x0 RCU_GP_WAIT_FQS(5) ->state=0x0 ->cpu=2
[28031.295524] rcu: 	Unless rcu_preempt kthread gets sufficient CPU time, OOM is now expected behavior.
[28031.295872] rcu: RCU grace-period kthread stack dump:
[28031.296001] task:rcu_preempt     state:R  running task     stack:0     pid:15    tgid:15    ppid:2      task_flags:0x208040 flags:0x00000010
[28031.296319] Call trace:
[28031.296381]  __switch_to+0x194/0x2b4 (T)
[28031.296484]  __schedule+0x5b4/0x980
[28031.296572]  schedule+0x54/0xf8
[28031.296652]  schedule_timeout+0x88/0xf8
[28031.296750]  rcu_gp_fqs_loop+0x1c4/0x6b0
[28031.296851]  rcu_gp_kthread+0x60/0x134
[28031.296946]  kthread+0x140/0x254
[28031.297030]  ret_from_fork+0x10/0x20
[28031.297123] Sending NMI from CPU 2 to CPUs 1:
[28031.297242] NMI backtrace for cpu 1
[28031.297247] CPU: 1 UID: 0 PID: 81146 Comm: sh Not tainted 6.17.0-rc3+ #511 PREEMPT
[28031.297249] Hardware name: QEMU QEMU Virtual Machine, BIOS edk2-stable202408-prebuilt.qemu.org 08/13/2024
[28031.297250] pstate: 01400005 (nzcv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
[28031.297250] pc : queued_read_lock_slowpath+0x80/0x14c
[28031.297253] lr : _raw_read_lock_bh+0x64/0x68
[28031.297254] sp : ffff800088ee39e0
[28031.297254] x29: ffff800088ee39e0 x28: 0000000000002e2e x27: 0000000000000501
[28031.297256] x26: 0000000000000030 x25: 2f2f2f2f2f2f2f2f x24: 61c8864680b583eb
[28031.297257] x23: fefefefefefefeff x22: 0000000000000000 x21: ffff0000c2671138
[28031.297257] x20: 0000000000000081 x19: 00001643fef959e4 x18: 0000000000000000
[28031.297258] x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
[28031.297259] x14: 0000000000000000 x13: 0000000000000000 x12: 8080808080808000
[28031.297259] x11: 000000000000002e x10: 0000000000000018 x9 : 0000000000000000
[28031.297260] x8 : 00000000000008ff x7 : 0000000000000000 x6 : 0000000000000000
[28031.297261] x5 : 0000000000000000 x4 : 0000000000000000 x3 : ffff800088ee3c98
[28031.297261] x2 : 0000000000000000 x1 : 0000000000013cee x0 : ffff8000818ae3c8
[28031.297262] Call trace:
[28031.297262]  queued_read_lock_slowpath+0x80/0x14c (P)
[28031.297264]  _raw_read_lock_bh+0x64/0x68
[28031.297265]  lua_lsm_inode_permission+0x54/0x35c
[28031.297268]  security_inode_permission+0x54/0xb0
[28031.297269]  inode_permission+0x64/0x170
[28031.297270]  link_path_walk+0xb4/0x388
[28031.297272]  path_lookupat+0x68/0x120
[28031.297272]  filename_lookup+0xe0/0x1e0
[28031.297273]  vfs_statx+0x7c/0x1a0
[28031.297276]  vfs_fstatat+0xb4/0xe0
[28031.297277]  __arm64_sys_newfstatat+0x68/0xa8
[28031.297278]  invoke_syscall+0x40/0xf8
[28031.297280]  el0_svc_common+0xa8/0xd8
[28031.297281]  do_el0_svc+0x1c/0x28
[28031.297282]  el0_svc+0x38/0x8c
[28031.297283]  el0t_64_sync_handler+0x84/0x12c
[28031.297284]  el0t_64_sync+0x198/0x19c
[28031.298239] CPU: 2 UID: 0 PID: 81145 Comm: cat Not tainted 6.17.0-rc3+ #511 PREEMPT
[28031.303082] Hardware name: QEMU QEMU Virtual Machine, BIOS edk2-stable202408-prebuilt.qemu.org 08/13/2024
[28031.303326] pstate: 01400005 (nzcv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--)
[28031.303504] pc : queued_read_lock_slowpath+0x80/0x14c
[28031.303634] lr : _raw_read_lock_bh+0x64/0x68
[28031.303743] sp : ffff8000818ebe00
[28031.303828] x29: ffff8000818ebe00 x28: 0000000000000200 x27: 000000000000000a
[28031.304011] x26: ffff800081788000 x25: ffff8000800fa104 x24: 0000000000000001
[28031.304192] x23: 0000000000000000 x22: 000000000000000a x21: 0000000000000000
[28031.304374] x20: ffff000008774600 x19: 00001643fef8ae04 x18: 0000000000000000
[28031.304556] x17: ffff80007e099000 x16: ffff8000818e8000 x15: 0000000000000000
[28031.304739] x14: 0000000000000004 x13: ffff0000ff6187a8 x12: 0000000000000002
[28031.304922] x11: ffff0000c2288a18 x10: 0000000000000018 x9 : 0000000000000000
[28031.305104] x8 : 00000000000008ff x7 : 7fffffffffffffff x6 : 00003d0900007d00
[28031.305285] x5 : ffff800081874120 x4 : 0000000000000008 x3 : ffff80008a153710
[28031.305470] x2 : 00000000000404d4 x1 : 00000000000138ce x0 : ffff8000818ae3c8
[28031.305651] Call trace:
[28031.305714]  queued_read_lock_slowpath+0x80/0x14c (P)
[28031.305844]  _raw_read_lock_bh+0x64/0x68
[28031.305945]  lua_lsm_cred_free+0x54/0x2bc
[28031.306048]  security_cred_free+0x5c/0x90
[28031.306151]  put_cred_rcu+0x28/0x15c
[28031.306244]  rcu_core+0x2c0/0x5e0
[28031.306330]  rcu_core_si+0x10/0x1c
[28031.306418]  handle_softirqs+0xdc/0x200
[28031.306517]  __do_softirq+0x14/0x20
[28031.306686]  ____do_softirq+0x10/0x1c
[28031.306782]  call_on_irq_stack+0x30/0x48
[28031.306884]  do_softirq_own_stack+0x1c/0x28
[28031.306993]  __irq_exit_rcu+0x54/0xf8
[28031.307087]  irq_exit_rcu+0x10/0x1c
[28031.307176]  el1_interrupt+0x38/0x54
[28031.307269]  el1h_64_irq_handler+0x18/0x24
[28031.307375]  el1h_64_irq+0x6c/0x70
[28031.307463]  lua_module_register+0x550/0x588 (P)
[28031.307582]  register_write+0x78/0xac
[28031.307676]  vfs_write+0x160/0x3bc
[28031.307764]  ksys_write+0x70/0xe4
[28031.307849]  __arm64_sys_write+0x1c/0x28
[28031.307950]  invoke_syscall+0x40/0xf8
[28031.308044]  el0_svc_common+0xa8/0xd8
[28031.308138]  do_el0_svc+0x1c/0x28
[28031.308225]  el0_svc+0x38/0x8c
[28031.308311]  el0t_64_sync_handler+0x84/0x12c
[28031.308422]  el0t_64_sync+0x198/0x19c
When interrupts are disabled, file system operations triggered by memory
allocation may cause a potential deadlock.

The log is as follows:

[   37.041540] =====================================================
[   37.041790] WARNING: SOFTIRQ-safe -> SOFTIRQ-unsafe lock order detected
[   37.042170] 6.17.0-rc3+ #515 Tainted: G        W
[   37.042492] -----------------------------------------------------
[   37.042953] cat/1610 [HC0[0]:SC0[2]:HE1:SE0] is trying to acquire:
[   37.043316] ffff800081f2b9f8 (fs_reclaim){+.+.}-{0:0}, at: __kmalloc_noprof+0xb0/0x4d4
[   37.043783]
[   37.043783] and this task is already holding:
[   37.044121] ffff0000c177e138 (&dict->lock){+.-.}-{3:3}, at: kvcache_incr+0x68/0x21c
[   37.044569] which would create a new lock dependency:
[   37.044861]  (&dict->lock){+.-.}-{3:3} -> (fs_reclaim){+.+.}-{0:0}
[   37.045222]
[   37.045222] but this new dependency connects a SOFTIRQ-irq-safe lock:
[   37.045685]  (&dict->lock){+.-.}-{3:3}
[   37.045687]
[   37.045687] ... which became SOFTIRQ-irq-safe at:
[   37.046273]   lock_acquire+0x118/0x26c
[   37.046479]   _raw_write_lock+0x4c/0x88
[   37.046736]   kvcache_dict_free+0x24/0x104
[   37.046950]   lua_lsm_inode_free_security_rcu+0x1d4/0x274
[   37.047231]   inode_free_by_rcu+0x54/0x9c
[   37.047442]   rcu_core+0x474/0xa50
[   37.047623]   rcu_core_si+0x10/0x1c
[   37.047805]   handle_softirqs+0x178/0x42c
[   37.048018]   __do_softirq+0x14/0x20
[   37.048205]   ____do_softirq+0x10/0x1c
[   37.048404]   call_on_irq_stack+0x30/0x48
[   37.048614]   do_softirq_own_stack+0x1c/0x28
[   37.048838]   __irq_exit_rcu+0xd4/0x194
[   37.049039]   irq_exit_rcu+0x10/0x34
[   37.049226]   el1_interrupt+0x38/0x54
[   37.049418]   el1h_64_irq_handler+0x18/0x24
[   37.049636]   el1h_64_irq+0x6c/0x70
[   37.049818]   do_idle+0xe8/0x26c
[   37.049994]   cpu_startup_entry+0x34/0x38
[   37.050206]   kernel_init+0x0/0x128
[   37.050389]   start_kernel+0x304/0x3c0
[   37.050659]   __primary_switched+0x88/0x90
[   37.050881]
[   37.050881] to a SOFTIRQ-irq-unsafe lock:
[   37.051169]  (fs_reclaim){+.+.}-{0:0}
[   37.051171]
[   37.051171] ... which became SOFTIRQ-irq-unsafe at:
[   37.051699] ...
[   37.051704]   lock_acquire+0x118/0x26c
[   37.051997]   fs_reclaim_acquire+0x64/0xd0
[   37.052215]   mem_cgroup_css_alloc+0xdc/0x6e4
[   37.052436]   cgroup_init_subsys+0x7c/0x1d0
[   37.052642]   cgroup_init+0x2d8/0x490
[   37.052822]   start_kernel+0x2f4/0x3c0
[   37.053006]   __primary_switched+0x88/0x90
[   37.053210]
[   37.053210] other info that might help us debug this:
[   37.053210]
[   37.053598]  Possible interrupt unsafe locking scenario:
[   37.053598]
[   37.053929]        CPU0                    CPU1
[   37.054152]        ----                    ----
[   37.054372]   lock(fs_reclaim);
[   37.054649]                                local_irq_disable();
[   37.055469]                                lock(&dict->lock);
[   37.055751]                                lock(fs_reclaim);
[   37.056024]   <Interrupt>
[   37.056151]     lock(&dict->lock);
[   37.056316]
[   37.056316]  *** DEADLOCK ***
[   37.056316]
[   37.056602] 2 locks held by cat/1610:
[   37.056779]  #0: ffff800081fdaf18 (modules_lock){++.-}-{3:3}, at: lua_lsm_file_permission+0x58/0x36c
[   37.057221]  openanolis#1: ffff0000c177e138 (&dict->lock){+.-.}-{3:3}, at: kvcache_incr+0x68/0x21c
[   37.057611]
[   37.057611] the dependencies between SOFTIRQ-irq-safe lock and the holding lock:
[   37.058091] -> (&dict->lock){+.-.}-{3:3} {
[   37.058303]    HARDIRQ-ON-W at:
[   37.058495]                     lock_acquire+0x118/0x26c
[   37.058777]                     _raw_write_lock+0x4c/0x88
[   37.059053]                     kvcache_dict_free+0x24/0x104
[   37.059353]                     lua_lsm_file_free_security+0x210/0x2bc
[   37.059691]                     security_file_free+0x5c/0xa4
[   37.059991]                     __fput+0x1a0/0x2f0
[   37.060242]                     delayed_fput+0x44/0x58
[   37.060514]                     process_one_work+0x210/0x548
[   37.060815]                     worker_thread+0x244/0x380
[   37.061096]                     kthread+0x138/0x260
[   37.061348]                     ret_from_fork+0x10/0x20
[   37.061622]    IN-SOFTIRQ-W at:
[   37.061790]                     lock_acquire+0x118/0x26c
[   37.062117]                     _raw_write_lock+0x4c/0x88
[   37.062405]                     kvcache_dict_free+0x24/0x104
[   37.062730]                     lua_lsm_inode_free_security_rcu+0x1d4/0x274
[   37.063097]                     inode_free_by_rcu+0x54/0x9c
[   37.063390]                     rcu_core+0x474/0xa50
[   37.063652]                     rcu_core_si+0x10/0x1c
[   37.063917]                     handle_softirqs+0x178/0x42c
[   37.064213]                     __do_softirq+0x14/0x20
[   37.064475]                     ____do_softirq+0x10/0x1c
[   37.064739]                     call_on_irq_stack+0x30/0x48
[   37.065014]                     do_softirq_own_stack+0x1c/0x28
[   37.065307]                     __irq_exit_rcu+0xd4/0x194
[   37.065583]                     irq_exit_rcu+0x10/0x34
[   37.065901]                     el1_interrupt+0x38/0x54
[   37.066155]                     el1h_64_irq_handler+0x18/0x24
[   37.066399]                     el1h_64_irq+0x6c/0x70
[   37.066660]                     do_idle+0xe8/0x26c
[   37.066891]                     cpu_startup_entry+0x34/0x38
[   37.067168]                     kernel_init+0x0/0x128
[   37.067418]                     start_kernel+0x304/0x3c0
[   37.067679]                     __primary_switched+0x88/0x90
[   37.067959]    INITIAL USE at:
[   37.068115]                    lock_acquire+0x118/0x26c
[   37.068375]                    _raw_write_lock+0x4c/0x88
[   37.068637]                    kvcache_dict_free+0x24/0x104
[   37.068917]                    lua_lsm_file_free_security+0x210/0x2bc
[   37.069222]                    security_file_free+0x5c/0xa4
[   37.069499]                    __fput+0x1a0/0x2f0
[   37.069726]                    delayed_fput+0x44/0x58
[   37.070043]                    process_one_work+0x210/0x548
[   37.070282]                    worker_thread+0x244/0x380
[   37.070499]                    kthread+0x138/0x260
[   37.070713]                    ret_from_fork+0x10/0x20
[   37.070923]  }
[   37.070992]  ... key      at: [<ffff800082ce7398>] kvcache_dict_init.__key+0x0/0x10
[   37.071308]
[   37.071308] the dependencies between the lock to be acquired
[   37.071309]  and SOFTIRQ-irq-unsafe lock:
[   37.071759] -> (fs_reclaim){+.+.}-{0:0} {
[   37.071923]    HARDIRQ-ON-W at:
[   37.072052]                     lock_acquire+0x118/0x26c
[   37.072268]                     fs_reclaim_acquire+0x64/0xd0
[   37.072499]                     mem_cgroup_css_alloc+0xdc/0x6e4
[   37.072740]                     cgroup_init_subsys+0x7c/0x1d0
[   37.072974]                     cgroup_init+0x2d8/0x490
[   37.073187]                     start_kernel+0x2f4/0x3c0
[   37.073403]                     __primary_switched+0x88/0x90
[   37.073633]    SOFTIRQ-ON-W at:
[   37.073761]                     lock_acquire+0x118/0x26c
[   37.074010]                     fs_reclaim_acquire+0x64/0xd0
[   37.074241]                     mem_cgroup_css_alloc+0xdc/0x6e4
[   37.074482]                     cgroup_init_subsys+0x7c/0x1d0
[   37.074735]                     cgroup_init+0x2d8/0x490
[   37.074947]                     start_kernel+0x2f4/0x3c0
[   37.075160]                     __primary_switched+0x88/0x90
[   37.075388]    INITIAL USE at:
[   37.075512]                    lock_acquire+0x118/0x26c
[   37.075722]                    fs_reclaim_acquire+0x64/0xd0
[   37.075944]                    mem_cgroup_css_alloc+0xdc/0x6e4
[   37.076169]                    cgroup_init_subsys+0x7c/0x1d0
[   37.076399]                    cgroup_init+0x2d8/0x490
[   37.076598]                    start_kernel+0x2f4/0x3c0
[   37.076798]                    __primary_switched+0x88/0x90
[   37.077012]  }
[   37.077078]  ... key      at: [<ffff800081f2b9f8>] __fs_reclaim_map+0x0/0x30
[   37.077349]  ... acquired at:
[   37.077464]    fs_reclaim_acquire+0x64/0xd0
[   37.077628]    __kmalloc_noprof+0xb0/0x4d4
[   37.077787]    kvcache_incr+0xe0/0x21c
[   37.077967]    shdict_incr+0x34/0x44
[   37.078108]    luaD_precall+0x330/0x674
[   37.078259]    luaV_execute+0xaac/0x119c
[   37.078413]    luaD_call+0xb0/0x128
[   37.078550]    f_call+0x1c/0x28
[   37.078692]    luaD_rawrunprotected+0x7c/0xb8
[   37.078862]    luaD_pcall+0x40/0x184
[   37.079003]    lua_pcall+0xac/0x190
[   37.079191]    lua_lsm_file_permission+0x1fc/0x36c
[   37.079379]    security_file_permission+0x4c/0xa8
[   37.079562]    rw_verify_area+0x54/0x138
[   37.079715]    vfs_read+0xa4/0x2b0
[   37.079846]    ksys_read+0x70/0xe4
[   37.079974]    __arm64_sys_read+0x1c/0x28
[   37.080123]    invoke_syscall+0x40/0xf8
[   37.080269]    el0_svc_common+0xa8/0xd8
[   37.080413]    do_el0_svc+0x1c/0x28
[   37.080544]    el0_svc+0x50/0xcc
[   37.080665]    el0t_64_sync_handler+0x84/0x12c
[   37.080831]    el0t_64_sync+0x198/0x19c
[   37.080975]
[   37.081031]
[   37.081031] stack backtrace:
[   37.081195] CPU: 1 UID: 0 PID: 1610 Comm: cat Tainted: G        W           6.17.0-rc3+ #515 PREEMPT
[   37.081535] Tainted: [W]=WARN
[   37.081646] Hardware name: QEMU QEMU Virtual Machine, BIOS edk2-stable202408-prebuilt.qemu.org 08/13/2024
[   37.082039] Call trace:
[   37.082130]  show_stack+0x18/0x24 (C)
[   37.082269]  __dump_stack+0x28/0x38
[   37.082400]  dump_stack_lvl+0x64/0x84
[   37.082537]  dump_stack+0x18/0x24
[   37.082688]  __lock_acquire+0x2b2c/0x2b90
[   37.082837]  lock_acquire+0x118/0x26c
[   37.082972]  fs_reclaim_acquire+0x64/0xd0
[   37.083119]  __kmalloc_noprof+0xb0/0x4d4
[   37.083264]  kvcache_incr+0xe0/0x21c
[   37.083395]  shdict_incr+0x34/0x44
[   37.083520]  luaD_precall+0x330/0x674
[   37.083656]  luaV_execute+0xaac/0x119c
[   37.083793]  luaD_call+0xb0/0x128
[   37.083916]  f_call+0x1c/0x28
[   37.084025]  luaD_rawrunprotected+0x7c/0xb8
[   37.084180]  luaD_pcall+0x40/0x184
[   37.084306]  lua_pcall+0xac/0x190
[   37.084428]  lua_lsm_file_permission+0x1fc/0x36c
[   37.084598]  security_file_permission+0x4c/0xa8
[   37.084765]  rw_verify_area+0x54/0x138
[   37.084902]  vfs_read+0xa4/0x2b0
[   37.085021]  ksys_read+0x70/0xe4
[   37.085139]  __arm64_sys_read+0x1c/0x28
[   37.085280]  invoke_syscall+0x40/0xf8
[   37.085416]  el0_svc_common+0xa8/0xd8
[   37.085553]  do_el0_svc+0x1c/0x28
[   37.085676]  el0_svc+0x50/0xcc
[   37.085790]  el0t_64_sync_handler+0x84/0x12c
[   37.086018]  el0t_64_sync+0x198/0x19c
Signed-off-by: Zongyao Chen <ZongYao.Chen@linux.alibaba.com>
Signed-off-by: Zongyao Chen <ZongYao.Chen@linux.alibaba.com>
Signed-off-by: Zongyao Chen <ZongYao.Chen@linux.alibaba.com>
Signed-off-by: Zongyao Chen <ZongYao.Chen@linux.alibaba.com>
Generic /proc/<pid>/attr/* uses legacy single-owner lsm dispatch.
When Lua-LSM registers getprocattr/setprocattr ahead of SELinux,
procattr operations are routed to Lua first and fail with -EINVAL.

Teach Lua-LSM to treat getprocattr and setprocattr as unsupported,
and skip registering them in lua_lsm_init().
This leaves the legacy procattr ABI to the LSMs that actually implement it.

Signed-off-by: Zongyao Chen <ZongYao.Chen@linux.alibaba.com>
security_lsmprop_to_secctx() uses the first registered LSM when called
with LSM_ID_UNDEF. audit_log_subj_ctx() takes that path in the
single-provider case.

Lua-LSM does not implement lsmprop_to_secctx yet and currently falls
back to the default -EOPNOTSUPP handler. When Lua is ordered before
SELinux, audit reaches Lua first and emits repeated
"audit: error in audit_log_subj_ctx" messages.

Mark lsmprop_to_secctx unsupported and do not register it for Lua-LSM
for now. This leaves secctx conversion to LSMs that implement it.

Signed-off-by: Zongyao Chen <ZongYao.Chen@linux.alibaba.com>
Lua-LSM should not advertise hooks that it does not support or
register.

Reject unsupported hook names during module loading, and omit them from
kernel.lsm_funcs() and /sys/kernel/security/lua/lsm_funcs.

Signed-off-by: Zongyao Chen <ZongYao.Chen@linux.alibaba.com>
Document that Lua-LSM exposes only its supported hook set through
module APIs and observability output, and call out that getprocattr,
setprocattr, and lsmprop_to_secctx are not part of that surface.

Signed-off-by: Zongyao Chen <ZongYao.Chen@linux.alibaba.com>
Signed-off-by: Zongyao Chen <ZongYao.Chen@linux.alibaba.com>
Signed-off-by: Zongyao Chen <ZongYao.Chen@linux.alibaba.com>
Signed-off-by: Zongyao Chen <ZongYao.Chen@linux.alibaba.com>
uudiin and others added 9 commits May 12, 2026 14:57
Signed-off-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
kernel_task_is_descendant() accepts either a task userdata or a pid. The
pid path takes a task reference with find_get_task_by_vpid(), but the
RCU walk may replace parent with parent->group_leader before the final
put_task_struct().

When the pid names a non-leader thread, this leaks the looked-up task
and drops a reference from the group leader without having acquired one
here.

Keep the looked-up task in a separate pointer and release that reference
after the walk. The group leader pointer is only used for comparison
under the RCU read-side critical section.

Signed-off-by: Zongyao Chen <ZongYao.Chen@linux.alibaba.com>
arg2cap() accepts capability names and numeric capability values. The
name path resolves values from a fixed table, but the numeric path
returned the Lua integer without checking that it names a valid
capability.

Callers pass the result to helpers such as cap_raise(), cap_lower(),
and cap_capable(). Reject invalid values in arg2cap() so Lua policy
cannot drive capability helpers with out-of-range bit numbers.

Signed-off-by: Zongyao Chen <ZongYao.Chen@linux.alibaba.com>
Some Lua accessors expose optional kernel pointers. d_backing_inode(),
sk->sk_socket, sock->sk, skb->sk, and skb_to_full_sk() can return
NULL, but the accessors wrapped those values as typed userdata.

Lua policy then sees a non-nil object and can call methods that
dereference the stored pointer without another NULL check.

Return nil for missing optional objects, matching the existing
convention used by Lua-LSM hook argument marshalling.

Signed-off-by: Zongyao Chen <ZongYao.Chen@linux.alibaba.com>
filp_open() can return ERR_PTR(). Keep that value out of the GC-backed
userdata so meth_file_gc() never passes an error pointer to fput().

Store the file pointer in a temporary first and publish it to the
userdata only after the open succeeds.

Signed-off-by: Zongyao Chen <ZongYao.Chen@linux.alibaba.com>
lua_tolstring() returns the byte length through an out parameter.
The marshalling helpers passed pointer-sized storage in places that
only need the returned length value.

Use the local length variable consistently so Lua strings with embedded
NUL bytes are marshalled using the length reported by Lua.

Signed-off-by: Zongyao Chen <ZongYao.Chen@linux.alibaba.com>
Lua-LSM publishes module pointers under modules_ss SRCU. The unregister
path removes a module from the list before freeing it, but readers that
already entered the SRCU read-side critical section may still hold the
old pointer.

Wait for an SRCU grace period after unlinking the module and before
freeing its storage.

Signed-off-by: Zongyao Chen <ZongYao.Chen@linux.alibaba.com>
AF_UNIX socket addresses are not C strings in all cases. Abstract
socket names may contain embedded NUL bytes and their usable length
comes from the sockaddr length passed by the hook.

Carry the sockaddr length together with the pointer exposed to Lua and
use that length when formatting AF_UNIX paths. This preserves abstract
socket names and avoids reading past the supplied sockaddr.

Signed-off-by: Zongyao Chen <ZongYao.Chen@linux.alibaba.com>
@chenzongyao200127
chenzongyao200127 force-pushed the lua-lsm-b13d9e8-af-unix-sockaddr-len branch from 6e0b273 to 40da5d4 Compare May 14, 2026 09:23
@chenzongyao200127
chenzongyao200127 deleted the lua-lsm-b13d9e8-af-unix-sockaddr-len branch June 3, 2026 13:13
@chenzongyao200127
chenzongyao200127 restored the lua-lsm-b13d9e8-af-unix-sockaddr-len branch June 4, 2026 01:58
uudiin pushed a commit that referenced this pull request Aug 21, 2026
Tariq Toukan says:

====================
devlink: add per-port resource support

This series by Or adds devlink per-port resource support:

Currently, devlink resources are only available at the device level.
However, some resources are inherently per-port, such as the maximum
number of subfunctions (SFs) that can be created on a specific PF port.
This limitation prevents user space from obtaining accurate per-port
capacity information.
This series adds infrastructure for per-port resources in devlink core
and implements it in the mlx5 driver to expose the max_SFs resource
on PF devlink ports.

Patch #1  refactors resource functions to be generic
Patch #2  adds port-level resource registration infrastructure
Patch #3  registers SF resource on PF port representor in mlx5
Patch #4  adds devlink port resource registration to netdevsim for testing
Patch #5  adds dump support for device-level resources
Patch #6  includes port resources in the resource dump dumpit path
Patch #7  adds port-specific option to resource dump doit path
Patch #8  adds selftest for devlink port resource doit
Patch #9  documents port-level resources and full dump
Patch #10 adds resource scope filtering to resource dump
Patch #11 adds selftest for resource dump and scope filter
Patch #12 documents resource scope filtering
====================

Link: https://patch.msgid.link/20260407194107.148063-1-tariqt@nvidia.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
uudiin pushed a commit that referenced this pull request Aug 21, 2026
Currently, the initialization of loongarch_jump_ops does not contain an
assignment to its .free field. This causes disasm_line__free() to fall
through to ins_ops__delete() for LoongArch jump instructions.

ins_ops__delete() will free ins_operands.source.raw and
ins_operands.source.name, and these fields overlaps with
ins_operands.jump.raw_comment and ins_operands.jump.raw_func_start.
Since in loongarch_jump__parse(), these two fields are populated by
strchr()-ing the same buffer, trying to free them will lead to undefined
behavior.

This invalid free usually leads to crashes:

        Process 1712902 (perf) of user 1000 dumped core.
        Stack trace of thread 1712902:
        #0  0x00007fffef155c58 n/a (libc.so.6 + 0x95c58)
        #1  0x00007fffef0f7a94 raise (libc.so.6 + 0x37a94)
        #2  0x00007fffef0dd6a8 abort (libc.so.6 + 0x1d6a8)
        #3  0x00007fffef145490 n/a (libc.so.6 + 0x85490)
        #4  0x00007fffef1646f4 n/a (libc.so.6 + 0xa46f4)
        #5  0x00007fffef164718 n/a (libc.so.6 + 0xa4718)
        #6  0x00005555583a6764 __zfree (/home/csmantle/dist/linux-arch/tools/perf/perf + 0x106764)
        #7  0x000055555854fb70 disasm_line__free (/home/csmantle/dist/linux-arch/tools/perf/perf + 0x2afb70)
        #8  0x000055555853d618 annotated_source__purge (/home/csmantle/dist/linux-arch/tools/perf/perf + 0x29d618)
        #9  0x000055555852300c __hist_entry__tui_annotate (/home/csmantle/dist/linux-arch/tools/perf/perf + 0x28300c)
        #10 0x0000555558526718 do_annotate (/home/csmantle/dist/linux-arch/tools/perf/perf + 0x286718)
        #11 0x000055555852ed94 evsel__hists_browse (/home/csmantle/dist/linux-arch/tools/perf/perf + 0x28ed94)
        #12 0x000055555831fdd0 cmd_report (/home/csmantle/dist/linux-arch/tools/perf/perf + 0x7fdd0)
        #13 0x000055555839b644 handle_internal_command (/home/csmantle/dist/linux-arch/tools/perf/perf + 0xfb644)
        #14 0x00005555582fe6ac main (/home/csmantle/dist/linux-arch/tools/perf/perf + 0x5e6ac)
        #15 0x00007fffef0ddd90 n/a (libc.so.6 + 0x1dd90)
        #16 0x00007fffef0ddf0c __libc_start_main (libc.so.6 + 0x1df0c)
        #17 0x00005555582fed10 _start (/home/csmantle/dist/linux-arch/tools/perf/perf + 0x5ed10)
        ELF object binary architecture: LoongArch

... and it can be confirmed with Valgrind:

        ==1721834== Invalid free() / delete / delete[] / realloc()
        ==1721834==    at 0x4EA9014: free (in /usr/lib/valgrind/vgpreload_memcheck-loongarch64-linux.so)
        ==1721834==    by 0x4106287: __zfree (zalloc.c:13)
        ==1721834==    by 0x42ADC8F: disasm_line__free (in /home/csmantle/dist/linux-arch/tools/perf/perf)
        ==1721834==    by 0x429B737: annotated_source__purge (in /home/csmantle/dist/linux-arch/tools/perf/perf)
        ==1721834==    by 0x42811EB: __hist_entry__tui_annotate (in /home/csmantle/dist/linux-arch/tools/perf/perf)
        ==1721834==    by 0x42848D7: do_annotate (in /home/csmantle/dist/linux-arch/tools/perf/perf)
        ==1721834==    by 0x428CF33: evsel__hists_browse (in /home/csmantle/dist/linux-arch/tools/perf/perf)
        ==1721834==  Address 0x7d34303 is 35 bytes inside a block of size 62 alloc'd
        ==1721834==    at 0x4EA59B8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-loongarch64-linux.so)
        ==1721834==    by 0x6B80B6F: strdup (strdup.c:42)
        ==1721834==    by 0x42AD917: disasm_line__new (in /home/csmantle/dist/linux-arch/tools/perf/perf)
        ==1721834==    by 0x42AE5A3: symbol__disassemble_objdump (in /home/csmantle/dist/linux-arch/tools/perf/perf)
        ==1721834==    by 0x42AF0A7: symbol__disassemble (in /home/csmantle/dist/linux-arch/tools/perf/perf)
        ==1721834==    by 0x429B3CF: symbol__annotate (in /home/csmantle/dist/linux-arch/tools/perf/perf)
        ==1721834==    by 0x429C233: symbol__annotate2 (in /home/csmantle/dist/linux-arch/tools/perf/perf)
        ==1721834==    by 0x42804D3: __hist_entry__tui_annotate (in /home/csmantle/dist/linux-arch/tools/perf/perf)
        ==1721834==    by 0x42848D7: do_annotate (in /home/csmantle/dist/linux-arch/tools/perf/perf)
        ==1721834==    by 0x428CF33: evsel__hists_browse (in /home/csmantle/dist/linux-arch/tools/perf/perf)

This patch adds the missing free() specialization in loongarch_jump_ops,
which prevents disasm_line__free() from invoking the default cleanup
function.

Fixes: fb7fd2a ("perf annotate: Move raw_comment and raw_func_start fields out of 'struct ins_operands'")
Cc: stable@vger.kernel.org
Cc: WANG Rui <wangrui@loongson.cn>
Cc: Huacai Chen <chenhuacai@kernel.org>
Cc: WANG Xuerui <kernel@xen0n.name>
Cc: loongarch@lists.linux.dev
Signed-off-by: Rong Bao <rong.bao@csmantle.top>
Tested-by: WANG Rui <wangrui@loongson.cn>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
uudiin pushed a commit that referenced this pull request Aug 21, 2026
ice_reset_all_vfs() ignores the return value of ice_vf_rebuild_vsi().
When the VSI rebuild fails (e.g. during NVM firmware update via
nvmupdate64e), ice_vsi_rebuild() tears down the VSI on its error path,
leaving txq_map and rxq_map as NULL. The subsequent unconditional call
to ice_vf_post_vsi_rebuild() leads to a NULL pointer dereference in
ice_ena_vf_q_mappings() when it accesses vsi->txq_map[0].

The single-VF reset path in ice_reset_vf() already handles this
correctly by checking the return value of ice_vf_reconfig_vsi() and
skipping ice_vf_post_vsi_rebuild() on failure.

Apply the same pattern to ice_reset_all_vfs(): check the return value
of ice_vf_rebuild_vsi() and skip ice_vf_post_vsi_rebuild() and
ice_eswitch_attach_vf() on failure. The VF is left safely disabled
(ICE_VF_STATE_INIT not set, VFGEN_RSTAT not set to VFACTIVE) and can
be recovered via a VFLR triggered by a PCI reset of the VF
(sysfs reset or driver rebind).

Note that this patch does not prevent the VF VSI rebuild from failing
during NVM update — the underlying cause is firmware being in a
transitional state while the EMP reset is processed, which can cause
Admin Queue commands (ice_add_vsi, ice_cfg_vsi_lan) to fail. This
patch only prevents the subsequent NULL pointer dereference that
crashes the kernel when the rebuild does fail.

 crash> bt
     PID: 50795    TASK: ff34c9ee708dc680  CPU: 1    COMMAND: "kworker/u512:5"
      #0 [ff72159bcfe5bb50] machine_kexec at ffffffffaa8850ee
      #1 [ff72159bcfe5bba8] __crash_kexec at ffffffffaaa15fba
      #2 [ff72159bcfe5bc68] crash_kexec at ffffffffaaa16540
      #3 [ff72159bcfe5bc70] oops_end at ffffffffaa837eda
      #4 [ff72159bcfe5bc90] page_fault_oops at ffffffffaa893997
      #5 [ff72159bcfe5bce8] exc_page_fault at ffffffffab528595
      #6 [ff72159bcfe5bd10] asm_exc_page_fault at ffffffffab600bb2
         [exception RIP: ice_ena_vf_q_mappings+0x79]
         RIP: ffffffffc0a85b29  RSP: ff72159bcfe5bdc8  RFLAGS: 00010206
         RAX: 00000000000f0000  RBX: ff34c9efc9c00000  RCX: 0000000000000000
         RDX: 0000000000000000  RSI: 0000000000000010  RDI: ff34c9efc9c00000
         RBP: ff34c9efc27d4828   R8: 0000000000000093   R9: 0000000000000040
         R10: ff34c9efc27d4828  R11: 0000000000000040  R12: 0000000000100000
         R13: 0000000000000010  R14:   R15:
         ORIG_RAX: ffffffffffffffff  CS: 0010  SS: 0018
      #7 [ff72159bcfe5bdf8] ice_sriov_post_vsi_rebuild at ffffffffc0a85e2e [ice]
      #8 [ff72159bcfe5be08] ice_reset_all_vfs at ffffffffc0a920b4 [ice]
      #9 [ff72159bcfe5be48] ice_service_task at ffffffffc0a31519 [ice]
     #10 [ff72159bcfe5be88] process_one_work at ffffffffaa93dca4
     #11 [ff72159bcfe5bec8] worker_thread at ffffffffaa93e9de
     #12 [ff72159bcfe5bf18] kthread at ffffffffaa946663
     #13 [ff72159bcfe5bf50] ret_from_fork at ffffffffaa8086b9

 The panic occurs attempting to dereference the NULL pointer in RDX at
 ice_sriov.c:294, which loads vsi->txq_map (offset 0x4b8 in ice_vsi).

 The faulting VSI is an allocated slab object but not fully initialized
 after a failed ice_vsi_rebuild():

  crash> struct ice_vsi 0xff34c9efc27d4828
    netdev = 0x0,
    rx_rings = 0x0,
    tx_rings = 0x0,
    q_vectors = 0x0,
    txq_map = 0x0,
    rxq_map = 0x0,
    alloc_txq = 0x10,
    num_txq = 0x10,
    alloc_rxq = 0x10,
    num_rxq = 0x10,

 The nvmupdate64e process was performing NVM firmware update:

  crash> bt 0xff34c9edd1a30000
  PID: 49858    TASK: ff34c9edd1a30000  CPU: 1    COMMAND: "nvmupdate64e"
   #0 [ff72159bcd617618] __schedule at ffffffffab5333f8
   #4 [ff72159bcd617750] ice_sq_send_cmd at ffffffffc0a35347 [ice]
   #5 [ff72159bcd6177a8] ice_sq_send_cmd_retry at ffffffffc0a35b47 [ice]
   #6 [ff72159bcd617810] ice_aq_send_cmd at ffffffffc0a38018 [ice]
   #7 [ff72159bcd617848] ice_aq_read_nvm at ffffffffc0a40254 [ice]
   #8 [ff72159bcd6178b8] ice_read_flat_nvm at ffffffffc0a4034c [ice]
   #9 [ff72159bcd617918] ice_devlink_nvm_snapshot at ffffffffc0a6ffa5 [ice]

 dmesg:
  ice 0000:13:00.0: firmware recommends not updating fw.mgmt, as it
    may result in a downgrade. continuing anyways
  ice 0000:13:00.1: ice_init_nvm failed -5
  ice 0000:13:00.1: Rebuild failed, unload and reload driver

Fixes: 12bb018 ("ice: Refactor VF reset")
Signed-off-by: Petr Oros <poros@redhat.com>
Tested-by: Rafal Romanowski <rafal.romanowski@intel.com>
Signed-off-by: Jacob Keller <jacob.e.keller@intel.com>
Link: https://patch.msgid.link/20260427-jk-iwl-net-petr-oros-fixes-v1-5-cdcb48303fd8@intel.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants