Linux内核如何知道应该从系统调用路径参数中读取多少字节? [英] How does the Linux kernel know how many bytes it should read from system call path arguments?

查看:67
本文介绍了Linux内核如何知道应该从系统调用路径参数中读取多少字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在google中搜索时发现Linux内核使用结构体来获取变量.

I searched in google I found that Linux kernel uses a struct for variables.

#define EMBEDDED_LEVELS 2
struct nameidata {
    struct path path;
    struct qstr last;
    struct path root;
    struct inode    *inode; /* path.dentry.d_inode */
    unsigned int    flags;
    unsigned    seq, m_seq;
    int     last_type;
    unsigned    depth;
    int     total_link_count;
    struct saved {
        struct path link;
        struct delayed_call done;
        const char *name;
        unsigned seq;
    } *stack, internal[EMBEDDED_LEVELS];
    struct filename *name;
    struct nameidata *saved;
    struct inode    *link_inode;
    unsigned    root_seq;
    int     dfd;
} __randomize_layout;

例如,用于 execve 系统调用(在此处找到

for example for execve systeml call (found here https://elixir.bootlin.com/linux/latest/source/fs/exec.c)
this function will pass the filename pointer to another function as a pathName and set the nameidata struct name to this pathName

static int __do_execve_file(int fd, struct filename *filename,
                struct user_arg_ptr argv,
                struct user_arg_ptr envp,
                int flags, struct file *file)

我的问题是如何计算从堆栈传递给该函数的参数的长度(例如"/bin/sh" )?

my question here is how is it calculating the length of the parameter passed to this function from stack (for example "/bin/sh")?

(编者注:

(Editor's note: the const char *pathname arg to execve(2) doesn't have to point to stack memory. I think this question is assuming the shellcode use-case where you do construct a path on the user-space stack and pass a pointer to that.)

(我正在学习汇编,我被困在系统调用的参数传递部分)

(I am learning assembly and I'm stuck in parameter passing section to system calls)

推荐答案

Linux使用零终止的字符串,这是C的标准字符串格式.字符串的结尾由零字节标记,除第一个零以外的任何字节字符串中的字节不是字符串的一部分.值得注意的是,这意味着文件名中不能包含零字节.(出于同样的原因,大多数shellcode不能有零字节,因为它们是为了利用某种字符串缓冲区溢出.)

Linux uses zero terminated strings, which are the standard string format for C. The end of the string is marked by a zero byte, any bytes beyond the first zero byte in the string are not part of the string. Notably that this means that filenames cannot have a zero byte in them. (For the same reason most shellcode can't have a zero byte, as they're meant to exploit some sort of string buffer overflow.)

在实践中,内核通常不需要知道文件名的长度,而是使用像 strcmp 这样的函数逐字节比较字符串,在比较不同的第一个字节处停止,或在遇到的第一个零字节.但是,如有必要,可以使用类似 strlen 的函数来计算字符串的长度.

In practice the kernel often doesn't need to know the length of a filename, and uses functions like strcmp which compare strings byte by byte, stopping either at the first byte that compares different or at the first zero byte encountered. If necessary however the length of a string can be computed with a function like strlen.

这篇关于Linux内核如何知道应该从系统调用路径参数中读取多少字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆