policy_module(sysnetwork)

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

## <desc>
##      <p>
##      Determine whether DHCP client
##      can manage samba
##      </p>
## </desc>
gen_tunable(dhcpc_manage_samba, false)

attribute_role dhcpc_roles;
roleattribute system_r dhcpc_roles;

# this is shared between dhcpc and dhcpd:
type dhcp_etc_t;
files_config_file(dhcp_etc_t)

# this is shared between dhcpc and dhcpd:
type dhcp_state_t;
files_type(dhcp_state_t)

type dhcpc_t;
type dhcpc_exec_t;
init_daemon_domain(dhcpc_t, dhcpc_exec_t)
role dhcpc_roles types dhcpc_t;

type dhcpc_runtime_t alias dhcpc_var_run_t;
files_runtime_file(dhcpc_runtime_t)

type dhcpc_state_t;
files_type(dhcpc_state_t)

type dhcpc_tmp_t;
files_tmp_file(dhcpc_tmp_t)

type ifconfig_t;
type ifconfig_exec_t;
init_system_domain(ifconfig_t, ifconfig_exec_t)
role system_r types ifconfig_t;

type ifconfig_runtime_t;
files_runtime_file(ifconfig_runtime_t)

optional_policy(`
	container_mountpoint(ifconfig_runtime_t)
')

type net_conf_t;
files_config_file(net_conf_t)

ifdef(`distro_debian',`
	init_daemon_runtime_file(net_conf_t, dir, "network")
')

########################################
#
# DHCP client local policy
#
allow dhcpc_t self:capability { dac_override fsetid net_admin net_bind_service net_raw setgid setpcap setuid sys_chroot sys_nice sys_resource sys_tty_config };
dontaudit dhcpc_t self:capability { sys_ptrace sys_tty_config };
# for access("/etc/bashrc", X_OK) on Red Hat
dontaudit dhcpc_t self:capability { dac_read_search sys_module };
allow dhcpc_t self:process { getcap getsched ptrace setcap setfscreate setrlimit signal_perms };
allow dhcpc_t self:cap_userns { net_bind_service };

allow dhcpc_t self:fifo_file rw_fifo_file_perms;
allow dhcpc_t self:tcp_socket create_stream_socket_perms;
allow dhcpc_t self:udp_socket create_socket_perms;
allow dhcpc_t self:packet_socket create_socket_perms;
allow dhcpc_t self:netlink_generic_socket create_socket_perms;
allow dhcpc_t self:netlink_kobject_uevent_socket create_socket_perms;
allow dhcpc_t self:netlink_route_socket create_netlink_socket_perms;
allow dhcpc_t self:rawip_socket create_socket_perms;
allow dhcpc_t self:unix_dgram_socket { create_socket_perms sendto };

allow dhcpc_t dhcp_etc_t:dir list_dir_perms;
read_lnk_files_pattern(dhcpc_t, dhcp_etc_t, dhcp_etc_t)
exec_files_pattern(dhcpc_t, dhcp_etc_t, dhcp_etc_t)

allow dhcpc_t dhcp_state_t:file read_file_perms;
manage_files_pattern(dhcpc_t, dhcpc_state_t, dhcpc_state_t)
filetrans_pattern(dhcpc_t, dhcp_state_t, dhcpc_state_t, file)
allow dhcpc_t dhcpc_state_t:file map;

# create pid file
manage_files_pattern(dhcpc_t, dhcpc_runtime_t, dhcpc_runtime_t)
manage_sock_files_pattern(dhcpc_t, dhcpc_runtime_t, dhcpc_runtime_t)
create_dirs_pattern(dhcpc_t, dhcpc_runtime_t, dhcpc_runtime_t)
files_runtime_filetrans(dhcpc_t, dhcpc_runtime_t, { file dir })

# Allow read/write to /etc/resolv.conf and /etc/ntp.conf. Note that any files
# in /etc created by dhcpcd will be labelled net_conf_t.
sysnet_manage_config(dhcpc_t)
files_etc_filetrans(dhcpc_t, net_conf_t, file)

# create temp files
manage_dirs_pattern(dhcpc_t, dhcpc_tmp_t, dhcpc_tmp_t)
manage_files_pattern(dhcpc_t, dhcpc_tmp_t, dhcpc_tmp_t)
files_tmp_filetrans(dhcpc_t, dhcpc_tmp_t, { file dir })

can_exec(dhcpc_t, dhcpc_exec_t)

kernel_read_system_state(dhcpc_t)
kernel_read_network_state(dhcpc_t)
kernel_search_network_sysctl(dhcpc_t)
kernel_read_kernel_sysctls(dhcpc_t)
kernel_request_load_module(dhcpc_t)
kernel_use_fds(dhcpc_t)
kernel_rw_net_sysctls(dhcpc_t)

corecmd_exec_bin(dhcpc_t)
corecmd_exec_shell(dhcpc_t)

corenet_all_recvfrom_netlabel(dhcpc_t)
corenet_tcp_sendrecv_all_if(dhcpc_t)
corenet_raw_sendrecv_all_if(dhcpc_t)
corenet_udp_sendrecv_all_if(dhcpc_t)
corenet_tcp_sendrecv_all_nodes(dhcpc_t)
corenet_raw_sendrecv_all_nodes(dhcpc_t)
corenet_udp_sendrecv_all_nodes(dhcpc_t)
corenet_tcp_bind_all_nodes(dhcpc_t)
corenet_udp_bind_all_nodes(dhcpc_t)
corenet_tcp_bind_dhcpc_port(dhcpc_t)
corenet_udp_bind_dhcpc_port(dhcpc_t)
corenet_udp_bind_all_unreserved_ports(dhcpc_t)
corenet_tcp_connect_all_ports(dhcpc_t)
corenet_sendrecv_dhcpd_client_packets(dhcpc_t)
corenet_sendrecv_all_server_packets(dhcpc_t)
corenet_sendrecv_icmp_packets(dhcpc_t)

dev_read_sysfs(dhcpc_t)
# for SSP:
dev_read_urand(dhcpc_t)

domain_use_interactive_fds(dhcpc_t)
domain_dontaudit_read_all_domains_state(dhcpc_t)

files_read_etc_files(dhcpc_t)
files_read_etc_runtime_files(dhcpc_t)
files_read_usr_files(dhcpc_t)
files_search_home(dhcpc_t)
files_search_var_lib(dhcpc_t)
files_dontaudit_search_locks(dhcpc_t)
files_getattr_generic_locks(dhcpc_t)
files_manage_var_files(dhcpc_t)

fs_getattr_all_fs(dhcpc_t)
fs_getattr_nsfs_files(dhcpc_t)
fs_search_auto_mountpoints(dhcpc_t)
fs_search_cgroup_dirs(dhcpc_t)

term_dontaudit_use_all_ttys(dhcpc_t)
term_dontaudit_use_all_ptys(dhcpc_t)
term_dontaudit_use_unallocated_ttys(dhcpc_t)
term_dontaudit_use_generic_ptys(dhcpc_t)

init_rw_utmp(dhcpc_t)

logging_send_syslog_msg(dhcpc_t)

miscfiles_read_localization(dhcpc_t)
# dhclient reads /etc/ssl
miscfiles_read_generic_certs(dhcpc_t)

sysnet_run_ifconfig(dhcpc_t, dhcpc_roles)

userdom_use_user_terminals(dhcpc_t)
userdom_dontaudit_search_user_home_dirs(dhcpc_t)

ifdef(`distro_redhat', `
	files_exec_etc_files(dhcpc_t)
')

ifdef(`distro_ubuntu',`
	optional_policy(`
		unconfined_domain(dhcpc_t)
	')
')

ifdef(`init_systemd',`
	init_rw_stream_sockets(dhcpc_t)
	init_read_state(dhcpc_t)
	init_stream_connect(dhcpc_t)
	init_get_all_units_status(dhcpc_t)
	init_search_units(dhcpc_t)

	optional_policy(`
		systemd_dbus_chat_resolved(dhcpc_t)
	')
')

optional_policy(`
	avahi_domtrans(dhcpc_t)
')

optional_policy(`
	consoletype_run(dhcpc_t, dhcpc_roles)
')

optional_policy(`
	dbus_system_domain(dhcpc_t, dhcpc_exec_t)

	init_dbus_chat_script(dhcpc_t)

	optional_policy(`
		networkmanager_dbus_chat(dhcpc_t)
	')
')

optional_policy(`
	hostname_run(dhcpc_t, dhcpc_roles)
')

optional_policy(`
	modutils_run(dhcpc_t, dhcpc_roles)
')

# for the dhcp client to run ping to check IP addresses
optional_policy(`
	netutils_run_ping(dhcpc_t, dhcpc_roles)
	netutils_run(dhcpc_t, dhcpc_roles)
')

optional_policy(`
	networkmanager_domtrans(dhcpc_t)
	networkmanager_read_runtime_files(dhcpc_t)
	networkmanager_manage_lib_files(dhcpc_t)
	networkmanager_stream_connect(dhcpc_t)
')

optional_policy(`
	nis_read_ypbind_runtime_files(dhcpc_t)
')

optional_policy(`
	nscd_initrc_domtrans(dhcpc_t)
	nscd_domtrans(dhcpc_t)
	nscd_read_runtime_files(dhcpc_t)
')

optional_policy(`
	ntp_initrc_domtrans(dhcpc_t)
	ntp_read_drift_files(dhcpc_t)
	ntp_read_conf_files(dhcpc_t)
')

optional_policy(`
	tunable_policy(`dhcpc_manage_samba',`
		samba_manage_config(dhcpc_t)
		samba_manage_var_files(dhcpc_t)
		init_exec_script_files(dhcpc_t)
		init_get_system_status(dhcpc_t)
		samba_stop(dhcpc_t)
		samba_start(dhcpc_t)
		samba_reload(dhcpc_t)
		samba_status(dhcpc_t)
	')
')

optional_policy(`
	seutil_sigchld_newrole(dhcpc_t)
	seutil_dontaudit_search_config(dhcpc_t)
')

optional_policy(`
	udev_read_runtime_files(dhcpc_t)
')

optional_policy(`
	userdom_use_all_users_fds(dhcpc_t)
')

optional_policy(`
	vmware_append_log(dhcpc_t)
')

optional_policy(`
	kernel_read_xen_state(dhcpc_t)
	kernel_write_xen_state(dhcpc_t)
	xen_append_log(dhcpc_t)
')

########################################
#
# Ifconfig local policy
#

allow ifconfig_t self:capability { net_admin net_raw sys_admin sys_tty_config };
dontaudit ifconfig_t self:capability sys_module;
allow ifconfig_t self:process { dyntransition getattr getcap getpgid getrlimit getsched getsession noatsecure rlimitinh setcap setkeycreate setpgid setsched setsockcreate share siginh signal_perms transition };
allow ifconfig_t self:fd use;
allow ifconfig_t self:fifo_file rw_fifo_file_perms;
allow ifconfig_t self:sock_file read_sock_file_perms;
allow ifconfig_t self:socket create_socket_perms;
allow ifconfig_t self:unix_dgram_socket create_socket_perms;
allow ifconfig_t self:unix_stream_socket create_stream_socket_perms;
allow ifconfig_t self:unix_dgram_socket sendto;
allow ifconfig_t self:unix_stream_socket connectto;
allow ifconfig_t self:shm create_shm_perms;
allow ifconfig_t self:sem create_sem_perms;
allow ifconfig_t self:msgq create_msgq_perms;
allow ifconfig_t self:msg { receive send };
# Create UDP sockets, necessary when called from dhcpc
allow ifconfig_t self:udp_socket create_socket_perms;
# for /sbin/ip
allow ifconfig_t self:packet_socket create_socket_perms;
# generic netlink socket for iw
# socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_GENERIC) = 3
allow ifconfig_t self:netlink_socket create_socket_perms;
allow ifconfig_t self:netlink_generic_socket create_socket_perms;
allow ifconfig_t self:netlink_route_socket create_netlink_socket_perms;
allow ifconfig_t self:netlink_xfrm_socket create_netlink_socket_perms;
allow ifconfig_t self:tcp_socket { create ioctl };

# Allow "ip netns" to remount /var/run/netns and to mount network namespace files on /var/run/netns/$NSNAME
allow ifconfig_t ifconfig_runtime_t:dir mounton;
allow ifconfig_t ifconfig_runtime_t:file mounton;
manage_files_pattern(ifconfig_t, ifconfig_runtime_t, ifconfig_runtime_t)
create_dirs_pattern(ifconfig_t, ifconfig_runtime_t, ifconfig_runtime_t)
files_runtime_filetrans(ifconfig_t, ifconfig_runtime_t, dir)

kernel_use_fds(ifconfig_t)
kernel_read_system_state(ifconfig_t)
kernel_read_network_state(ifconfig_t)
kernel_request_load_module(ifconfig_t)
kernel_search_network_sysctl(ifconfig_t)
kernel_rw_net_sysctls(ifconfig_t)
kernel_search_debugfs(ifconfig_t)

corenet_rw_tun_tap_dev(ifconfig_t)

dev_read_sysfs(ifconfig_t)
dev_dontaudit_rw_cardmgr(ifconfig_t)
# for IPSEC setup:
dev_read_urand(ifconfig_t)

domain_use_interactive_fds(ifconfig_t)

files_read_etc_files(ifconfig_t)
files_read_etc_runtime_files(ifconfig_t)
files_read_usr_files(ifconfig_t)

fs_getattr_xattr_fs(ifconfig_t)
fs_read_nsfs_files(ifconfig_t)
fs_search_auto_mountpoints(ifconfig_t)
fs_unmount_nsfs(ifconfig_t)

selinux_dontaudit_getattr_fs(ifconfig_t)

term_dontaudit_use_console(ifconfig_t)
term_dontaudit_use_all_ttys(ifconfig_t)
term_dontaudit_use_all_ptys(ifconfig_t)
term_dontaudit_use_ptmx(ifconfig_t)
term_dontaudit_use_generic_ptys(ifconfig_t)

files_dontaudit_read_root_files(ifconfig_t)

init_use_fds(ifconfig_t)
init_use_script_ptys(ifconfig_t)

logging_send_syslog_msg(ifconfig_t)

miscfiles_read_localization(ifconfig_t)

seutil_use_runinit_fds(ifconfig_t)

sysnet_dontaudit_rw_dhcpc_udp_sockets(ifconfig_t)

# For "ip netns identify $$"
userdom_read_all_users_state(ifconfig_t)
userdom_use_user_terminals(ifconfig_t)
userdom_use_all_users_fds(ifconfig_t)

ifdef(`distro_debian',`
	term_dontaudit_use_unallocated_ttys(ifconfig_t)
')

ifdef(`distro_ubuntu',`
	optional_policy(`
		unconfined_domain(ifconfig_t)
	')
')

optional_policy(`
	devicekit_read_runtime_files(ifconfig_t)
	devicekit_append_inherited_log_files(ifconfig_t)
')

optional_policy(`
	ipsec_write_runtime_files(ifconfig_t)
	ipsec_setcontext_default_spd(ifconfig_t)
')

optional_policy(`
	modutils_domtrans(ifconfig_t)
')

optional_policy(`
	nis_use_ypbind(ifconfig_t)
')

optional_policy(`
	ppp_use_fds(ifconfig_t)
')

optional_policy(`
        udev_dontaudit_rw_dgram_sockets(ifconfig_t)
')

optional_policy(`
	unconfined_dontaudit_rw_pipes(ifconfig_t)
')

optional_policy(`
	vmware_append_log(ifconfig_t)
')

optional_policy(`
	kernel_read_xen_state(ifconfig_t)
	kernel_write_xen_state(ifconfig_t)
	xen_append_log(ifconfig_t)
')
