linux内核capable源代码分析

桃扇骨 2022-08-21 02:24 418阅读 0赞

linux内核里对于进程的权限管理有一个很重要的函数capable,以前看了好多遍,今天下决心搞定他,也在此立下一个碑,以后有谁想搞明白他的话,我还可以提供一些帮助。
capable函数定义在kernel/capability.c,作用是检验当前进程有没有相应的权限,定义如下

  1. int capable(int cap)
  2. {
  3. return __capable(current, cap);
  4. }

继续看__capable函数,这个函数也定义在kernel/capability.c,定义如下

  1. int __capable(struct task_struct *t, int cap)
  2. {
  3. /*首先执行security_capable函数检查,如果成功就给进程的flags置位,标志获得超级权限,PF_SUPERPRIV定义如下
  4. #define PF_SUPERPRIV 0x00000100 /* used super-user privileges */就是超级用户的意思
  5. */
  6. if (security_capable(t, cap) == 0) {
  7. t->flags |= PF_SUPERPRIV;
  8. return 1;
  9. }
  10. return 0;
  11. }

我们继续看security_capable函数,定义在linux/security.h

  1. static inline int security_capable(struct task_struct *tsk, int cap)
  2. {
  3. return cap_capable(tsk, cap);
  4. }

继续看cap_capable函数,定义在security/commonncap.c

  1. int cap_capable (struct task_struct *tsk, int cap)
  2. {
  3. /* 权限检查的主要工作函数 */
  4. if (cap_raised(tsk->cap_effective, cap))
  5. return 0;
  6. return -EPERM;
  7. }

我们继续看cap_raised,这是一个宏,定义如下
#define CAP_TO_MASK(x) (1 << (x))
#define cap_raise(c, flag) (cap_t(c) |= CAP_TO_MASK(flag))
#define cap_lower(c, flag) (cap_t(c) &= ~CAP_TO_MASK(flag))
#define cap_raised(c, flag) (cap_t(c) & CAP_TO_MASK(flag))
所以可以看出cap_capable函数就是查看task_struct的cap_effective变量,然后与(1<<cap)执行按位与操作。
cap_effective变量就是进程结构体里的一个32位的int变量,每一个位代表一个权限,定义如下

  1. /**
  2. ** POSIX-标准定义的权限能力
  3. **/
  4. #define CAP_CHOWN 0
  5. /* Override all DAC access, including ACL execute access if
  6. [_POSIX_ACL] is defined. Excluding DAC access covered by
  7. CAP_LINUX_IMMUTABLE. */
  8. #define CAP_DAC_OVERRIDE 1
  9. /* Overrides all DAC restrictions regarding read and search on files
  10. and directories, including ACL restrictions if [_POSIX_ACL] is
  11. defined. Excluding DAC access covered by CAP_LINUX_IMMUTABLE. */
  12. #define CAP_DAC_READ_SEARCH 2
  13. /* Overrides all restrictions about allowed operations on files, where
  14. file owner ID must be equal to the user ID, except where CAP_FSETID
  15. is applicable. It doesn't override MAC and DAC restrictions. */
  16. #define CAP_FOWNER 3
  17. /* Overrides the following restrictions that the effective user ID
  18. shall match the file owner ID when setting the S_ISUID and S_ISGID
  19. bits on that file; that the effective group ID (or one of the
  20. supplementary group IDs) shall match the file owner ID when setting
  21. the S_ISGID bit on that file; that the S_ISUID and S_ISGID bits are
  22. cleared on successful return from chown(2) (not implemented). */
  23. #define CAP_FSETID 4
  24. /* Used to decide between falling back on the old suser() or fsuser(). */
  25. #define CAP_FS_MASK 0x1f
  26. /* Overrides the restriction that the real or effective user ID of a
  27. process sending a signal must match the real or effective user ID
  28. of the process receiving the signal. */
  29. #define CAP_KILL 5
  30. /* Allows setgid(2) manipulation */
  31. /* Allows setgroups(2) */
  32. /* Allows forged gids on socket credentials passing. */
  33. #define CAP_SETGID 6
  34. /* Allows set*uid(2) manipulation (including fsuid). */
  35. /* Allows forged pids on socket credentials passing. */
  36. #define CAP_SETUID 7
  37. /**
  38. ** Linux-specific capabilities
  39. **/
  40. /* Transfer any capability in your permitted set to any pid,
  41. remove any capability in your permitted set from any pid */
  42. #define CAP_SETPCAP 8
  43. /* Allow modification of S_IMMUTABLE and S_APPEND file attributes */
  44. #define CAP_LINUX_IMMUTABLE 9
  45. /* Allows binding to TCP/UDP sockets below 1024 */
  46. /* Allows binding to ATM VCIs below 32 */
  47. #define CAP_NET_BIND_SERVICE 10
  48. /* Allow broadcasting, listen to multicast */
  49. #define CAP_NET_BROADCAST 11
  50. /* Allow interface configuration */
  51. /* Allow administration of IP firewall, masquerading and accounting */
  52. /* Allow setting debug option on sockets */
  53. /* Allow modification of routing tables */
  54. /* Allow setting arbitrary process / process group ownership on
  55. sockets */
  56. /* Allow binding to any address for transparent proxying */
  57. /* Allow setting TOS (type of service) */
  58. /* Allow setting promiscuous mode */
  59. /* Allow clearing driver statistics */
  60. /* Allow multicasting */
  61. /* Allow read/write of device-specific registers */
  62. /* Allow activation of ATM control sockets */
  63. #define CAP_NET_ADMIN 12
  64. /* Allow use of RAW sockets */
  65. /* Allow use of PACKET sockets */
  66. #define CAP_NET_RAW 13
  67. /* Allow locking of shared memory segments */
  68. /* Allow mlock and mlockall (which doesn't really have anything to do
  69. with IPC) */
  70. #define CAP_IPC_LOCK 14
  71. /* Override IPC ownership checks */
  72. #define CAP_IPC_OWNER 15
  73. /* Insert and remove kernel modules - modify kernel without limit */
  74. /* Modify cap_bset */
  75. #define CAP_SYS_MODULE 16
  76. /* Allow ioperm/iopl access */
  77. /* Allow sending USB messages to any device via /proc/bus/usb */
  78. #define CAP_SYS_RAWIO 17
  79. /* Allow use of chroot() */
  80. #define CAP_SYS_CHROOT 18
  81. /* Allow ptrace() of any process */
  82. #define CAP_SYS_PTRACE 19
  83. /* Allow configuration of process accounting */
  84. #define CAP_SYS_PACCT 20
  85. /* Allow configuration of the secure attention key */
  86. /* Allow administration of the random device */
  87. /* Allow examination and configuration of disk quotas */
  88. /* Allow configuring the kernel's syslog (printk behaviour) */
  89. /* Allow setting the domainname */
  90. /* Allow setting the hostname */
  91. /* Allow calling bdflush() */
  92. /* Allow mount() and umount(), setting up new smb connection */
  93. /* Allow some autofs root ioctls */
  94. /* Allow nfsservctl */
  95. /* Allow VM86_REQUEST_IRQ */
  96. /* Allow to read/write pci config on alpha */
  97. /* Allow irix_prctl on mips (setstacksize) */
  98. /* Allow flushing all cache on m68k (sys_cacheflush) */
  99. /* Allow removing semaphores */
  100. /* Used instead of CAP_CHOWN to "chown" IPC message queues, semaphores
  101. and shared memory */
  102. /* Allow locking/unlocking of shared memory segment */
  103. /* Allow turning swap on/off */
  104. /* Allow forged pids on socket credentials passing */
  105. /* Allow setting readahead and flushing buffers on block devices */
  106. /* Allow setting geometry in floppy driver */
  107. /* Allow turning DMA on/off in xd driver */
  108. /* Allow administration of md devices (mostly the above, but some
  109. extra ioctls) */
  110. /* Allow tuning the ide driver */
  111. /* Allow access to the nvram device */
  112. /* Allow administration of apm_bios, serial and bttv (TV) device */
  113. /* Allow manufacturer commands in isdn CAPI support driver */
  114. /* Allow reading non-standardized portions of pci configuration space */
  115. /* Allow DDI debug ioctl on sbpcd driver */
  116. /* Allow setting up serial ports */
  117. /* Allow sending raw qic-117 commands */
  118. /* Allow enabling/disabling tagged queuing on SCSI controllers and sending
  119. arbitrary SCSI commands */
  120. /* Allow setting encryption key on loopback filesystem */
  121. /* Allow setting zone reclaim policy */
  122. #define CAP_SYS_ADMIN 21
  123. /* Allow use of reboot() */
  124. #define CAP_SYS_BOOT 22
  125. /* Allow raising priority and setting priority on other (different
  126. UID) processes */
  127. /* Allow use of FIFO and round-robin (realtime) scheduling on own
  128. processes and setting the scheduling algorithm used by another
  129. process. */
  130. /* Allow setting cpu affinity on other processes */
  131. #define CAP_SYS_NICE 23
  132. /* Override resource limits. Set resource limits. */
  133. /* Override quota limits. */
  134. /* Override reserved space on ext2 filesystem */
  135. /* Modify data journaling mode on ext3 filesystem (uses journaling
  136. resources) */
  137. /* NOTE: ext2 honors fsuid when checking for resource overrides, so
  138. you can override using fsuid too */
  139. /* Override size restrictions on IPC message queues */
  140. /* Allow more than 64hz interrupts from the real-time clock */
  141. /* Override max number of consoles on console allocation */
  142. /* Override max number of keymaps */
  143. #define CAP_SYS_RESOURCE 24
  144. /* Allow manipulation of system clock */
  145. /* Allow irix_stime on mips */
  146. /* Allow setting the real-time clock */
  147. #define CAP_SYS_TIME 25
  148. /* Allow configuration of tty devices */
  149. /* Allow vhangup() of tty */
  150. #define CAP_SYS_TTY_CONFIG 26
  151. /* Allow the privileged aspects of mknod() */
  152. #define CAP_MKNOD 27
  153. /* Allow taking of leases on files */
  154. #define CAP_LEASE 28
  155. #define CAP_AUDIT_WRITE 29
  156. #define CAP_AUDIT_CONTROL 30

检验权限的时候,就检查进程结构体task_struct对应的位是不是1就ok了。

发表评论

表情:
评论列表 (有 0 条评论,418人围观)

还没有评论,来说两句吧...

相关阅读

    相关 查看linux内核源代码

    这里介绍的就是通过自己的虚拟机进去查看你linux系统的内核源代码,当然也可以去网站下载,自己按需使用 1.源码路径 一般路径都是在根目录下的/usr/src下,其

    相关 linux内核capable源代码分析

    linux内核里对于进程的权限管理有一个很重要的函数capable,以前看了好多遍,今天下决心搞定他,也在此立下一个碑,以后有谁想搞明白他的话,我还可以提供一些帮助。 ca

    相关 linux内核read操作源代码分析

    read操作是任何操作系统里的基本操作,我们来看一下在linux内核里,read文件是怎样实现的。 read函数在用户空间是由read系统调用实现的,由编译器编译成软中断i