policy_module(locallogin, 1.12.0)

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

type local_login_t;
domain_interactive_fd(local_login_t)
auth_login_pgm_domain(local_login_t)
auth_login_entry_type(local_login_t)

type local_login_lock_t;
files_lock_file(local_login_lock_t)

type local_login_home_t;
userdom_user_home_content(local_login_home_t)

type sulogin_t;
type sulogin_exec_t;
domain_obj_id_change_exemption(sulogin_t)
domain_subj_id_change_exemption(sulogin_t)
domain_role_change_exemption(sulogin_t)
domain_interactive_fd(sulogin_t)
init_domain(sulogin_t, sulogin_exec_t)
init_system_domain(sulogin_t, sulogin_exec_t)
role system_r types sulogin_t;

ifdef(`enable_mcs',`
	init_ranged_daemon_domain(sulogin_t, sulogin_exec_t, s0 - mcs_systemhigh)
')

ifdef(`enable_mls',`
	init_ranged_daemon_domain(sulogin_t, sulogin_exec_t, s0 - mls_systemhigh)
')

########################################
#
# Local login local policy
#

allow local_login_t self:capability { dac_read_search dac_override chown fowner fsetid kill setgid setuid sys_admin sys_nice sys_resource sys_tty_config };
allow local_login_t self:process ~{ ptrace setcurrent setfscreate execmem execstack execheap };
allow local_login_t self:fd use;
allow local_login_t self:fifo_file rw_fifo_file_perms;
allow local_login_t self:sock_file read_sock_file_perms;
allow local_login_t self:unix_dgram_socket create_socket_perms;
allow local_login_t self:unix_stream_socket create_stream_socket_perms;
allow local_login_t self:unix_dgram_socket sendto;
allow local_login_t self:unix_stream_socket connectto;
allow local_login_t self:shm create_shm_perms;
allow local_login_t self:sem create_sem_perms;
allow local_login_t self:msgq create_msgq_perms;
allow local_login_t self:msg { send receive };
allow local_login_t self:key { search write link };

allow local_login_t local_login_lock_t:file manage_file_perms;
files_lock_filetrans(local_login_t, local_login_lock_t, file)

allow local_login_t local_login_home_t:file read_file_perms;

kernel_read_system_state(local_login_t)
kernel_read_kernel_sysctls(local_login_t)
kernel_search_key(local_login_t)
kernel_link_key(local_login_t)

corecmd_list_bin(local_login_t)
corecmd_read_bin_symlinks(local_login_t)
# cjp: these are probably not needed:
corecmd_read_bin_files(local_login_t)
corecmd_read_bin_pipes(local_login_t)
corecmd_read_bin_sockets(local_login_t)

dev_setattr_mouse_dev(local_login_t)
dev_getattr_mouse_dev(local_login_t)
dev_getattr_power_mgmt_dev(local_login_t)
dev_setattr_power_mgmt_dev(local_login_t)
dev_getattr_sound_dev(local_login_t)
dev_setattr_sound_dev(local_login_t)
dev_rw_generic_usb_dev(local_login_t)
dev_read_video_dev(local_login_t)
dev_dontaudit_getattr_apm_bios_dev(local_login_t)
dev_dontaudit_setattr_apm_bios_dev(local_login_t)
dev_dontaudit_read_framebuffer(local_login_t)
dev_dontaudit_setattr_framebuffer_dev(local_login_t)
dev_dontaudit_getattr_generic_blk_files(local_login_t)
dev_dontaudit_setattr_generic_blk_files(local_login_t)
dev_dontaudit_getattr_generic_chr_files(local_login_t)
dev_dontaudit_setattr_generic_chr_files(local_login_t)
dev_dontaudit_setattr_generic_symlinks(local_login_t)
dev_dontaudit_getattr_misc_dev(local_login_t)
dev_dontaudit_setattr_misc_dev(local_login_t)
dev_dontaudit_getattr_scanner_dev(local_login_t)
dev_dontaudit_setattr_scanner_dev(local_login_t)
dev_dontaudit_search_sysfs(local_login_t)
dev_dontaudit_getattr_video_dev(local_login_t)
dev_dontaudit_setattr_video_dev(local_login_t)

domain_read_all_entry_files(local_login_t)

files_read_etc_files(local_login_t)
files_read_etc_runtime_files(local_login_t)
files_read_usr_files(local_login_t)
files_list_mnt(local_login_t)
files_list_world_readable(local_login_t)
files_read_world_readable_files(local_login_t)
files_read_world_readable_symlinks(local_login_t)
files_read_world_readable_pipes(local_login_t)
files_read_world_readable_sockets(local_login_t)
# for when /var/mail is a symlink
files_read_var_symlinks(local_login_t)
files_create_home_dir(local_login_t)

fs_search_auto_mountpoints(local_login_t)

storage_dontaudit_getattr_fixed_disk_dev(local_login_t)
storage_dontaudit_setattr_fixed_disk_dev(local_login_t)
storage_dontaudit_getattr_removable_dev(local_login_t)
storage_dontaudit_setattr_removable_dev(local_login_t)

term_use_all_ttys(local_login_t)
term_use_unallocated_ttys(local_login_t)
term_relabel_unallocated_ttys(local_login_t)
term_relabel_all_ttys(local_login_t)
term_setattr_all_ttys(local_login_t)
term_setattr_unallocated_ttys(local_login_t)
term_use_usb_ttys(local_login_t)
term_use_all_ptys(local_login_t)
term_relabel_all_ptys(local_login_t)
term_setattr_all_ptys(local_login_t)

auth_rw_login_records(local_login_t)
auth_rw_faillog(local_login_t)
auth_manage_pam_console_data(local_login_t)
auth_domtrans_pam_console(local_login_t)
auth_use_nsswitch(local_login_t)

init_dontaudit_use_fds(local_login_t)
init_stream_connect(local_login_t)

udev_read_db(local_login_t)

userdom_spec_domtrans_all_users(local_login_t)
userdom_signal_all_users(local_login_t)
userdom_search_user_home_content(local_login_t)
userdom_use_unpriv_users_fds(local_login_t)
userdom_sigchld_all_users(local_login_t)
userdom_create_all_users_keys(local_login_t)

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

userdom_home_reader(local_login_t)
userdom_manage_tmp_files(local_login_t)
userdom_tmp_filetrans_user_tmp(local_login_t, file)

tunable_policy(`login_console_enabled',`
     term_use_console(local_login_t)
     # Able to relabel /dev/console to user tty types.
     term_relabel_console(local_login_t)
     term_setattr_console(local_login_t)
')

optional_policy(`
	alsa_domtrans(local_login_t)
')

optional_policy(`
	dbus_system_bus_client(local_login_t)

	consolekit_dbus_chat(local_login_t)
    snapper_dbus_chat(local_login_t)
')

optional_policy(`
	gpm_getattr_gpmctl(local_login_t)
	gpm_setattr_gpmctl(local_login_t)
')

optional_policy(`
	# Search for mail spool file.
	mta_getattr_spool(local_login_t)
')

optional_policy(`
	udev_read_db(local_login_t)
')

optional_policy(`
	unconfined_shell_domtrans(local_login_t)
')

optional_policy(`
	usermanage_read_crack_db(local_login_t)
')

optional_policy(`
	xserver_read_xdm_tmp_files(local_login_t)
	xserver_rw_xdm_tmp_files(local_login_t)
    xserver_rw_xdm_keys(local_login_t)
')

#################################
#
# Sulogin local policy
#

allow sulogin_t self:capability { dac_read_search  sys_admin };
allow sulogin_t self:process ~{ ptrace setcurrent setexec setfscreate setrlimit execmem execstack execheap };
allow sulogin_t self:fd use;
allow sulogin_t self:fifo_file rw_fifo_file_perms;
allow sulogin_t self:unix_dgram_socket create_socket_perms;
allow sulogin_t self:unix_stream_socket create_stream_socket_perms;
allow sulogin_t self:unix_dgram_socket sendto;
allow sulogin_t self:unix_stream_socket connectto;
allow sulogin_t self:shm create_shm_perms;
allow sulogin_t self:sem create_sem_perms;
allow sulogin_t self:msgq create_msgq_perms;
allow sulogin_t self:msg { send receive };

kernel_getattr_core_if(sulogin_t)
kernel_read_crypto_sysctls(sulogin_t)
kernel_read_system_state(sulogin_t)

dev_getattr_all_chr_files(sulogin_t)
dev_getattr_all_blk_files(sulogin_t)

dev_read_urand(sulogin_t)
dev_read_rand(sulogin_t)

fs_search_auto_mountpoints(sulogin_t)
fs_rw_tmpfs_chr_files(sulogin_t)

files_read_etc_files(sulogin_t)
# because file systems are not mounted:
files_dontaudit_search_isid_type_dirs(sulogin_t)
files_search_pids(sulogin_t)

auth_read_shadow(sulogin_t)
auth_use_nsswitch(sulogin_t)

init_getpgid_script(sulogin_t)
init_getpgid(sulogin_t)
init_getattr_initctl(sulogin_t)

logging_send_syslog_msg(sulogin_t)

seutil_read_config(sulogin_t)
seutil_read_default_contexts(sulogin_t)

userdom_use_unpriv_users_fds(sulogin_t)

userdom_search_admin_dir(sulogin_t)
userdom_search_user_home_dirs(sulogin_t)
userdom_use_user_ptys(sulogin_t)

term_use_console(sulogin_t)
term_use_unallocated_ttys(sulogin_t)
term_use_generic_ptys(sulogin_t)

ifdef(`enable_mls',`
	sysadm_shell_domtrans(sulogin_t)
',`
	optional_policy(`
		unconfined_shell_domtrans(sulogin_t)
	')
')

# suse and debian do not use pam with sulogin...
ifdef(`distro_suse', `define(`sulogin_no_pam')')
ifdef(`distro_debian', `define(`sulogin_no_pam')')

allow sulogin_t self:capability sys_tty_config;
ifdef(`sulogin_no_pam', `
	init_getpgid(sulogin_t)
', `
	allow sulogin_t self:process setexec;
	selinux_get_fs_mount(sulogin_t)
	selinux_validate_context(sulogin_t)
	selinux_compute_access_vector(sulogin_t)
	selinux_compute_create_context(sulogin_t)
	selinux_compute_relabel_context(sulogin_t)
	selinux_compute_user_contexts(sulogin_t)
')

optional_policy(`
	plymouthd_exec_plymouth(sulogin_t)
')
