Skip to content

lua-lsm: expose kernel file load ids#18

Open
chenzongyao200127 wants to merge 2 commits into
openanolis:lua-lsmfrom
chenzongyao200127:lua-lsm-file-load-ids
Open

lua-lsm: expose kernel file load ids#18
chenzongyao200127 wants to merge 2 commits into
openanolis:lua-lsmfrom
chenzongyao200127:lua-lsm-file-load-ids

Conversation

@chenzongyao200127
Copy link
Copy Markdown
Collaborator

@chenzongyao200127 chenzongyao200127 commented May 14, 2026

Lua-LSM currently leaves the file/load id arguments for kernel_load_data(), kernel_post_load_data(), kernel_read_file(), and kernel_post_read_file() as nil placeholders. This makes Lua policy unable to distinguish kernel modules, firmware, security policy loads, and other kernel file/load users without looking at unrelated context.

Expose those ids as the stable strings returned by kernel_load_data_id_str() and kernel_read_file_id_str(), and drop the separate Lua-visible size argument from the post hooks. The post hooks already expose the data as binary-safe Lua strings, so policy can use the Lua length operator instead.

Example Lua policy:

return {
    name = "file-load-policy",
    author = "Zongyao Chen",
    license = "GPL",

    kernel_load_data = function(id, contents)
        if id == "kernel-module" and not contents then
            return false, errno.EPERM
        end
        return true
    end,

    kernel_post_load_data = function(data, id, description)
        if id ~= "kernel-module" then
            return true
        end

        if #data < 4 or data:sub(1, 4) ~= "\127ELF" then
            return false, errno.EPERM
        end

        return true
    end,

    kernel_read_file = function(file, id, contents)
        return true
    end,

    kernel_post_read_file = function(file, data, id)
        if id == "kernel-module" and #data >= 4 then
            return data:sub(1, 4) == "\127ELF"
        end
        return true
    end,
}

The series contains these commits:

  • lua-lsm: expose kernel file load ids to Lua hooks
  • lua-lsm: drop redundant size args for file data hooks

Validation:

  • ./scripts/checkpatch.pl --git origin/lua-lsm..lua-lsm-file-load-ids
  • git diff --check origin/lua-lsm..lua-lsm-file-load-ids

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

Lua-LSM left the id arguments for kernel_load_data(),
kernel_post_load_data(), kernel_read_file(), and kernel_post_read_file()
as nil placeholders.

Expose those ids as the stable strings returned by
kernel_load_data_id_str() and kernel_read_file_id_str(). Lua policies
can compare descriptive values such as "kernel-module" or
"security-policy" without depending on kernel enum numbers.

Signed-off-by: Zongyao Chen <ZongYao.Chen@linux.alibaba.com>
kernel_post_load_data() and kernel_post_read_file() expose file data to
Lua as binary-safe strings. Lua policy can get the byte length with the
Lua length operator, so passing a separate size argument duplicates
information already carried by the string.

Drop the Lua-visible size argument from those hooks while keeping the
kernel hook signatures unchanged.

Signed-off-by: Zongyao Chen <ZongYao.Chen@linux.alibaba.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.

1 participant