policy_module(ssh, 2.4.2)

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

## <desc>
##      <p>
##      allow host key based authentication
##      </p>
## </desc>
gen_tunable(ssh_keysign, false)

## <desc>
##	<p>
##	Allow ssh logins as sysadm_r:sysadm_t
##	</p>
## </desc>
gen_tunable(ssh_sysadm_login, false)

## <desc>
## <p>
## allow ssh with chroot env to read and write files
## in the user home directories
## </p>
## </desc>
gen_tunable(ssh_chroot_rw_homedirs, false)

## <desc>
## <p>
## Allow sshd to use tcp wrappers
## </p>
## </desc>
gen_tunable(ssh_use_tcpd, false)


attribute ssh_dyntransition_domain;
attribute ssh_server;
attribute ssh_agent_type;

ssh_dyntransition_domain_template(chroot_user_t)
ssh_dyntransition_domain_template(sshd_sandbox_t)
ssh_dyntransition_domain_template(sshd_net_t)

type ssh_keygen_t;
type ssh_keygen_exec_t;
init_system_domain(ssh_keygen_t, ssh_keygen_exec_t)

type ssh_keygen_tmp_t;
files_tmp_file(ssh_keygen_tmp_t)

type sshd_keygen_t;
type sshd_keygen_exec_t;
init_daemon_domain(sshd_keygen_t, sshd_keygen_exec_t)

type sshd_keygen_unit_file_t;
systemd_unit_file(sshd_keygen_unit_file_t)

type sshd_exec_t;
corecmd_executable_file(sshd_exec_t)

ssh_server_template(sshd)
init_daemon_domain(sshd_t, sshd_exec_t)
mls_trusted_object(sshd_t)
mls_process_write_all_levels(sshd_t)
mls_dbus_send_all_levels(sshd_t)

type sshd_initrc_exec_t;
init_script_file(sshd_initrc_exec_t)

type sshd_unit_file_t;
systemd_unit_file(sshd_unit_file_t)

type sshd_key_t;
files_type(sshd_key_t)

type sshd_keytab_t;
files_type(sshd_keytab_t)

type ssh_t;
type ssh_exec_t;
typealias ssh_t alias { user_ssh_t staff_ssh_t sysadm_ssh_t };
typealias ssh_t alias { auditadm_ssh_t secadm_ssh_t };
userdom_user_application_domain(ssh_t, ssh_exec_t)

type ssh_agent_exec_t;
corecmd_executable_file(ssh_agent_exec_t)

type ssh_agent_tmp_t;
typealias ssh_agent_tmp_t alias { user_ssh_agent_tmp_t staff_ssh_agent_tmp_t sysadm_ssh_agent_tmp_t };
typealias ssh_agent_tmp_t alias { auditadm_ssh_agent_tmp_t secadm_ssh_agent_tmp_t };
userdom_user_tmp_file(ssh_agent_tmp_t)

type ssh_keysign_t;
type ssh_keysign_exec_t;
typealias ssh_keysign_t alias { user_ssh_keysign_t staff_ssh_keysign_t sysadm_ssh_keysign_t };
typealias ssh_keysign_t alias { auditadm_ssh_keysign_t secadm_ssh_keysign_t };
userdom_user_application_domain(ssh_keysign_t, ssh_keysign_exec_t)

type ssh_tmpfs_t;
typealias ssh_tmpfs_t alias { user_ssh_tmpfs_t staff_ssh_tmpfs_t sysadm_ssh_tmpfs_t };
typealias ssh_tmpfs_t alias { auditadm_ssh_tmpfs_t secadm_ssh_tmpfs_t };
userdom_user_tmp_file(ssh_tmpfs_t)

type ssh_home_t;
typealias ssh_home_t alias { home_ssh_t user_ssh_home_t user_home_ssh_t staff_home_ssh_t sysadm_home_ssh_t };
typealias ssh_home_t alias { auditadm_home_ssh_t secadm_home_ssh_t };
userdom_user_home_content(ssh_home_t)
files_poly_parent(ssh_home_t)

ifdef(`enable_mcs',`
	init_ranged_daemon_domain(sshd_t, sshd_exec_t, s0 - mcs_systemhigh)
')

##############################
#
# SSH client local policy
#

allow ssh_t self:capability { setuid setgid  dac_read_search };
allow ssh_t self:process ~{ ptrace setcurrent setexec setfscreate setrlimit execmem execstack execheap };
allow ssh_t self:fd use;
allow ssh_t self:fifo_file rw_fifo_file_perms;
allow ssh_t self:key manage_key_perms;
allow ssh_t self:unix_dgram_socket { create_socket_perms sendto };
allow ssh_t self:unix_stream_socket { create_stream_socket_perms connectto };
allow ssh_t self:shm create_shm_perms;
allow ssh_t self:sem create_sem_perms;
allow ssh_t self:msgq create_msgq_perms;
allow ssh_t self:msg { send receive };
allow ssh_t self:tcp_socket create_stream_socket_perms;
can_exec(ssh_t, ssh_exec_t)

# Read the ssh key file.
allow ssh_t sshd_key_t:file read_file_perms;

manage_files_pattern(ssh_t, ssh_tmpfs_t, ssh_tmpfs_t)
manage_lnk_files_pattern(ssh_t, ssh_tmpfs_t, ssh_tmpfs_t)
manage_fifo_files_pattern(ssh_t, ssh_tmpfs_t, ssh_tmpfs_t)
manage_sock_files_pattern(ssh_t, ssh_tmpfs_t, ssh_tmpfs_t)
#fs_tmpfs_filetrans(ssh_t, ssh_tmpfs_t, { dir file lnk_file sock_file fifo_file })

manage_dirs_pattern(ssh_t, ssh_home_t, ssh_home_t)
manage_sock_files_pattern(ssh_t, ssh_home_t, ssh_home_t)
userdom_user_home_dir_filetrans(ssh_t, ssh_home_t, sock_file)
userdom_user_home_content_filetrans(ssh_t, ssh_home_t, sock_file)
userdom_user_home_dir_filetrans(ssh_t, ssh_home_t, dir, ".ssh")
userdom_read_all_users_keys(ssh_t)
userdom_stream_connect(ssh_t)
userdom_search_admin_dir(sshd_t)

# Allow the ssh program to communicate with ssh-agent.
stream_connect_pattern(ssh_t, ssh_agent_tmp_t, ssh_agent_tmp_t, ssh_agent_type)

allow ssh_t sshd_t:unix_stream_socket connectto;
allow ssh_t sshd_t:peer recv;

# ssh client can manage the keys and config
manage_files_pattern(ssh_t, ssh_home_t, ssh_home_t)
read_lnk_files_pattern(ssh_t, ssh_home_t, ssh_home_t)

# ssh servers can read the user keys and config
manage_dirs_pattern(ssh_server, ssh_home_t, ssh_home_t)
manage_files_pattern(ssh_server, ssh_home_t, ssh_home_t)

kernel_read_kernel_sysctls(ssh_t)
kernel_read_system_state(ssh_t)

corenet_all_recvfrom_netlabel(ssh_t)
corenet_tcp_sendrecv_generic_if(ssh_t)
corenet_tcp_sendrecv_generic_node(ssh_t)
corenet_tcp_sendrecv_all_ports(ssh_t)
corenet_tcp_connect_ssh_port(ssh_t)
corenet_tcp_connect_all_unreserved_ports(ssh_t)
corenet_sendrecv_ssh_client_packets(ssh_t)
corenet_tcp_bind_generic_node(ssh_t)
#corenet_tcp_bind_all_unreserved_ports(ssh_t)
corenet_rw_tun_tap_dev(ssh_t)

dev_read_rand(ssh_t)
dev_read_urand(ssh_t)

fs_getattr_all_fs(ssh_t)
fs_search_auto_mountpoints(ssh_t)

# run helper programs - needed eg for x11-ssh-askpass
corecmd_exec_shell(ssh_t)
corecmd_exec_bin(ssh_t)

domain_use_interactive_fds(ssh_t)

files_list_home(ssh_t)
files_read_usr_files(ssh_t)
files_read_etc_runtime_files(ssh_t)
files_read_etc_files(ssh_t)
files_read_var_files(ssh_t)

logging_send_syslog_msg(ssh_t)
logging_read_generic_logs(ssh_t)

term_use_ptmx(ssh_t)

auth_use_nsswitch(ssh_t)

miscfiles_read_generic_certs(ssh_t)

seutil_read_config(ssh_t)

userdom_dontaudit_list_user_home_dirs(ssh_t)
userdom_search_user_home_dirs(ssh_t)
userdom_search_admin_dir(ssh_t)
# Write to the user domain tty.
userdom_use_inherited_user_terminals(ssh_t)
# needs to read krb/write tgt
userdom_read_user_tmp_files(ssh_t)
userdom_write_user_tmp_files(ssh_t)
userdom_read_user_home_content_symlinks(ssh_t)
userdom_rw_inherited_user_home_content_files(ssh_t)
userdom_read_home_certs(ssh_t)
userdom_home_manager(ssh_t)

tunable_policy(`ssh_keysign',`
	domtrans_pattern(ssh_t, ssh_keysign_exec_t, ssh_keysign_t)
')

# for port forwarding
tunable_policy(`selinuxuser_tcp_server',`
	corenet_tcp_bind_ssh_port(ssh_t)
	corenet_tcp_bind_generic_node(ssh_t)
	corenet_tcp_bind_all_unreserved_ports(ssh_t)
')

ifdef(`enable_mcs',`
    optional_policy(`
        condor_startd_ranged_domtrans_to(sshd_t, sshd_exec_t, mcs_systemlow - mcs_systemhigh)
    ')
')

optional_policy(`
	gnome_stream_connect_gkeyringd(ssh_t)
')

optional_policy(`
    gpg_agent_stream_connect(ssh_t)
')

optional_policy(`
	kerberos_read_keytab(ssh_t)
')

optional_policy(`
	xserver_user_x_domain_template(ssh, ssh_t, ssh_tmpfs_t)
	xserver_domtrans_xauth(ssh_t)
')


##############################
#
# ssh_keysign_t local policy
#

allow ssh_keysign_t self:capability { setgid setuid };
allow ssh_keysign_t self:unix_stream_socket create_socket_perms;
allow ssh_keysign_t self:udp_socket create_socket_perms;

allow ssh_keysign_t sshd_key_t:file { getattr open read };

kernel_read_network_state(ssh_keysign_t)

auth_read_passwd(ssh_keysign_t)

dev_read_urand(ssh_keysign_t)
dev_read_rand(ssh_keysign_t)

files_read_etc_files(ssh_keysign_t)

optional_policy(`
	nscd_use(ssh_keysign_t)
')

optional_policy(`
    sssd_read_public_files(ssh_keysign_t)
')

optional_policy(`
    sge_rw_tcp_sockets(ssh_keysign_t)
')

#################################
#
# sshd local policy
#
# sshd_t is the domain for the sshd program.
#

# so a tunnel can point to another ssh tunnel
allow sshd_t self:netlink_route_socket r_netlink_socket_perms;
allow sshd_t self:key { search link write };
allow sshd_t self:process setcurrent;

allow sshd_t sshd_keytab_t:file read_file_perms;

kernel_search_key(sshd_t)
kernel_link_key(sshd_t)
kernel_read_net_sysctls(sshd_t)

files_search_all(sshd_t)
files_create_home_dir(sshd_t)

fs_search_cgroup_dirs(sshd_t)
fs_rw_cgroup_files(sshd_t)

term_use_all_ptys(sshd_t)
term_setattr_all_ptys(sshd_t)
term_setattr_all_ttys(sshd_t)
term_relabelto_all_ptys(sshd_t)
term_use_ptmx(sshd_t)

# for X forwarding
corenet_tcp_bind_xserver_port(sshd_t)
corenet_tcp_bind_vnc_port(sshd_t)
corenet_sendrecv_xserver_server_packets(sshd_t)

auth_exec_login_program(sshd_t)
auth_signal_chk_passwd(sshd_t)

userdom_read_user_home_content_files(sshd_t)
userdom_read_user_home_content_symlinks(sshd_t)
#userdom_manage_tmp_role(system_r, sshd_t)
userdom_spec_domtrans_unpriv_users(sshd_t)
userdom_signal_unpriv_users(sshd_t)
userdom_dyntransition_unpriv_users(sshd_t)
userdom_map_tmp_files(sshd_t)

tunable_policy(`ssh_sysadm_login',`
	# Relabel and access ptys created by sshd
	# ioctl is necessary for logout() processing for utmp entry and for w to
	# display the tty.
	# some versions of sshd on the new SE Linux require setattr
	userdom_signal_all_users(sshd_t)
	userdom_spec_domtrans_all_users(sshd_t)
	userdom_dyntransition_admin_users(sshd_t)
')

optional_policy(`
	amanda_search_var_lib(sshd_t)
')

optional_policy(`
	condor_rw_lib_files(sshd_t)
	condor_rw_tcp_sockets_startd(sshd_t)
	condor_rw_tcp_sockets_schedd(sshd_t)
')

optional_policy(`
    cockpit_read_pid_files(sshd_t)
')

optional_policy(`
	daemontools_service_domain(sshd_t, sshd_exec_t)
')

optional_policy(`
	ftp_dyntrans_sftpd(sshd_t)
	ftp_dyntrans_anon_sftpd(sshd_t)
')

optional_policy(`
	gitosis_manage_lib_files(sshd_t)
')

optional_policy(`
    gnome_exec_keyringd(sshd_t)
')

optional_policy(`
	inetd_tcp_service_domain(sshd_t, sshd_exec_t)
')

optional_policy(`
	kerberos_read_keytab(sshd_t)
	kerberos_tmp_filetrans_host_rcache(sshd_t, "krb5_0.rcache2")
	kerberos_use(sshd_t)
	kerberos_write_kadmind_tmp_files(sshd_t)
')

optional_policy(`
    lvm_domtrans(sshd_t)
')

optional_policy(`
	munin_read_var_lib_files(sshd_t)
')

optional_policy(`
	nx_read_home_files(sshd_t)
')

optional_policy(`
	oddjob_domtrans_mkhomedir(sshd_t)
')

optional_policy(`
	rpc_rw_gssd_keys(sshd_t)
')

optional_policy(`
	rpm_use_script_fds(sshd_t)
')

optional_policy(`
	rssh_spec_domtrans(sshd_t)
	# For reading /home/user/.ssh
	rssh_read_ro_content(sshd_t)
')

optional_policy(`
	rsync_read_data(sshd_t)
')

optional_policy(`
	systemd_exec_systemctl(sshd_t)
')

optional_policy(`
	usermanage_domtrans_passwd(sshd_t)
	usermanage_read_crack_db(sshd_t)
')

optional_policy(`
	openshift_dyntransition(sshd_t)
	openshift_transition(sshd_t)
	openshift_manage_tmp_files(sshd_t)
	openshift_manage_tmp_sockets(sshd_t)
	openshift_mounton_tmp(sshd_t)
	openshift_read_lib_files(sshd_t)
')

optional_policy(`
	postgresql_search_db(sshd_t)
')

optional_policy(`
	unconfined_shell_domtrans(sshd_t)
')

optional_policy(`
    sge_rw_tcp_sockets(sshd_t)
')

optional_policy(`
	kernel_write_proc_files(sshd_t)
	virt_transition_svirt_sandbox(sshd_t, system_r)
	virt_stream_connect_sandbox(sshd_t)
	virt_stream_connect(sshd_t)
')

optional_policy(`
	xserver_domtrans_xauth(sshd_t)
    xserver_xdm_signull(sshd_t)
')

optional_policy(`
    tunable_policy(`ssh_use_tcpd',`
        tcpd_wrapped_domain(sshd_t,sshd_exec_t)
        tcpd_rw_tcp_sockets(sshd_net_t)
    ')
')

ifdef(`TODO',`
	tunable_policy(`ssh_sysadm_login',`
		# Relabel and access ptys created by sshd
		# ioctl is necessary for logout() processing for utmp entry and for w to
		# display the tty.
		# some versions of sshd on the new SE Linux require setattr
		allow sshd_t ptyfile:chr_file relabelto;

			optional_policy(`
				domain_trans(sshd_t, xauth_exec_t, userdomain)
			')
	',`
		optional_policy(`
			domain_trans(sshd_t, xauth_exec_t, unpriv_userdomain)
		')
		# Relabel and access ptys created by sshd
		# ioctl is necessary for logout() processing for utmp entry and for w to
		# display the tty.
		# some versions of sshd on the new SE Linux require setattr
		allow sshd_t userpty_type:chr_file { relabelto rw_inherited_chr_file_perms setattr_chr_file_perms };
	')
') dnl endif TODO

########################################
#
# sshd-keygen local policy
#

allow sshd_keygen_t self:capability { chown fsetid };
allow sshd_keygen_t self:fifo_file rw_fifo_file_perms;
allow sshd_keygen_t self:unix_stream_socket create_stream_socket_perms;

allow sshd_keygen_t sshd_key_t:file manage_file_perms;

kernel_read_system_state(sshd_keygen_t)
kernel_dontaudit_request_load_module(sshd_keygen_t)

corecmd_exec_bin(sshd_keygen_t)

auth_use_nsswitch(sshd_keygen_t)

files_rw_etc_dirs(sshd_keygen_t)

#run restorecon
seutil_domtrans_setfiles(sshd_keygen_t)

ssh_domtrans_keygen(sshd_keygen_t)

optional_policy(`
    plymouthd_exec_plymouth(sshd_keygen_t)
')

########################################
#
# ssh_keygen local policy
#

# ssh_keygen_t is the type of the ssh-keygen program when run at install time
# and by sysadm_t

allow ssh_keygen_t self:capability { dac_read_search  };
dontaudit ssh_keygen_t self:capability sys_tty_config;
allow ssh_keygen_t self:process { sigchld sigkill sigstop signull signal };
allow ssh_keygen_t self:unix_stream_socket create_stream_socket_perms;

allow ssh_keygen_t sshd_key_t:file manage_file_perms;
files_etc_filetrans(ssh_keygen_t, sshd_key_t, file)

manage_dirs_pattern(ssh_keygen_t, ssh_home_t, ssh_home_t)
manage_files_pattern(ssh_keygen_t, ssh_home_t, ssh_home_t)
userdom_admin_home_dir_filetrans(ssh_keygen_t, ssh_home_t, dir)
userdom_user_home_dir_filetrans(ssh_keygen_t, ssh_home_t, dir)

manage_dirs_pattern(ssh_keygen_t, ssh_keygen_tmp_t, ssh_keygen_tmp_t)
manage_files_pattern(ssh_keygen_t, ssh_keygen_tmp_t, ssh_keygen_tmp_t)
files_tmp_filetrans(ssh_keygen_t, ssh_keygen_tmp_t, { file dir })

kernel_read_system_state(ssh_keygen_t)
kernel_read_kernel_sysctls(ssh_keygen_t)

corecmd_exec_shell(ssh_keygen_t)
corecmd_exec_bin(ssh_keygen_t)

fs_search_auto_mountpoints(ssh_keygen_t)

dev_read_sysfs(ssh_keygen_t)
dev_read_rand(ssh_keygen_t)
dev_read_urand(ssh_keygen_t)

term_dontaudit_use_console(ssh_keygen_t)

domain_use_interactive_fds(ssh_keygen_t)

files_read_etc_files(ssh_keygen_t)

init_use_fds(ssh_keygen_t)
init_use_script_ptys(ssh_keygen_t)

auth_use_nsswitch(ssh_keygen_t)

logging_send_syslog_msg(ssh_keygen_t)

userdom_home_manager(ssh_keygen_t)
userdom_dontaudit_use_unpriv_user_fds(ssh_keygen_t)
userdom_use_user_terminals(ssh_keygen_t)

optional_policy(`
	seutil_sigchld_newrole(ssh_keygen_t)
')

optional_policy(`
	udev_read_db(ssh_keygen_t)
')

####################################
#
# ssh_dyntransition domain local policy
#

allow ssh_dyntransition_domain self:capability { setuid sys_chroot setgid };
allow ssh_dyntransition_domain  self:unix_dgram_socket create_socket_perms;

allow ssh_dyntransition_domain self:fifo_file rw_fifo_file_perms;
allow ssh_dyntransition_domain sshd_t:fd use;

optional_policy(`
    ssh_rw_stream_sockets(ssh_dyntransition_domain)
    ssh_rw_tcp_sockets(ssh_dyntransition_domain)
')

#####################################
#
# ssh_sandbox local policy
#

allow sshd_t sshd_sandbox_t:process signal;

init_ioctl_stream_sockets(sshd_sandbox_t)

logging_send_audit_msgs(sshd_sandbox_t)

#####################################
#
#  sshd [net] child local policy
#

allow sshd_t sshd_net_t:process signal;

allow sshd_net_t self:process setrlimit;

dev_rw_crypto(sshd_net_t)

init_ioctl_stream_sockets(sshd_net_t)
init_rw_tcp_sockets(sshd_net_t)

logging_send_audit_msgs(sshd_net_t)


######################################
#
# chroot_user_t local policy
#
allow chroot_user_t self:fifo_file rw_fifo_file_perms;
allow chroot_user_t self:unix_dgram_socket create_socket_perms;

corecmd_exec_shell(chroot_user_t)

domain_subj_id_change_exemption(chroot_user_t)
domain_role_change_exemption(chroot_user_t)

term_search_ptys(chroot_user_t)
term_use_ptmx(chroot_user_t)

fs_getattr_all_fs(chroot_user_t)

userdom_read_user_home_content_files(chroot_user_t)
userdom_read_inherited_user_home_content_files(chroot_user_t)
userdom_read_user_home_content_symlinks(chroot_user_t)
userdom_exec_user_home_content_files(chroot_user_t)
userdom_use_inherited_user_ptys(chroot_user_t)

tunable_policy(`ssh_chroot_rw_homedirs',`
        files_list_home(chroot_user_t)
		userdom_manage_user_home_content_files(chroot_user_t)
		userdom_manage_user_home_content_symlinks(chroot_user_t)
		userdom_manage_user_home_content_pipes(chroot_user_t)
		userdom_manage_user_home_content_sockets(chroot_user_t)
		userdom_manage_user_home_content_dirs(chroot_user_t)
')

tunable_policy(`ssh_chroot_rw_homedirs && use_nfs_home_dirs',`
    fs_manage_nfs_dirs(chroot_user_t)
    fs_manage_nfs_files(chroot_user_t)
    fs_manage_nfs_symlinks(chroot_user_t)
')

tunable_policy(`ssh_chroot_rw_homedirs && use_samba_home_dirs',`
    fs_manage_cifs_dirs(chroot_user_t)
    fs_manage_cifs_files(chroot_user_t)
    fs_manage_cifs_symlinks(chroot_user_t)
')

tunable_policy(`ssh_chroot_rw_homedirs && use_fusefs_home_dirs',`
    fs_manage_fusefs_dirs(chroot_user_t)
    fs_manage_fusefs_files(chroot_user_t)
    fs_manage_fusefs_symlinks(chroot_user_t)
')

tunable_policy(`use_samba_home_dirs',`
    fs_read_cifs_files(chroot_user_t)
    fs_read_cifs_symlinks(chroot_user_t)
')

userdom_home_manager(chroot_user_t)

optional_policy(`
    ssh_rw_dgram_sockets(chroot_user_t)
')

optional_policy(`
    unconfined_shell_domtrans(chroot_user_t)
')

######################################
#
# ssh_agent_type common policy local policy
#
allow ssh_agent_type self:process setrlimit;
allow ssh_agent_type self:capability setgid;

manage_dirs_pattern(ssh_agent_type, ssh_agent_tmp_t, ssh_agent_tmp_t)
manage_sock_files_pattern(ssh_agent_type, ssh_agent_tmp_t, ssh_agent_tmp_t)
files_tmp_filetrans(ssh_agent_type, ssh_agent_tmp_t, { dir sock_file })

kernel_read_kernel_sysctls(ssh_agent_type)

dev_read_urand(ssh_agent_type)
dev_read_rand(ssh_agent_type)

fs_search_auto_mountpoints(ssh_agent_type)

domain_use_interactive_fds(ssh_agent_type)

files_read_etc_files(ssh_agent_type)
files_read_etc_runtime_files(ssh_agent_type)

libs_read_lib_files(ssh_agent_type)

miscfiles_read_generic_certs(ssh_agent_type)

# Write to the user domain tty.
userdom_use_inherited_user_terminals(ssh_agent_type)

# for the transition back to normal privs upon exec
userdom_search_user_home_content(ssh_agent_type)

optional_policy(`
    cockpit_rw_pipes(ssh_agent_type)
')

optional_policy(`
	xserver_use_xdm_fds(ssh_agent_type)
	xserver_rw_xdm_pipes(ssh_agent_type)
')
