Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1074612
  • 博文数量: 1647
  • 博客积分: 80000
  • 博客等级: 元帅
  • 技术积分: 9980
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 15:15
文章分类

全部博文(1647)

文章存档

2011年(1)

2008年(1646)

我的朋友

分类:

2008-10-28 18:34:30


  五.密封内核
  
  我们需要在系统启动的时候做一些必要的操作,但是我们也需要在系统运行的时候保护它们。
  
  例如,我们需要象内核里插入一些需要的模块,但是我们不希望在系统运行的时候插入任何模块,因为那样会十分危险。如何解决这个问题呢?这里就有一些密封的方法。我们可以在系统启动的时候做我们任何想做的事情,然后我们就密封内核。然后,我们就不能做那些以前在没有密封的时候可以做的事情。用密封的方法,我们可以用模块来解决问题,我们可以在密封前向内核里插入我们想要的模块,在密封后我们就不可以在内核里插入或是删除任何模块。
  
  5.1 用LIDS密封内核
  为了密封内核,我们可以用下面的LIDS命令
  
  #lidsadm –I -- -CAP_xxx….
  它们可以放到脚本里让系统启动的时候就执行它。具体你们可以看我以前在byte和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, "cant open " LIDS_LOCKS);
  }
  if (read(fd,&locks,sizeof(lids_locks_t))==-1) {
  perror("read");
  exit_error (2, "cant 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, "cant 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和密码。
  
  在密封前保护程序
  
  因为在密封前的状态是危险的,我们必须知道在密封前那些运行的程序是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("Execed unprotected program %s before sealing LIDS",filename);
  #endif
  }
  }
  #endif
  ......
  }
  你会看到当LIDS保护系统开启(lids_load==1)和当前系统没有密封(lids_firest_time 为1)的时候,内核就会检查当前程序是否在LIDS的lids_check_base()保护下。如果没有被保护,它就会启动报警信息。
  
  
  六.LIDS与Capability
  Capability是一套来表明一个进程可以做为什么的位标志。在LIDS,我们可以用capability的限制来限制所有的进程。
  在/include//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_load) || (!lids_local_load)))
  #else
  #define cap_raised(c, flag) (cap_t(c) & CAP_TO_MASK(flag) & cap_bset)
  #endif
  
  你会看到这里的cap_bset(一般默认都是1)是很重要的。如果有人在那里把一些位置0,capability就可以会禁止整个系统。如,18 位的CAP_SYS_CHROOT, 如果我们把他置0,表明我们就不能用chroot()了。
  
  如果你看到sys_chroot的源代码,你就发现很多问题了:
  if (!capable(CAP_SYS_CHROOT)) {
  goto dput_and_out;
  }
  capable()会返回0,在位18为0,这样chroot就会给用户返回一个错误信息。
  
  6.2.在LIDS里的capability
  
  LIDS用capability来限制整个动作进程。LIDS用的函数是capable()。在内核代码中已经存在的许多capable()里。我们可以禁止一些当前系统默认的capability并且在用户违反LIDS定义的规则的时候报警。
  
  至于管理员,他们也可以用lidsadm和密码来改变capability。当内核授权用户的时候,capability变量cap_bset 就会改变。
  
  作为管理员一个需要理解的重要东西是每一个capability的意思。然后,在密封内核的时候禁止c
【责编:admin】

--------------------next---------------------

阅读(272) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~