policy_module(udev)

########################################
#
# Declarations
#
attribute_role udevadm_roles;

type udev_t;
type udev_exec_t alias udevadm_exec_t;
type udev_helper_exec_t;
kernel_domtrans_to(udev_t, udev_exec_t)
domain_obj_id_change_exemption(udev_t)
domain_entry_file(udev_t, udev_helper_exec_t)
domain_interactive_fd(udev_t)
init_daemon_domain(udev_t, udev_exec_t)
init_named_socket_activation(udev_t, udev_runtime_t)

type udevadm_t;
application_domain(udevadm_t, udev_exec_t)
role udevadm_roles types udevadm_t;

type udev_etc_t alias etc_udev_t;
files_config_file(udev_etc_t)

type udev_rules_t;
files_type(udev_rules_t)

type udev_runtime_t alias { udev_tbl_t udev_var_run_t };
files_runtime_file(udev_runtime_t)
init_daemon_runtime_file(udev_runtime_t, dir, "udev")
optional_policy(`
	kubernetes_mountpoint(udev_runtime_t)
')

########################################
#
# udev Local policy
#

allow udev_t self:capability { chown dac_override dac_read_search fowner fsetid mknod net_admin net_raw setgid setuid sys_admin sys_nice sys_ptrace sys_rawio sys_resource };
allow udev_t self:capability2 { block_suspend wake_alarm };
allow udev_t self:process { dyntransition execmem getattr getcap getpgid getrlimit getsched getsession noatsecure ptrace rlimitinh setcap setfscreate setkeycreate setpgid setrlimit setsched setsockcreate share siginh signal_perms transition };
allow udev_t self:fd use;
allow udev_t self:fifo_file rw_fifo_file_perms;
allow udev_t self:sock_file read_sock_file_perms;
allow udev_t self:shm create_shm_perms;
allow udev_t self:sem create_sem_perms;
allow udev_t self:msgq create_msgq_perms;
allow udev_t self:msg { receive send };
allow udev_t self:unix_stream_socket { accept listen };
allow udev_t self:unix_dgram_socket sendto;
allow udev_t self:unix_stream_socket connectto;
allow udev_t self:netlink_kobject_uevent_socket create_socket_perms;
allow udev_t self:netlink_generic_socket create_socket_perms;
allow udev_t self:rawip_socket create_socket_perms;
# rdma_rename
allow udev_t self:netlink_rdma_socket create_socket_perms;

ifdef(`init_systemd',`
	# systemd-vconsole-setup will be called by udev during virtual terminal initialization
	allow udev_t self:capability sys_tty_config;
',`
	dontaudit udev_t self:capability sys_tty_config;
')

# for systemd-udevd to rename interfaces
allow udev_t self:netlink_route_socket nlmsg_write;

can_exec(udev_t, udev_exec_t)

allow udev_t udev_helper_exec_t:dir list_dir_perms;
can_exec(udev_t, udev_helper_exec_t)

# read udev config
allow udev_t udev_etc_t:file read_file_perms;
allow udev_t udev_runtime_t:dir watch;

list_dirs_pattern(udev_t, udev_rules_t, udev_rules_t)
manage_files_pattern(udev_t, udev_rules_t, udev_rules_t)
manage_lnk_files_pattern(udev_t, udev_rules_t, udev_rules_t)
allow udev_t udev_rules_t:dir watch;

manage_dirs_pattern(udev_t, udev_runtime_t, udev_runtime_t)
manage_files_pattern(udev_t, udev_runtime_t, udev_runtime_t)
manage_lnk_files_pattern(udev_t, udev_runtime_t, udev_runtime_t)
manage_sock_files_pattern(udev_t, udev_runtime_t, udev_runtime_t)
files_runtime_filetrans(udev_t, udev_runtime_t, dir, "udev")

kernel_load_module(udev_t)
kernel_read_system_state(udev_t)
kernel_request_load_module(udev_t)
kernel_getattr_core_if(udev_t)
kernel_use_fds(udev_t)
kernel_read_device_sysctls(udev_t)
kernel_read_hotplug_sysctls(udev_t)
kernel_read_modprobe_sysctls(udev_t)
kernel_read_kernel_sysctls(udev_t)
kernel_rw_fs_sysctls(udev_t)
kernel_rw_hotplug_sysctls(udev_t)
kernel_rw_unix_dgram_sockets(udev_t)
kernel_signal(udev_t)
kernel_search_debugfs(udev_t)
kernel_search_key(udev_t)
# kpartx:
kernel_get_sysvipc_info(udev_t)
# needed as of systemd 255
kernel_read_fs_sysctls(udev_t)
#https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=235182
kernel_rw_net_sysctls(udev_t)
kernel_read_network_state(udev_t)
kernel_read_software_raid_state(udev_t)
kernel_dontaudit_search_unlabeled(udev_t)

corecmd_exec_all_executables(udev_t)

dev_rw_sysfs(udev_t)
dev_manage_all_dev_nodes(udev_t)
dev_rw_generic_files(udev_t)
dev_delete_generic_files(udev_t)
dev_search_usbfs(udev_t)
dev_relabel_all_dev_nodes(udev_t)
# udev_node.c/node_symlink() symlink labels are explicitly
# preserved, instead of short circuiting the relabel
dev_relabel_generic_symlinks(udev_t)
dev_manage_generic_symlinks(udev_t)

domain_read_all_domains_state(udev_t)
domain_dontaudit_ptrace_all_domains(udev_t) #pidof triggers these

files_read_usr_files(udev_t)
files_read_etc_runtime_files(udev_t)
files_read_etc_files(udev_t)
files_mmap_read_kernel_modules(udev_t)
files_exec_etc_files(udev_t)
files_getattr_generic_locks(udev_t)
files_search_mnt(udev_t)
files_dontaudit_getattr_default_files(udev_t)
files_dontaudit_getattr_home_dir(udev_t)
files_dontaudit_getattr_lost_found_dirs(udev_t)
files_dontaudit_getattr_tmp_dirs(udev_t)

fs_getattr_all_fs(udev_t)
fs_list_inotifyfs(udev_t)
fs_read_cgroup_files(udev_t)
fs_rw_anon_inodefs_files(udev_t)
fs_search_tracefs(udev_t)
fs_manage_efivarfs_files(udev_t)
fs_watch_memory_pressure(udev_t)

mls_file_read_all_levels(udev_t)
mls_file_write_all_levels(udev_t)
mls_file_upgrade(udev_t)
mls_file_downgrade(udev_t)
mls_process_write_all_levels(udev_t)

selinux_get_fs_mount(udev_t)
selinux_validate_context(udev_t)
selinux_compute_access_vector(udev_t)
selinux_compute_create_context(udev_t)
selinux_compute_relabel_context(udev_t)
selinux_compute_user_contexts(udev_t)
selinux_use_status_page(udev_t)

storage_watch_fixed_disk(udev_t)

auth_read_pam_console_data(udev_t)
auth_domtrans_pam_console(udev_t)
auth_use_nsswitch(udev_t)

init_read_utmp(udev_t)
init_domtrans_script(udev_t)
# systemd-udevd searches /run/systemd
init_search_run(udev_t)
init_dontaudit_write_utmp(udev_t)
init_getattr_initctl(udev_t)
init_start_all_units(udev_t)
init_stop_all_units(udev_t)
# for hdparm init script run by udev
init_get_script_status(udev_t)

logging_search_logs(udev_t)
logging_send_syslog_msg(udev_t)
logging_send_audit_msgs(udev_t)

miscfiles_read_localization(udev_t)
miscfiles_read_hwdata(udev_t)

modutils_domtrans(udev_t)
modutils_read_module_config(udev_t)
# read modules.inputmap:
modutils_read_module_deps(udev_t)

seutil_read_config(udev_t)
seutil_read_default_contexts(udev_t)
seutil_read_file_contexts(udev_t)
seutil_domtrans_setfiles(udev_t)

sysnet_domtrans_ifconfig(udev_t)
sysnet_domtrans_dhcpc(udev_t)
sysnet_rw_dhcp_config(udev_t)
sysnet_read_dhcpc_runtime_files(udev_t)
sysnet_delete_dhcpc_runtime_files(udev_t)
sysnet_signal_dhcpc(udev_t)
sysnet_manage_config(udev_t)
sysnet_etc_filetrans_config(udev_t)

systemd_map_hwdb(udev_t)
systemd_read_hwdb(udev_t)

userdom_dontaudit_getattr_user_home_dirs(udev_t)
userdom_dontaudit_search_user_home_content(udev_t)

ifdef(`distro_debian',`
	# for https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=851933
	files_read_default_files(udev_t)

	files_runtime_filetrans(udev_t, udev_runtime_t, dir, "xen-hotplug")

	sysnet_runtime_filetrans_config(udev_t, dir, "network")
	sysnet_create_config(udev_t)

	optional_policy(`
		# for /usr/lib/avahi/avahi-daemon-check-dns.sh
		kernel_read_vm_sysctls(udev_t)
		corenet_udp_bind_generic_node(udev_t)
		miscfiles_read_generic_certs(udev_t)
		avahi_create_runtime_dirs(udev_t)
		avahi_initrc_domtrans(udev_t)
		avahi_manage_runtime_files(udev_t)
		avahi_setattr_runtime_dirs(udev_t)
		avahi_filetrans_runtime(udev_t, dir, "avahi-daemon")
	')

	optional_policy(`
		consolesetup_exec_conf(udev_t)
		consolesetup_manage_runtime(udev_t)
		consolesetup_runtime_filetrans_runtime_dir(udev_t)
	')
')

ifdef(`distro_gentoo',`
	# during boot, init scripts use /dev/.rcsysinit
	# existence to determine if we are in early booting
	init_getattr_script_status_files(udev_t)
')

ifdef(`distro_redhat',`
	fs_manage_tmpfs_dirs(udev_t)
	fs_manage_tmpfs_files(udev_t)
	fs_manage_tmpfs_symlinks(udev_t)
	fs_manage_tmpfs_sockets(udev_t)
	fs_manage_tmpfs_blk_files(udev_t)
	fs_manage_tmpfs_chr_files(udev_t)
	fs_relabel_tmpfs_blk_files(udev_t)
	fs_relabel_tmpfs_chr_files(udev_t)

	term_search_ptys(udev_t)

	# for arping used for static IP addresses on PCMCIA ethernet
	netutils_domtrans(udev_t)

	optional_policy(`
		unconfined_domain(udev_t)
	')
')

ifdef(`init_systemd',`
	files_search_kernel_modules(udev_t)

	# systemd-udev creates cgroup files under
	# /sys/fs/cgroup/system.slice/systemd-udevd.service/udev
	fs_create_cgroup_dirs(udev_t)
	fs_create_cgroup_files(udev_t)
	fs_rw_cgroup_files(udev_t)

	init_dgram_send(udev_t)
	init_get_generic_units_status(udev_t)
	init_stream_connect(udev_t)
	init_start_system(udev_t)

	systemd_read_logind_sessions_files(udev_t)
	systemd_read_logind_runtime_files(udev_t)
	# udev searches for .link files and applies custom udev rules
	systemd_read_networkd_runtime(udev_t)

	optional_policy(`
		init_dbus_chat(udev_t)
	')
',`
	fs_manage_tmpfs_dirs(udev_t)
	fs_manage_tmpfs_files(udev_t)
')

optional_policy(`
	alsa_domtrans(udev_t)
	alsa_read_lib(udev_t)
	alsa_read_config(udev_t)
')

optional_policy(`
	bluetooth_domtrans(udev_t)
')

optional_policy(`
	brctl_domtrans(udev_t)
')

optional_policy(`
	clock_domtrans(udev_t)
')

optional_policy(`
	consoletype_exec(udev_t)
')

optional_policy(`
	cups_domtrans_config(udev_t)
	cups_domtrans_hplip(udev_t)
')

optional_policy(`
	dbus_system_bus_client(udev_t)
	dbus_use_system_bus_fds(udev_t)
')

optional_policy(`
	devicekit_read_runtime_files(udev_t)
	devicekit_dgram_send(udev_t)
')

optional_policy(`
	fstools_domtrans(udev_t)
')

optional_policy(`
	iptables_domtrans(udev_t)
')

optional_policy(`
	lvm_domtrans(udev_t)
')

optional_policy(`
	mount_domtrans(udev_t)
')

optional_policy(`
	openct_read_runtime_files(udev_t)
	openct_domtrans(udev_t)
')

optional_policy(`
	pcscd_read_runtime_files(udev_t)
	pcscd_domtrans(udev_t)
')

optional_policy(`
	raid_domtrans_mdadm(udev_t)
	raid_read_mdadm_runtime_files(udev_t)
')

optional_policy(`
	unconfined_signal(udev_t)
')

optional_policy(`
	vbetool_domtrans(udev_t)
')

optional_policy(`
	# for systemd-udevd when starting xen domu
	virt_read_config(udev_t)
')

optional_policy(`
	kernel_write_xen_state(udev_t)
	kernel_read_xen_state(udev_t)
	xen_manage_log(udev_t)
	xen_read_image_files(udev_t)
	fs_manage_xenfs_files(udev_t)
')

optional_policy(`
	xserver_read_xdm_runtime_files(udev_t)
')

optional_policy(`
	zfs_read_config(udev_t)
')

########################################
#
# udevadm Local policy
#

allow udevadm_t self:capability dac_read_search;
allow udevadm_t self:netlink_kobject_uevent_socket create_socket_perms;
allow udevadm_t self:unix_stream_socket create_socket_perms;

stream_connect_pattern(udevadm_t, udev_runtime_t, udev_runtime_t, udev_t)

delete_dirs_pattern(udevadm_t, udev_runtime_t, udev_runtime_t)
delete_files_pattern(udevadm_t, udev_runtime_t, udev_runtime_t)
delete_lnk_files_pattern(udevadm_t, udev_runtime_t, udev_runtime_t)
list_dirs_pattern(udevadm_t, udev_runtime_t, udev_runtime_t)
read_files_pattern(udevadm_t, udev_runtime_t, udev_runtime_t)
read_lnk_files_pattern(udevadm_t, udev_runtime_t, udev_runtime_t)
allow udevadm_t udev_runtime_t:dir watch;

dev_getattr_sysfs(udevadm_t)
dev_rw_sysfs(udevadm_t)
dev_getattr_all_chr_files(udevadm_t)
dev_getattr_generic_chr_files(udevadm_t)
dev_read_urand(udevadm_t)

domain_use_interactive_fds(udevadm_t)

files_read_etc_files(udevadm_t)
files_read_usr_files(udevadm_t)

fs_getattr_xattr_fs(udevadm_t)

init_list_runtime(udevadm_t)
init_read_state(udevadm_t)

kernel_dontaudit_getattr_proc(udevadm_t)
kernel_read_kernel_sysctls(udevadm_t)
kernel_read_system_state(udevadm_t)

selinux_use_status_page(udevadm_t)

seutil_read_file_contexts(udevadm_t)

storage_getattr_fixed_disk_dev(udevadm_t)

userdom_use_user_terminals(udevadm_t)
