争怎路由网:是一个主要分享无线路由器安装设置经验的网站,汇总WiFi常见问题的解决方法。

LIDS精通与进阶(中)

时间:2024/7/2作者:未知来源:争怎路由网人气:

=S_IMMUTABLE;

}

...

}

然后如果进程被PF_HIDDEN标记,它就不会在proc文件系统里显示。


五、密封内核

我们需要在系统启动的时候做一些必要的操作,但是我们也需要在系统运行的时候保护它们。


例如,我们需要象内核里插入一些需要的模块,但是我们不希望在系统运行的时候插入任何模块,因为那样会十分危险。如何解决这个问题呢?这里就有一些密封的方法。我们可以在系统启动的时候做我们任何想做的事情,然后我们就密封内核。然后,我们就不能做那些以前在没有密封的时候可以做的事情。用密封的方法,我们可以用模块来解决问题,我们可以在密封前向内核里插入我们想要的模块,在密封后我们就不可以在内核里插入或是删除任何模块。

1、用LIDS密封内核

为了密封内核,我们可以用下面的LIDS命令


#lidsadm –I -- -CAP_xxx….

它们可以放到脚本里让系统启动的时候就执行它。具体你们可以看我以前在linuxbyte和chinabyte发表的文章。LIDS是通过/proc/sys/lids/locks和内核通讯的。


当你密封了内核,lidsadm是调用lidsadm.c的lids_init()的调用。


#define LIDS_LOCKS "/proc/sys/lids/locks"

......

void lids_init(int optind, int argc, char *argv[])

{

......

if ((fd=open(LIDS_LOCKS,O_RDWR)) == -1) {

perror("open");

exit_error (2, "can't open " LIDS_LOCKS);

}

if (read(fd,&locks,sizeof(lids_locks_t))==-1) {

perror("read");

exit_error (2, "can't read " LIDS_LOCKS);

}


lids_set_caps(optind,argc,argv,&locks);


locks.magic1=LIDS_MAGIC_1;

.........


if (write(fd,&locks,sizeof(lids_locks_t))==-1) {

perror("write");

exit_error (2, "can't write " LIDS_LOCKS);

}

.....

}


这个系统调用在LIDS_LOCKS生成新的变量loks,内核会通过lids_proc_locks_sysctl()命令来读取它。Lids_proc_locks_sysctl也会从用户区完全检查并读取它,然后改变密封的变量lids_first_time为0。


让我们看看lids_proc_locks_sysctl().这个函数会在用户读写/proc/sys/lids/locks的时候调用。

int lids_proc_locks_sysctl(ctl_table *table, int write, struct file *filp,

void *buffer, size_t *lenp, int conv, int op)

{

...........

 

/* first: check the terminal and the program which access the sysctl */

#ifndef CONFIG_LIDS_REMOTE_SWITCH

if (current->tty && (current->tty->driver.type != 2) ) {

lids_security_alert("Try to %s locks sysctl (unauthorized terminal)",

write ? "write" : "read");

return -EPERM;

}

#endif

........

/* second: check wether it is not a timeout period after two many failed attempts */

.......

if (write) {

/* Third : check what is submitted (size, magics, passwd) */

if (*lenp != sizeof(lids_locks_t)) {

lids_security_alert("Try to feed locks sysctl with garbage");

return -EINVAL;

}

if (copy_from_user(&locks,buffer,sizeof(lids_locks_t)))

return -EFAULT;

.......

if ((lids_first_time) && (!locks.passwd[0])) {

.........

number_failed=0;

if (lids_process_flags(locks.flags)) {

cap_bset=locks.cap_bset;

lids_security_alert("Changed: cap_bset=0x%x lids_flags=0x%x",cap_t(cap_bset),lids_flags);

}

Change flag here ..--> lids_first_time=0;

.....

}

上面的函数会在密封内核或是改变内核安全级别的时候工作。变量lids_first_time是一个表明当前密封状态的的一个标志。当改变了需要的使能位,这个标志就会置1表明当前的状态是“密封后“。


密封内核有两个任务,首先,改变使能位,然后,改变lids_first_time标志为1。在密封后,系统就不允许改变它们了,除非你用lidsadm和密码。


2、在密封前保护程序


因为在密封前的状态是危险的,我们必须知道在密封前那些运行的程序是LIDS来保护的。为什么呢?因为密封后我们就不能改变它们了。如果文件没有被保护,一些人就可以改变他们然后重新启动,这些程序可能对系统非常危险。让我们来看看在没有密封前一个运行的非保护程序的代码。

int do_execve(char * filename, char ** argv, char ** envp, struct pt_regs * regs)

{

..........

#ifdef CONFIG_LIDS_SA_EXEC_UP

if (lids_first_time && lids_load) {

if (!lids_check_base(dentry,LIDS_READONLY))

#ifdef CONFIG_LIDS_NO_EXEC_UP

lids_security_alert("Try to exec unprotected program %s before sealing LIDS",filename);

if (dentry)

dput(dentry);

return -EPERM;

#else

lids_security_alert("Exec'ed unprotected program %s before sealing LIDS",filename);

#endif

}

}

#endif

......

}

你会看到当LIDS保护系统开启(lids_load==1)和当前系统没有密封(lids_firest_time 为1)的时候,内核就会检查当前程序是否在LIDS的lids_check_base()保护下。如果没有被保护,它就会启动报警信息。


六、LIDS与Capability

1、Capability是一套来表明一个进程可以做为什么的位标志。在LIDS,我们可以用capability的限制来限制所有的进程。

在/include/linux/capability.h


typedef struct __user_cap_header_struct {

__u32 version;

int pid;

} *cap_user_header_t;

typedef struct __user_cap_data_struct {

__u32 effective;

__u32 permitted;

__u32 inheritable;

} *cap_user_data_t;

#ifdef __KERNEL__

/* #define STRICT_CAP_T_TYPECHE

#ifdef STRICT_CAP_T_TYPECHECKS

typedef struct kernel_cap_struct {

__u32 cap;

} kernel_cap_t;

#else

typedef __u32 kernel_cap_t;

#endif

kernel_cap_t cap_bset = CAP_FULL_SET;

在kernel_ap_t的每一位都代表一个许可。Cap_bset是capability集的主要部分。它们的值可以通过改变/proc/sys/kernel/cap-bound来改变。


看看上面的文件,你就会发现一些问题。

 

/* in include/linux/capability.h */

/* In a system with the [_POSIX_CHOWN_RESTRICTED] option defined, this

overrides the restriction of changing file ownership and group

ownership. */

#define CAP_CHOWN 0

/* Override all DAC access, including ACL execute access if

[_POSIX_ACL] is defined. Excluding DAC access covered by

CAP_LINUX_IMMUTABLE. */

#define CAP_DAC_OVERRIDE 1

/* Overrides all DAC restrictions regarding read and search on files

and directories, including ACL restrictions if [_POSIX_ACL] is

defined. Excluding DAC access covered by CAP_LINUX_IMMUTABLE. */

#define CAP_DAC_READ_SEARCH 2

.........

每一个任务(进程)在结构task_struct定义了三个成员:cap_effective,cap_inheritable,cap_permitted.我们已经有了一个用来表明基本capability的变量cap_bset。它们会检测这个系统并确定那种capability用来控制系统。

在内核实现的大部分系统调用会调用函数capable() (在kernel/sched.c)。然后会调用cap_raised() (在/include/linux/capability.h)。如下:

#ifdef CONFIG_LIDS_ALLOW_SWITCH

#define cap_raised(c, flag) ((cap_t(c) & CAP_TO_MASK(flag)) && ((CAP_TO_MASK(flag) & cap_bset)

关键词:LIDS精通与进阶(中)




Copyright © 2012-2018 争怎路由网(http://www.zhengzen.com) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版