您的位置:首页 > 文档 > 网络安全 >
 文章分类 

LIDS精通与进阶--五、密封内核


创建:2005-11-07 00:00:00
作者:Unlinux
来自: http://www.Unlinux.com

冰块


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


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

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()保护下。如果没有被保护,它就会启动报警信息。

转载自:http://www.unlinux.com/doc/security/20051107/8521.html

评论】 【加入收藏夹】 【 】 【打印】 【关闭
 *  请尊重我们的劳动,转载请注明出自UnLinux.Com及作者名 * 

※ 相关链接
 ·LIDS精通与进阶(中)  (2005-11-07 00:00:00)
 ·LIDS精通与进阶(下)  (2005-11-07 00:00:00)
 ·用LIDS建立安全的系统(上)  (2005-11-07 00:00:00)
 ·用LIDS建立安全的系统(下)  (2005-11-07 00:00:00)
 ·LIDS精通与进阶(上)  (2005-11-07 00:00:00)
 ·LIDS译本  (2005-11-07 00:00:00)
 ·LIDS精通与进阶--一、系统入侵  (2005-11-07 00:00:00)
 ·LIDS精通与进阶--二、保护文件系统  (2005-11-07 00:00:00)
 ·LIDS精通与进阶--三、保护设备  (2005-11-07 00:00:00)
 ·LIDS精通与进阶--四、保护重要进程  (2005-11-07 00:00:00)

Copyright © 2005 UnLinux.Com All Rights Reserved