policy_module(ssh, 2.16.6)

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

## <desc>
## <p>
## allow host key based authentication
## </p>
## </desc>
gen_tunable(allow_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 to use gpg-agent
## </p>
## </desc>
gen_tunable(ssh_use_gpg_agent, false)

attribute ssh_server;
attribute ssh_agent_type;

type ssh_keygen_t;
type ssh_keygen_exec_t;
init_system_domain(ssh_keygen_t, ssh_keygen_exec_t)
role system_r types ssh_keygen_t;

type sshd_exec_t;
corecmd_executable_file(sshd_exec_t)

ssh_server_template(sshd)
init_daemon_domain(sshd_t, sshd_exec_t)

type sshd_key_t;
files_type(sshd_key_t)

type sshd_tmp_t;
files_tmp_file(sshd_tmp_t)
files_poly_parent(sshd_tmp_t)

type sshd_keygen_unit_t;
init_unit_file(sshd_keygen_unit_t)

type sshd_unit_t;
init_unit_file(sshd_unit_t)

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

type ssh_t;
type ssh_exec_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;
userdom_user_tmp_file(ssh_agent_tmp_t)

type ssh_keysign_t;
type ssh_keysign_exec_t;
userdom_user_application_domain(ssh_keysign_t, ssh_keysign_exec_t)

type ssh_tmpfs_t;
userdom_user_tmpfs_file(ssh_tmpfs_t)

type ssh_home_t;
userdom_user_home_content(ssh_home_t)

type sshd_keytab_t;
files_type(sshd_keytab_t)

ifdef(`distro_debian',`
	init_daemon_runtime_file(sshd_runtime_t, dir, "sshd")
')

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

allow ssh_t self:capability { dac_override dac_read_search setgid setuid };
allow ssh_t self:process { transition signal_perms getsched setsched getsession getpgid setpgid getcap setcap share getattr noatsecure siginh rlimitinh dyntransition setkeycreate setsockcreate getrlimit };
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;

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

# Access the ssh temporary files.
allow ssh_t sshd_tmp_t:dir manage_dir_perms;
allow ssh_t sshd_tmp_t:file manage_file_perms;
files_tmp_filetrans(ssh_t, sshd_tmp_t, { file dir })

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, { dir sock_file })

# 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;

# 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
allow ssh_server ssh_home_t:dir list_dir_perms;
read_files_pattern(ssh_server, ssh_home_t, ssh_home_t)
read_lnk_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_connect_ssh_port(ssh_t)
corenet_sendrecv_ssh_client_packets(ssh_t)

dev_read_urand(ssh_t)
dev_dontaudit_read_rand(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)

auth_use_nsswitch(ssh_t)

miscfiles_read_generic_certs(ssh_t)
miscfiles_read_localization(ssh_t)

seutil_read_config(ssh_t)

userdom_dontaudit_list_user_home_dirs(ssh_t)
userdom_search_user_home_dirs(ssh_t)
# Write to the user domain tty.
userdom_use_user_terminals(ssh_t)
# needs to read krb tgt
userdom_read_user_tmp_files(ssh_t)

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

tunable_policy(`use_nfs_home_dirs',`
	fs_manage_nfs_dirs(ssh_t)
	fs_manage_nfs_files(ssh_t)
')

tunable_policy(`use_samba_home_dirs',`
	fs_manage_cifs_dirs(ssh_t)
	fs_manage_cifs_files(ssh_t)
')

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

optional_policy(`
	tunable_policy(`ssh_use_gpg_agent',`
		gpg_stream_connect_agent(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 sshd_key_t:file read_inherited_file_perms;

dev_read_urand(ssh_keysign_t)

files_read_etc_files(ssh_keysign_t)

optional_policy(`
	nscd_use(ssh_keysign_t)
')

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

# for /run/user/UID/bus access, probably pam_systemd.so
allow sshd_t self:capability dac_read_search;

# 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 sshd_keytab_t:file read_file_perms;

manage_dirs_pattern(sshd_t, sshd_tmp_t, sshd_tmp_t)
manage_files_pattern(sshd_t, sshd_tmp_t, sshd_tmp_t)
manage_sock_files_pattern(sshd_t, sshd_tmp_t, sshd_tmp_t)
files_tmp_filetrans(sshd_t, sshd_tmp_t, { dir file sock_file })

# to exec /usr/bin/fipscheck
corecmd_exec_bin(sshd_t)

kernel_link_key(sshd_t)
kernel_read_crypto_sysctls(sshd_t)
kernel_search_key(sshd_t)

term_use_all_ptys(sshd_t)
term_setattr_all_ptys(sshd_t)
term_relabelto_all_ptys(sshd_t)

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

ifdef(`distro_debian',`
	allow sshd_t self:process { getcap setcap };
	auth_use_pam_motd_dynamic(sshd_t)
',`
	dontaudit sshd_t self:process { getcap setcap };
')

ifdef(`init_systemd',`
	auth_use_pam_systemd(sshd_t)
	init_dbus_chat(sshd_t)
	init_rw_stream_sockets(sshd_t)
	systemd_write_inherited_logind_sessions_pipes(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_spec_domtrans_all_users(sshd_t)
	userdom_signal_all_users(sshd_t)
',`
	userdom_spec_domtrans_unpriv_users(sshd_t)
	userdom_signal_unpriv_users(sshd_t)
')

tunable_policy(`allow_polyinstantiation',`
	allow sshd_t self:capability dac_override;
	files_relabel_generic_tmp_dirs(sshd_t)
')

optional_policy(`
	daemontools_service_domain(sshd_t, sshd_exec_t)
')

optional_policy(`
	inetd_tcp_service_domain(sshd_t, sshd_exec_t)
')

optional_policy(`
	kerberos_read_keytab(sshd_t)
	kerberos_use(sshd_t)
')

optional_policy(`
	oddjob_domtrans_mkhomedir(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(`
	xserver_domtrans_xauth(sshd_t)
	xserver_link_xdm_keys(sshd_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

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)

kernel_read_kernel_sysctls(ssh_keygen_t)
kernel_dontaudit_getattr_proc(ssh_keygen_t)
kernel_dontaudit_read_system_state(ssh_keygen_t)

fs_search_auto_mountpoints(ssh_keygen_t)

dev_read_sysfs(ssh_keygen_t)
dev_read_urand(ssh_keygen_t)
dev_dontaudit_read_rand(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)

miscfiles_read_localization(ssh_keygen_t)

userdom_dontaudit_use_unpriv_user_fds(ssh_keygen_t)

optional_policy(`
	seutil_sigchld_newrole(ssh_keygen_t)
')

