̽ÌÖSELinuxÊÂÇéÔÀí
ÔÚµ±½ñ»¥ÁªÍøʱ´ú£¬ÍøÂçÇå¾²ÎÊÌâÈÕÒæ͹ÏÔ¡£ÎªÁ˱£»¤ÏµÍ³ÃâÊܶñÒâ¹¥»÷ºÍδ¾ÊÚȨµÄ»á¼û£¬²Ù×÷ϵͳ¶ÔÇå¾²»úÖÆÓÐÁ˸ü¸ßµÄÒªÇó¡£SELinux£¨Security-Enhanced Linux£©×÷ΪLinuxÄں˵ÄÒ»¸öÇ徲ģ¿é£¬ÌṩÁËÇ¿Ê¢µÄÇå¾²Õ½ÂԺͻá¼û¿ØÖÆ»úÖÆ£¬ÎªÏµÍ³ÌṩÁËÌØÁíÍâÇå¾²°ü¹Ü¡£
Ò»¡¢SELinuxµÄÊÂÇéģʽ
SELinux½ÓÄÉÁËÇ¿ÖÆ»á¼û¿ØÖÆ£¨MAC£©»úÖÆ£¬Óë¹Å°åµÄ×ÔÖ÷»á¼û¿ØÖÆ£¨DAC£©ÓÐËùÇø±ð¡£ÔÚDACģʽÏ£¬»á¼û¿ØÖÆÈ¡¾öÓÚ×ÊÔ´µÄËùÓÐÕߣ¬¼´×ÊÔ´µÄ»á¼ûȨÏÞÓÉ×ÊÔ´µÄËùÓÐÕß×ÔÐоöÒé¡£¶øÔÚSELinuxµÄMACģʽÏ£¬ËùÓеÄ×ÊÔ´»á¼û¶¼Êܵ½ÑÏ¿áµÄÇ¿ÖÆÕ½ÂÔ¿ØÖÆ£¬°üÀ¨Àú³Ì¡¢Îļþ¡¢socketµÈ¡£ÕâÒâζ×Å×ÝȻһ¸öÓû§»ñµÃÁËrootȨÏÞ£¬Ò²ÎÞ·¨ÈƹýSELinuxµÄ±£»¤»úÖÆ¡£
ÔÚSELinuxÖУ¬Ã¿¸öÀú³ÌºÍ¹¤¾ß¶¼ÓÐÒ»¸öÓëÖ®¶ÔÓ¦µÄÇå¾²ÉÏÏÂÎÄ¡£Çå¾²ÉÏÏÂÎÄÓÉÖ÷Ìå±êÇ©ºÍ¹¤¾ß±êÇ©×é³É£¬Ö÷Ìå±êÇ©ÌåÏÖÀú³ÌµÄȨÏÞ£¬¹¤¾ß±êÇ©ÌåÏÖ¹¤¾ßµÄȨÏÞ¡£µ±Ò»¸öÇëÇó±»Ìᳫʱ£¬SELinux»áƾ֤Ö÷Ìå±êÇ©ºÍ¹¤¾ß±êÇ©µÄ»á¼û¿ØÖƾØÕóÀ´¾öÒéÊÇ·ñÔÊÐíÕâ¸öÇëÇó¡£
¶þ¡¢Ïêϸ´úÂëʾÀý
½ÓÏÂÀ´£¬ÎÒÃǽ«Í¨¹ýÒ»¸ö¼òÆӵĴúÂëʾÀýÀ´ÑÝʾSELinuxµÄÊÂÇéģʽ¡£ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃǽ«½¨ÉèÒ»¸ö¼òÆÓµÄC³ÌÐò£¬³ÌÐòʵÑé·¿ªÒ»¸öÎļþ²¢Ð´ÈëÄÚÈÝ¡£ÎÒÃǽ«Ê¹ÓÃSELinuxµÄÇå¾²¹æÔòÀ´ÏÞÖƸóÌÐòµÄȨÏÞ¡£
Ê×ÏÈ£¬ÎÒÃÇÐèҪȷ±£ÏµÍ³ÖÐ×°ÖÃÁËSELinux£¬²¢ÇÒÆôÓÃÁËSELinux¡£È»ºó£¬ÎÒÃǽ¨ÉèÒ»¸öÃûΪ”selinux_example.c”µÄÎļþ£¬±àдÒÔÏ´úÂ룺
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> int main() { char *file_path = "/tmp/example.txt"; char *content = "Hello, SELinux!"; int fd = open(file_path, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR); if (fd < 0) { perror("open"); return 1; } if (write(fd, content, sizeof(content)) < 0) { perror("write"); close(fd); return 1; } close(fd); return 0; }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸ö³ÌÐòÖУ¬ÎÒÃÇʵÑé·¿ªÒ»¸öÃûΪ”example.txt”µÄÎļþ²¢Ð´ÈëÄÚÈÝ”Hello, SELinux!”¡£½ÓÏÂÀ´£¬ÎÒÃÇÐèҪΪ¸Ã³ÌÐò½¨ÉèÒ»¸öSELinuxÇå¾²Õ½ÂÔ¡£ÎÒÃÇ¿ÉÒÔʹÓÔaudit2allow”¹¤¾ßÀ´ÌìÉúÒ»¸öÔÝʱSELinuxÕ½ÂÔ£¬È»ºó¼ÓÔØÕâ¸öÕ½ÂÔ¡£Ö´ÐÐÒÔÏÂÏÂÁ
audit2allow -a -M my_selinux_example semodule -i my_selinux_example.pp
µÇ¼ºó¸´ÖÆ
ÌìÉúÕ½ÂÔºó£¬ÎÒÃÇ¿ÉÒÔÔËÐбàÒëºóµÄ³ÌÐò£¬ËüÓ¦¸ÃÄܹ»ÀÖ³ÉдÈëÎļþ¡£È»ºó£¬ÎÒÃÇ¿ÉÒÔͨ¹ýSELinuxµÄÉó¼ÆÈÕÖ¾À´Éó²é»á¼ûȨÏÞµÄ×·×ٺͼͼ¡£Ö´ÐÐÒÔÏÂÏÂÁ
grep 'avc: ' /var/log/audit/audit.log | audit2why
µÇ¼ºó¸´ÖÆ
ͨ¹ýÒÔÉÏʾÀý´úÂëºÍ°ì·¨£¬ÎÒÃÇ¿ÉÒÔ¸üÉîÈëµØÏàʶSELinuxµÄÊÂÇéģʽºÍÔõÑùͨ¹ýÇå¾²Õ½ÂÔÀ´±£»¤ÏµÍ³¡£ SELinuxÌṩÁËÇ¿Ê¢µÄÇå¾²»úÖÆ£¬È·±£ÏµÍ³ÃâÊܶñÒâ¹¥»÷ºÍÀÄÓá£ÈçÐèÉîÈëѧϰSELinux£¬½¨Òé²éÔĸü¶àÏà¹Ø×ÊÁϺÍÎĵµ£¬½øÒ»²½ÏàʶÇå¾²Õ½ÂԵıàдºÍÖÎÀí·½·¨¡£
ÒÔÉϾÍÊÇ̽ÌÖSELinuxÊÂÇéÔÀíµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡