configurationselinux以允许snmpd读取到自定义位置

我有snmpd在/custom/location/myscript.sh下执行脚本。 如果我禁用selinux一切正常。 如果我启用selinux它停止工作,如果我做audit2why我看到以下内容:

type=AVC msg=audit(1371990300.064:23445): avc: denied { read open } for pid=21423 comm="snmpd" name="myscript.sh" dev=dm-0 ino=1046532 scontext=unconfined_u:system_r:snmpd_t:s0 tcontext=unconfined_u:object_r:default_t:s0 tclass=file Was caused by: Missing type enforcement (TE) allow rule. You can use audit2allow to generate a loadable module to allow this access. type=AVC msg=audit(1371990313.066:23446): avc: denied { read open } for pid=21425 comm="snmpd" name="myscript.sh" dev=dm-0 ino=1046532 scontext=unconfined_u:system_r:snmpd_t:s0 tcontext=unconfined_u:object_r:default_t:s0 tclass=file Was caused by: Missing type enforcement (TE) allow rule. You can use audit2allow to generate a loadable module to allow this access. 

如果我做一个LS-Z /自定义/位置我看到以下内容:

 -rwxr-xr-x. root root unconfined_u:object_r:default_t:s0 myscript.sh 

所以我需要在目录上做一个chcon-R。 我试过了:

 chcon -R -u unconfined_u -r system_r -t snmpd_t /custom/location 

只有得到以下错误:

 chcon: failed to change context of `myscript.sh' to `unconfined_u:system_r:snmpd_t:s0': Permission denied 

我也尝试使用audit2allow来创build一个自定义策略,但似乎没有工作。

我真的不想禁用SELinux。 任何人都可以帮助我了解如何让snmpd能够从/定制/位置读取?

感谢Brad

由于输出很长,我截断了一些 – 但这是ausearch的结果:

 ---- time->Sun Jun 23 08:27:13 2013 type=SYSCALL msg=audit(1371990433.731:23453): arch=c000003e syscall=189 success=no exit=-13 a0=11760e0 a1=3e50616319 a2=1178ef0 a3=21 items=0 ppid=20865 pid=21434 auid=0 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=pts0 ses=3863 comm="chcon" exe="/usr/bin/chcon" subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 key= (null) type=AVC msg=audit(1371990433.731:23453): avc: denied { relabelto } for pid=21434 comm="chcon" name="scripts" dev=dm-0 ino=1046530 scontext=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 tcontext=unconfined_u:system_r:snmpd_t:s0 tclass=dir ---- time->Sun Jun 23 08:27:24 2013 type=SYSCALL msg=audit(1371990444.075:23454): arch=c000003e syscall=59 success=no exit=-13 a0=7fdb8933c6d0 a1=7fdb8933c3a0 a2=7fff16ef1790 a3=1f items=0 ppid=18001 pid=21435 auid=0 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) ses=3765 comm="snmpd" exe="/usr/sbin/snmpd" subj=unconfined_u:system_r:snmpd_t:s0 key= (null) type=AVC msg=audit(1371990444.075:23454): avc: denied { read open } for pid=21435 comm="snmpd" name="snmp.sh" dev=dm-0 ino=1046532 scontext=unconfined_u:system_r:snmpd_t:s0 tcontext=unconfined_u:object_r:default_t:s0 tclass=file ---- 

首先, 您可能根本不需要编写自定义模块 。

我将把脚本放在SELinux希望find可执行文件的地方之一,并且只有在不可能的情况下,才能使用audit2allow的输出(而不是audit2why ,它提供了一个友好的人性化的消息,理解真正的问题的条款),我会分析AVC拒绝。

更改文件的安全上下文是一项特权操作,对于尝试执行该命令的用户而言,您并不知情。 使用root

但是请注意,正确的做法是不要chcon该文件,因为这不会在系统重新标记之后保留,而是使用semanage向path中添加安全上下文:

 semanage fcontext -a type_enforcement "/path/expression(/.*)?" 

检查semanage fcontext -l | less的输出semanage fcontext -l | less semanage fcontext -l | less了一个pathexpression式应该如何的第一印象。

为了更深入地理解问题,您需要知道snmpd守护进程允许转换到哪个上下文。 您可以在seinfosesearch手册中find帮助。

可以检查refpolicy的上游源代码,以便了解snmpd_t标记的文件将读取的可执行文件的正确标签。 最好的办法是使用你的软件包pipe理器下载与你的操作系统中使用的策略版本匹配的源策略。

这是理解模块中涉及的不同文件(特别是5b)的function的好资源。

首先检查为这个模块定义的文件上下文(这是你会发现使用semanage fcontext -l | grep snmp ,或多或less):

 # cat refpolicy/policy/modules/contrib/snmp.fc /etc/rc\.d/init\.d/((snmpd)|(snmptrapd)) -- gen_context(system_u:object_r:snmpd_initrc_exec_t,s0) /usr/sbin/snmptrap -- gen_context(system_u:object_r:snmpd_exec_t,s0) /usr/sbin/snmptrapd -- gen_context(system_u:object_r:snmpd_exec_t,s0) /usr/share/snmp/mibs/\.index -- gen_context(system_u:object_r:snmpd_var_lib_t,s0) /var/agentx(/.*)? gen_context(system_u:object_r:snmpd_var_lib_t,s0) /var/net-snmp(/.*) gen_context(system_u:object_r:snmpd_var_lib_t,s0) /var/lib/net-snmp(/.*)? gen_context(system_u:object_r:snmpd_var_lib_t,s0) /var/lib/snmp(/.*)? gen_context(system_u:object_r:snmpd_var_lib_t,s0) /var/log/snmpd\.log.* -- gen_context(system_u:object_r:snmpd_log_t,s0) /var/run/net-snmpd(/.*)? gen_context(system_u:object_r:snmpd_var_run_t,s0) /var/run/snmpd(/.*)? gen_context(system_u:object_r:snmpd_var_run_t,s0) /var/run/snmpd\.pid -- gen_context(system_u:object_r:snmpd_var_run_t,s0) 

然后,这里定义的types执行规则(片段):

 # head -22 refpolicy/policy/modules/contrib/snmp.te policy_module(snmp, 1.14.0) ######################################## # # Declarations # type snmpd_t; type snmpd_exec_t; init_daemon_domain(snmpd_t, snmpd_exec_t) type snmpd_initrc_exec_t; init_script_file(snmpd_initrc_exec_t) type snmpd_log_t; logging_log_file(snmpd_log_t) type snmpd_var_run_t; files_pid_file(snmpd_var_run_t) type snmpd_var_lib_t; files_type(snmpd_var_lib_t) 

最后,接口( snmp.if ),文件。

有了这些信息,您需要找出snmpd能够执行脚本的正确上下文,然后手动将上下文规则添加到可执行文件所在的path中。