policy_module(rhcs, 1.2.1)

########################################
#
# Declarations
#

## <desc>
##	<p>
##	Determine whether fenced can
##	connect to the TCP network.
##	</p>
## </desc>
gen_tunable(fenced_can_network_connect, false)

## <desc>
##	<p>
##	Determine whether fenced can use ssh.
##	</p>
## </desc>
gen_tunable(fenced_can_ssh, false)

## <desc>
## <p>
## Allow cluster administrative domains to connect to the network using TCP.
## </p>
## </desc>
gen_tunable(cluster_can_network_connect, false)

## <desc>
## <p>
## Allow cluster administrative domains to manage all files on a system.
## </p>
## </desc>
gen_tunable(cluster_manage_all_files, false)

## <desc>
## <p>
## Allow cluster administrative cluster domains memcheck-amd64- to use executable memory
## </p>
## </desc>
gen_tunable(cluster_use_execmem, false)

## <desc>
##	<p>
##	Determine whether haproxy can
##	connect to all TCP ports.
##	</p>
## </desc>
gen_tunable(haproxy_connect_any, false)

attribute cluster_domain;
attribute cluster_log;
attribute cluster_pid;
attribute cluster_tmpfs;

rhcs_domain_template(dlm_controld)

type dlm_controld_initrc_exec_t;
init_script_file(dlm_controld_initrc_exec_t)

rhcs_domain_template(fenced)

type fenced_lock_t;
files_lock_file(fenced_lock_t)

type fenced_tmp_t;
files_tmp_file(fenced_tmp_t)

rhcs_domain_template(foghorn)

type foghorn_initrc_exec_t;
init_script_file(foghorn_initrc_exec_t)

rhcs_domain_template(gfs_controld)
rhcs_domain_template(haproxy)

type haproxy_var_lib_t;
files_type(haproxy_var_lib_t)

type haproxy_unit_file_t;
systemd_unit_file(haproxy_unit_file_t)

rhcs_domain_template(groupd)
rhcs_domain_template(qdiskd)

type qdiskd_var_lib_t;
files_type(qdiskd_var_lib_t)

# cluster_t is a new domain for administrative generic cluster services 
# (rgmanager, corosync, hearbeat, cman, pacemaker)
rhcs_domain_template(cluster)

typealias cluster_t alias { aisexec_t corosync_t pacemaker_t rgmanager_t };
typealias cluster_exec_t alias { aisexec_exec_t corosync_exec_t pacemaker_exec_t rgmanager_exec_t };
typealias cluster_tmpfs_t alias { aisexec_tmpfs_t corosync_tmpfs_t pacemaker_tmpfs_t rgmanager_tmpfs_t };
typealias cluster_var_log_t alias { aisexec_var_log_t corosync_var_log_t rgmanager_var_log_t };
typealias cluster_var_run_t alias { aisexec_var_run_t corosync_var_run_t pacemaker_var_run_t rgmanager_var_run_t };

type cluster_initrc_exec_t;
typealias cluster_initrc_exec_t alias { aisexec_initrc_exec_t corosync_initrc_exec_t pacemaker_initrc_exec_t rgmanager_initrc_exec_t };
init_script_file(cluster_initrc_exec_t)

type cluster_tmp_t;
typealias cluster_tmp_t alias { aisexec_tmp_t corosync_tmp_t pacemaker_tmp_t rgmanager_tmp_t };
files_tmp_file(cluster_tmp_t)

type cluster_var_lib_t;
typealias cluster_var_lib_t alias { aisexec_var_lib_t corosync_var_lib_t pacemaker_var_lib_t rgmanager_var_lib_t };
files_type(cluster_var_lib_t)

type cluster_unit_file_t;
typealias cluster_unit_file_t alias { corosync_unit_file_t pacemaker_unit_file_t };
systemd_unit_file(cluster_unit_file_t)

#####################################
#
# Common cluster domains local policy
#

allow cluster_domain self:capability sys_nice;
allow cluster_domain self:process { signal setsched };
allow cluster_domain self:sem create_sem_perms;
allow cluster_domain self:fifo_file rw_fifo_file_perms;
allow cluster_domain self:unix_stream_socket create_stream_socket_perms;
allow cluster_domain self:unix_dgram_socket create_socket_perms;

manage_dirs_pattern(cluster_domain, cluster_log, cluster_log)
manage_files_pattern(cluster_domain, cluster_log, cluster_log)
manage_sock_files_pattern(cluster_domain, cluster_log, cluster_log)

tunable_policy(`cluster_use_execmem',`
    allow cluster_domain self:process execmem;
')

optional_policy(`
	ccs_stream_connect(cluster_domain)
')

optional_policy(`
	dbus_system_bus_client(cluster_domain)
')

#####################################
#
# cluster domain local policy
#

allow cluster_t self:capability { dac_read_search  fowner setuid setgid sys_nice sys_admin sys_resource ipc_lock ipc_owner };
# for hearbeat
allow cluster_t self:capability { net_raw chown };
allow cluster_t self:capability2 block_suspend;
allow cluster_t self:process { setpgid setrlimit setsched signull };

allow cluster_t self:tcp_socket create_stream_socket_perms;
allow cluster_t self:shm create_shm_perms;

manage_dirs_pattern(cluster_t, cluster_tmp_t, cluster_tmp_t)
manage_files_pattern(cluster_t, cluster_tmp_t, cluster_tmp_t)
files_tmp_filetrans(cluster_t, cluster_tmp_t, { file dir })

can_exec(cluster_t, cluster_var_lib_t)
manage_files_pattern(cluster_t, cluster_var_lib_t,cluster_var_lib_t)
manage_dirs_pattern(cluster_t, cluster_var_lib_t,cluster_var_lib_t)
manage_sock_files_pattern(cluster_t, cluster_var_lib_t,cluster_var_lib_t)
manage_fifo_files_pattern(cluster_t, cluster_var_lib_t,cluster_var_lib_t)
files_var_lib_filetrans(cluster_t,cluster_var_lib_t, { file dir fifo_file sock_file })

can_exec(cluster_t, cluster_exec_t)

kernel_kill(cluster_t)
kernel_read_all_sysctls(cluster_t)
kernel_rw_rpc_sysctls(cluster_t)
kernel_search_debugfs(cluster_t)
kernel_search_network_state(cluster_t)

corecmd_exec_bin(cluster_t)
corecmd_exec_shell(cluster_t)

corenet_all_recvfrom_unlabeled(cluster_t)
corenet_all_recvfrom_netlabel(cluster_t)
corenet_udp_sendrecv_generic_if(cluster_t)
corenet_udp_sendrecv_generic_node(cluster_t)
corenet_udp_bind_generic_node(cluster_t)

corenet_sendrecv_netsupport_server_packets(cluster_t)
corenet_udp_bind_netsupport_port(cluster_t)
corenet_udp_sendrecv_netsupport_port(cluster_t)

corenet_sendrecv_cluster_server_packets(cluster_t)
corenet_udp_bind_cluster_port(cluster_t)
corenet_udp_sendrecv_cluster_port(cluster_t)

# need to write to /dev/misc/dlm-contro
dev_rw_dlm_control(cluster_t)
dev_setattr_dlm_control(cluster_t)
dev_read_sysfs(cluster_t)
dev_read_rand(cluster_t)
dev_read_urand(cluster_t)

domain_read_all_domains_state(cluster_t)

fs_getattr_xattr_fs(cluster_t)
fs_getattr_all_fs(cluster_t)

storage_raw_read_fixed_disk(cluster_t)

term_getattr_pty_fs(cluster_t)

files_manage_mounttab(cluster_t)
# needed by resources scripts
files_read_non_security_files(cluster_t)
auth_dontaudit_getattr_shadow(cluster_t)

init_domtrans_script(cluster_t)
init_initrc_domain(cluster_t)
init_read_script_state(cluster_t)
init_rw_script_tmp_files(cluster_t)
init_manage_script_status_files(cluster_t)
init_dbus_chat(cluster_t)

systemd_dbus_chat_logind(cluster_t)

userdom_delete_user_tmp_files(cluster_t)
userdom_rw_user_tmp_files(cluster_t)
userdom_kill_all_users(cluster_t)

tunable_policy(`cluster_can_network_connect',`
    corenet_tcp_connect_all_ports(cluster_t)
')

# we need to have dirs created with var_run_t in /run/cluster
files_create_var_run_dirs(cluster_t)

tunable_policy(`cluster_manage_all_files',`
	files_getattr_all_symlinks(cluster_t)
	files_list_all(cluster_t)
	files_manage_mnt_dirs(cluster_t)
	files_manage_mnt_files(cluster_t)
	files_manage_mnt_symlinks(cluster_t)
	files_manage_isid_type_files(cluster_t)
	files_manage_isid_type_dirs(cluster_t)
	fs_manage_tmpfs_files(cluster_t)
')

optional_policy(`
    ccs_read_config(cluster_t)
')

optional_policy(`
    cmirrord_rw_shm(cluster_t)
')

optional_policy(`
    consoletype_exec(cluster_t)
')

optional_policy(`
    lvm_domtrans(cluster_t)
	lvm_rw_clvmd_tmpfs_files(cluster_t)
    lvm_delete_clvmd_tmpfs_files(cluster_t)
')

optional_policy(`
    fstools_domtrans(cluster_t)
')

optional_policy(`
    rpc_dbus_chat_nfsd(cluster_t)
')

optional_policy(`
    hostname_exec(cluster_t)
')

optional_policy(`
    ccs_manage_config(cluster_t)
    ccs_stream_connect(cluster_t)
')

optional_policy(`
    fprintd_dbus_chat(cluster_t)
')

optional_policy(`
    ldap_systemctl(cluster_t)
')

optional_policy(`
    mount_domtrans(cluster_t)
')

optional_policy(`
    mysql_filetrans_named_content(cluster_t)
    mysql_domtrans_mysql_safe(cluster_t)
    mysql_stream_connect(cluster_t)
')

optional_policy(`
    netutils_domtrans(cluster_t)
    netutils_domtrans_ping(cluster_t)
')

optional_policy(`
    postgresql_signal(cluster_t)
')

optional_policy(`
	rhcs_getattr_fenced(cluster_t)
	rhcs_rw_cluster_shm(cluster_t)
    rhcs_rw_cluster_semaphores(cluster_t)
    rhcs_stream_connect_cluster(cluster_t)
    rhcs_relabel_cluster_lib_files(cluster_t)
')

optional_policy(`
    rdisc_exec(cluster_t)
')

optional_policy(`
    ricci_dontaudit_rw_modcluster_pipes(cluster_t)
')

optional_policy(`
    rhcs_named_filetrans_log_dir(cluster_t)
')

optional_policy(`
    rpc_systemctl_nfsd(cluster_t)
    rpc_systemctl_rpcd(cluster_t)

    rpc_domtrans_nfsd(cluster_t)
    rpc_domtrans_rpcd(cluster_t)
    rpc_manage_nfs_state_data(cluster_t)
	rpc_filetrans_var_lib_nfs_content(cluster_t)
')

optional_policy(`
    samba_manage_var_files(cluster_t)
    samba_rw_config(cluster_t)
    samba_signal_smbd(cluster_t)
    samba_signal_nmbd(cluster_t)
')

optional_policy(`
    sysnet_domtrans_ifconfig(cluster_t)
')

optional_policy(`
    udev_read_db(cluster_t)
')

optional_policy(`
    virt_stream_connect(cluster_t)
')

optional_policy(`
    unconfined_domain(cluster_t)
')

optional_policy(`
    wdmd_rw_tmpfs(cluster_t)
')

optional_policy(`
    xen_domtrans_xm(cluster_t)
')

#####################################
#
# dlm_controld local policy
#

allow dlm_controld_t self:capability { dac_read_search net_admin sys_admin setgid sys_resource };
allow dlm_controld_t self:netlink_kobject_uevent_socket create_socket_perms;

files_pid_filetrans(dlm_controld_t, dlm_controld_var_run_t, dir)

stream_connect_pattern(dlm_controld_t, fenced_var_run_t, fenced_var_run_t, fenced_t)
stream_connect_pattern(dlm_controld_t, groupd_var_run_t, groupd_var_run_t, groupd_t)

kernel_rw_net_sysctls(dlm_controld_t)

corecmd_exec_bin(dlm_controld_t)

dev_read_rand(dlm_controld_t)
dev_rw_dlm_control(dlm_controld_t)
dev_rw_sysfs(dlm_controld_t)

fs_manage_configfs_files(dlm_controld_t)
fs_manage_configfs_dirs(dlm_controld_t)

init_rw_script_tmp_files(dlm_controld_t)

logging_send_syslog_msg(dlm_controld_t)

optional_policy(`
    lvm_domtrans(dlm_controld_t)
')

optional_policy(`
	rhcs_rw_cluster_tmpfs(dlm_controld_t)
')

optional_policy(`
    rhcs_stream_connect_cluster(dlm_controld_t)
')

#######################################
#
# fenced local policy
#

allow fenced_t self:capability { net_admin sys_boot sys_rawio sys_resource sys_admin };
allow fenced_t self:process { getsched setcap setpgid signal_perms };

allow fenced_t self:tcp_socket create_stream_socket_perms;
allow fenced_t self:udp_socket create_socket_perms;
allow fenced_t self:unix_stream_socket connectto;

can_exec(fenced_t, fenced_exec_t)

manage_files_pattern(fenced_t, fenced_lock_t, fenced_lock_t)
files_lock_filetrans(fenced_t, fenced_lock_t, file)

manage_dirs_pattern(fenced_t, fenced_tmp_t, fenced_tmp_t)
manage_files_pattern(fenced_t, fenced_tmp_t, fenced_tmp_t)
manage_fifo_files_pattern(fenced_t, fenced_tmp_t, fenced_tmp_t)
files_tmp_filetrans(fenced_t, fenced_tmp_t, { file fifo_file dir })

stream_connect_pattern(fenced_t, groupd_var_run_t, groupd_var_run_t, groupd_t)

kernel_read_network_state(fenced_t)
kernel_read_fs_sysctls(fenced_t)

corecmd_exec_bin(fenced_t)
corecmd_exec_shell(fenced_t)

corenet_all_recvfrom_unlabeled(fenced_t)
corenet_all_recvfrom_netlabel(fenced_t)
corenet_tcp_sendrecv_generic_if(fenced_t)
corenet_udp_sendrecv_generic_if(fenced_t)
corenet_tcp_sendrecv_generic_node(fenced_t)
corenet_udp_sendrecv_generic_node(fenced_t)
corenet_tcp_bind_generic_node(fenced_t)
corenet_udp_bind_generic_node(fenced_t)

corenet_sendrecv_ionixnetmon_server_packets(fenced_t)
corenet_udp_bind_ionixnetmon_port(fenced_t)
corenet_udp_sendrecv_ionixnetmon_port(fenced_t)

corenet_sendrecv_zented_server_packets(fenced_t)
corenet_tcp_bind_zented_port(fenced_t)
corenet_udp_bind_zented_port(fenced_t)
corenet_tcp_connect_zented_port(fenced_t)
corenet_tcp_sendrecv_zented_port(fenced_t)

corenet_sendrecv_http_client_packets(fenced_t)
corenet_tcp_connect_http_port(fenced_t)
corenet_tcp_sendrecv_http_port(fenced_t)

dev_read_sysfs(fenced_t)
dev_read_urand(fenced_t)
dev_read_rand(fenced_t)
dev_rw_lvm_control(fenced_t)

storage_raw_read_fixed_disk(fenced_t)
storage_raw_write_fixed_disk(fenced_t)
storage_raw_read_removable_device(fenced_t)

term_getattr_pty_fs(fenced_t)
term_use_generic_ptys(fenced_t)
term_use_ptmx(fenced_t)

logging_send_syslog_msg(fenced_t)

tunable_policy(`fenced_can_network_connect',`
	corenet_sendrecv_all_client_packets(fenced_t)
	corenet_tcp_connect_all_ports(fenced_t)
	corenet_tcp_sendrecv_all_ports(fenced_t)
')

optional_policy(`
	tunable_policy(`fenced_can_ssh',`
		allow fenced_t self:capability { setuid setgid };

		corenet_sendrecv_ssh_client_packets(fenced_t)
		corenet_tcp_connect_ssh_port(fenced_t)
		corenet_tcp_sendrecv_ssh_port(fenced_t)

		ssh_exec(fenced_t)
		ssh_read_user_home_files(fenced_t)
	')
')

optional_policy(`
    rhcs_exec_cluster(fenced_t)
    rhcs_rw_cluster_tmpfs(fenced_t)
')

optional_policy(`
	ccs_read_config(fenced_t)
')

optional_policy(`
    libs_exec_ldconfig(fenced_t)
')

optional_policy(`
	lvm_domtrans(fenced_t)
	lvm_read_config(fenced_t)
    lvm_stream_connect(fenced_t)
')

optional_policy(`
    sanlock_domtrans(fenced_t)
')

optional_policy(`
	snmp_manage_var_lib_files(fenced_t)
	snmp_manage_var_lib_dirs(fenced_t)
')

optional_policy(`
	virt_domtrans(fenced_t)
	virt_read_config(fenced_t)
	virt_read_pid_files(fenced_t)
	virt_stream_connect(fenced_t)
')

optional_policy(`
	watchdog_unconfined_exec_read_lnk_files(fenced_t)
')

optional_policy(`
	gnome_dontaudit_search_config(fenced_t)
')

#######################################
#
# foghorn local policy
#

allow foghorn_t self:process signal;
allow foghorn_t self:tcp_socket create_stream_socket_perms;
allow foghorn_t self:udp_socket create_socket_perms;

corenet_all_recvfrom_unlabeled(foghorn_t)
corenet_all_recvfrom_netlabel(foghorn_t)
corenet_tcp_sendrecv_generic_if(foghorn_t)
corenet_tcp_sendrecv_generic_node(foghorn_t)

corenet_sendrecv_agentx_client_packets(foghorn_t)
corenet_tcp_connect_agentx_port(foghorn_t)
corenet_tcp_sendrecv_agentx_port(foghorn_t)

corenet_tcp_connect_snmp_port(foghorn_t)

dev_read_urand(foghorn_t)

logging_send_syslog_msg(foghorn_t)

optional_policy(`
	dbus_connect_system_bus(foghorn_t)

    optional_policy(`
        rhcs_dbus_chat_fenced(foghorn_t)
    ')
')

optional_policy(`
    snmp_manage_var_lib_files(foghorn_t)
	snmp_stream_connect(foghorn_t)
')

######################################
#
# gfs_controld local policy
#

allow gfs_controld_t self:capability { net_admin sys_resource };
allow gfs_controld_t self:shm create_shm_perms;
allow gfs_controld_t self:netlink_kobject_uevent_socket create_socket_perms;

stream_connect_pattern(gfs_controld_t, dlm_controld_var_run_t, dlm_controld_var_run_t, dlm_controld_t)
stream_connect_pattern(gfs_controld_t, fenced_var_run_t, fenced_var_run_t, fenced_t)
stream_connect_pattern(gfs_controld_t, groupd_var_run_t, groupd_var_run_t, groupd_t)


dev_rw_dlm_control(gfs_controld_t)
dev_setattr_dlm_control(gfs_controld_t)
dev_rw_sysfs(gfs_controld_t)
storage_getattr_fixed_disk_dev(gfs_controld_t)

fs_getattr_all_fs(gfs_controld_t)

storage_getattr_removable_dev(gfs_controld_t)

init_rw_script_tmp_files(gfs_controld_t)

logging_send_syslog_msg(gfs_controld_t)

optional_policy(`
	lvm_exec(gfs_controld_t)
	dev_rw_lvm_control(gfs_controld_t)
')

#######################################
#
# groupd local policy
#

allow groupd_t self:capability { sys_nice sys_resource };
allow groupd_t self:process setsched;
allow groupd_t self:shm create_shm_perms;

domtrans_pattern(groupd_t, fenced_exec_t, fenced_t)

dev_list_sysfs(groupd_t)

init_rw_script_tmp_files(groupd_t)

logging_send_syslog_msg(groupd_t)

########################################
#
# haproxy local policy
#

# bug in haproxy and process vs pid owner
allow haproxy_t self:capability { dac_read_search  kill };

allow haproxy_t self:capability { chown fowner setgid setuid sys_chroot sys_resource net_admin net_raw };
allow haproxy_t self:capability2 block_suspend;
allow haproxy_t self:process { fork setrlimit signal_perms };
allow haproxy_t self:fifo_file rw_fifo_file_perms;
allow haproxy_t self:unix_stream_socket { create_stream_socket_perms connectto };
allow haproxy_t self:tcp_socket create_stream_socket_perms;
allow haproxy_t self: udp_socket create_socket_perms;

manage_dirs_pattern(haproxy_t, haproxy_var_lib_t, haproxy_var_lib_t)
manage_files_pattern(haproxy_t, haproxy_var_lib_t, haproxy_var_lib_t)
manage_lnk_files_pattern(haproxy_t, haproxy_var_lib_t, haproxy_var_lib_t)
manage_sock_files_pattern(haproxy_t, haproxy_var_lib_t, haproxy_var_lib_t)
files_var_lib_filetrans(haproxy_t, haproxy_var_lib_t, { dir file lnk_file })

kernel_read_network_state(haproxy_t)

can_exec(haproxy_t, haproxy_exec_t)

corenet_sendrecv_unlabeled_packets(haproxy_t)

corenet_tcp_connect_commplex_link_port(haproxy_t)
corenet_tcp_connect_commplex_main_port(haproxy_t)
corenet_tcp_bind_commplex_main_port(haproxy_t)
corenet_tcp_bind_http_port(haproxy_t)
corenet_tcp_bind_http_cache_port(haproxy_t)

corenet_tcp_connect_fmpro_internal_port(haproxy_t)
corenet_tcp_connect_http_port(haproxy_t)
corenet_tcp_connect_http_cache_port(haproxy_t)
corenet_tcp_connect_rtp_media_port(haproxy_t)

dev_list_sysfs(haproxy_t)
dev_read_rand(haproxy_t)
dev_read_urand(haproxy_t)

sysnet_dns_name_resolve(haproxy_t)

tunable_policy(`haproxy_connect_any',`
	corenet_tcp_connect_all_ports(haproxy_t)
	corenet_tcp_bind_all_ports(haproxy_t)
	corenet_sendrecv_all_packets(haproxy_t)
	corenet_tcp_sendrecv_all_ports(haproxy_t)
')

######################################
#
# qdiskd local policy
#

allow qdiskd_t self:capability { ipc_lock sys_boot };
allow qdiskd_t self:tcp_socket { accept listen };

manage_files_pattern(qdiskd_t, qdiskd_var_lib_t, qdiskd_var_lib_t)
manage_dirs_pattern(qdiskd_t, qdiskd_var_lib_t, qdiskd_var_lib_t)
manage_sock_files_pattern(qdiskd_t, qdiskd_var_lib_t, qdiskd_var_lib_t)
files_var_lib_filetrans(qdiskd_t, qdiskd_var_lib_t, { file dir sock_file })

kernel_read_software_raid_state(qdiskd_t)
kernel_getattr_core_if(qdiskd_t)

corecmd_exec_bin(qdiskd_t)
corecmd_exec_shell(qdiskd_t)

dev_read_sysfs(qdiskd_t)
dev_list_all_dev_nodes(qdiskd_t)
dev_getattr_all_blk_files(qdiskd_t)
dev_getattr_all_chr_files(qdiskd_t)
dev_manage_generic_blk_files(qdiskd_t)
dev_manage_generic_chr_files(qdiskd_t)

domain_dontaudit_getattr_all_pipes(qdiskd_t)
domain_dontaudit_getattr_all_sockets(qdiskd_t)

files_dontaudit_getattr_all_sockets(qdiskd_t)
files_dontaudit_getattr_all_pipes(qdiskd_t)

fs_list_hugetlbfs(qdiskd_t)

storage_raw_read_removable_device(qdiskd_t)
storage_raw_write_removable_device(qdiskd_t)
storage_raw_read_fixed_disk(qdiskd_t)
storage_raw_write_fixed_disk(qdiskd_t)

auth_use_nsswitch(qdiskd_t)

logging_send_syslog_msg(qdiskd_t)

optional_policy(`
	netutils_domtrans_ping(qdiskd_t)
')

optional_policy(`
	udev_read_db(qdiskd_t)
')
