701bb3
From 3313343ba7803bff077af5d87df2260cdcd2d678 Mon Sep 17 00:00:00 2001
701bb3
From: Adrian Reber <areber@redhat.com>
701bb3
Date: Thu, 2 May 2019 13:41:46 +0000
701bb3
Subject: [PATCH 1/4] lsm: also dump and restore sockcreate
701bb3
701bb3
The file /proc/PID/attr/sockcreate is used by SELinux to label newly
701bb3
created sockets with the label available at sockcreate.
701bb3
701bb3
If it is NULL, the default label of the process will be used.
701bb3
701bb3
This reads out that file during checkpoint and restores the value during
701bb3
restore.
701bb3
701bb3
This value is irrelevant for existing sockets as they might have been
701bb3
created with another context. This is only to make sure that newly
701bb3
created sockets have the correct context.
701bb3
701bb3
Signed-off-by: Adrian Reber <areber@redhat.com>
701bb3
---
701bb3
 criu/cr-restore.c       | 36 ++++++++++++++++++++++++++++++++++++
701bb3
 criu/include/restorer.h |  2 ++
701bb3
 criu/lsm.c              | 32 ++++++++++++++++++++++++++++++++
701bb3
 criu/pie/restorer.c     | 15 ++++++++++-----
701bb3
 images/creds.proto      |  1 +
701bb3
 5 files changed, 81 insertions(+), 5 deletions(-)
701bb3
701bb3
diff --git a/criu/cr-restore.c b/criu/cr-restore.c
701bb3
index 5fd22e9246..f254cbc0eb 100644
701bb3
--- a/criu/cr-restore.c
701bb3
+++ b/criu/cr-restore.c
701bb3
@@ -2997,6 +2997,8 @@ static void rst_reloc_creds(struct thread_restore_args *thread_args,
701bb3
 
701bb3
 	if (args->lsm_profile)
701bb3
 		args->lsm_profile = rst_mem_remap_ptr(args->mem_lsm_profile_pos, RM_PRIVATE);
701bb3
+	if (args->lsm_sockcreate)
701bb3
+		args->lsm_sockcreate = rst_mem_remap_ptr(args->mem_lsm_sockcreate_pos, RM_PRIVATE);
701bb3
 	if (args->groups)
701bb3
 		args->groups = rst_mem_remap_ptr(args->mem_groups_pos, RM_PRIVATE);
701bb3
 
701bb3
@@ -3062,6 +3064,40 @@ rst_prep_creds_args(CredsEntry *ce, unsigned long *prev_pos)
701bb3
 		args->mem_lsm_profile_pos = 0;
701bb3
 	}
701bb3
 
701bb3
+	if (ce->lsm_sockcreate) {
701bb3
+		char *rendered = NULL;
701bb3
+		char *profile;
701bb3
+
701bb3
+		profile = ce->lsm_sockcreate;
701bb3
+
701bb3
+		if (validate_lsm(profile) < 0)
701bb3
+			return ERR_PTR(-EINVAL);
701bb3
+
701bb3
+		if (profile && render_lsm_profile(profile, &rendered)) {
701bb3
+			return ERR_PTR(-EINVAL);
701bb3
+		}
701bb3
+		if (rendered) {
701bb3
+			size_t lsm_sockcreate_len;
701bb3
+			char *lsm_sockcreate;
701bb3
+
701bb3
+			args->mem_lsm_sockcreate_pos = rst_mem_align_cpos(RM_PRIVATE);
701bb3
+			lsm_sockcreate_len = strlen(rendered);
701bb3
+			lsm_sockcreate = rst_mem_alloc(lsm_sockcreate_len + 1, RM_PRIVATE);
701bb3
+			if (!lsm_sockcreate) {
701bb3
+				xfree(rendered);
701bb3
+				return ERR_PTR(-ENOMEM);
701bb3
+			}
701bb3
+
701bb3
+			args = rst_mem_remap_ptr(this_pos, RM_PRIVATE);
701bb3
+			args->lsm_sockcreate = lsm_sockcreate;
701bb3
+			strncpy(args->lsm_sockcreate, rendered, lsm_sockcreate_len);
701bb3
+			xfree(rendered);
701bb3
+		}
701bb3
+	} else {
701bb3
+		args->lsm_sockcreate = NULL;
701bb3
+		args->mem_lsm_sockcreate_pos = 0;
701bb3
+	}
701bb3
+
701bb3
 	/*
701bb3
 	 * Zap fields which we can't use.
701bb3
 	 */
701bb3
diff --git a/criu/include/restorer.h b/criu/include/restorer.h
701bb3
index 2884ce9e6d..b83e9130c5 100644
701bb3
--- a/criu/include/restorer.h
701bb3
+++ b/criu/include/restorer.h
701bb3
@@ -69,8 +69,10 @@ struct thread_creds_args {
701bb3
 	unsigned int			secbits;
701bb3
 	char				*lsm_profile;
701bb3
 	unsigned int			*groups;
701bb3
+	char				*lsm_sockcreate;
701bb3
 
701bb3
 	unsigned long			mem_lsm_profile_pos;
701bb3
+	unsigned long			mem_lsm_sockcreate_pos;
701bb3
 	unsigned long			mem_groups_pos;
701bb3
 
701bb3
 	unsigned long			mem_pos_next;
701bb3
diff --git a/criu/lsm.c b/criu/lsm.c
701bb3
index 849ec37cde..b0ef0c396c 100644
701bb3
--- a/criu/lsm.c
701bb3
+++ b/criu/lsm.c
701bb3
@@ -98,6 +98,32 @@ static int selinux_get_label(pid_t pid, char **output)
701bb3
 	freecon(ctx);
701bb3
 	return ret;
701bb3
 }
701bb3
+
701bb3
+/*
701bb3
+ * selinux_get_sockcreate_label reads /proc/PID/attr/sockcreate
701bb3
+ * to see if the PID has a special label specified for sockets.
701bb3
+ * Most of the time this will be empty and the process will use
701bb3
+ * the process context also for sockets.
701bb3
+ */
701bb3
+static int selinux_get_sockcreate_label(pid_t pid, char **output)
701bb3
+{
701bb3
+	FILE *f;
701bb3
+
701bb3
+	f = fopen_proc(pid, "attr/sockcreate");
701bb3
+	if (!f)
701bb3
+		return -1;
701bb3
+
701bb3
+	fscanf(f, "%ms", output);
701bb3
+	/*
701bb3
+	 * No need to check the result of fscanf(). If there is something
701bb3
+	 * in /proc/PID/attr/sockcreate it will be copied to *output. If
701bb3
+	 * there is nothing it will stay NULL. So whatever fscanf() does
701bb3
+	 * it should be correct.
701bb3
+	 */
701bb3
+
701bb3
+	fclose(f);
701bb3
+	return 0;
701bb3
+}
701bb3
 #endif
701bb3
 
701bb3
 void kerndat_lsm(void)
701bb3
@@ -132,6 +158,7 @@ int collect_lsm_profile(pid_t pid, CredsEntry *ce)
701bb3
 	int ret;
701bb3
 
701bb3
 	ce->lsm_profile = NULL;
701bb3
+	ce->lsm_sockcreate = NULL;
701bb3
 
701bb3
 	switch (kdat.lsm) {
701bb3
 	case LSMTYPE__NO_LSM:
701bb3
@@ -143,6 +170,9 @@ int collect_lsm_profile(pid_t pid, CredsEntry *ce)
701bb3
 #ifdef CONFIG_HAS_SELINUX
701bb3
 	case LSMTYPE__SELINUX:
701bb3
 		ret = selinux_get_label(pid, &ce->lsm_profile);
701bb3
+		if (ret)
701bb3
+			break;
701bb3
+		ret = selinux_get_sockcreate_label(pid, &ce->lsm_sockcreate);
701bb3
 		break;
701bb3
 #endif
701bb3
 	default:
701bb3
@@ -153,6 +183,8 @@ int collect_lsm_profile(pid_t pid, CredsEntry *ce)
701bb3
 
701bb3
 	if (ce->lsm_profile)
701bb3
 		pr_info("%d has lsm profile %s\n", pid, ce->lsm_profile);
701bb3
+	if (ce->lsm_sockcreate)
701bb3
+		pr_info("%d has lsm sockcreate label %s\n", pid, ce->lsm_sockcreate);
701bb3
 
701bb3
 	return ret;
701bb3
 }
701bb3
diff --git a/criu/pie/restorer.c b/criu/pie/restorer.c
701bb3
index 6e18cc2606..4f42605a09 100644
701bb3
--- a/criu/pie/restorer.c
701bb3
+++ b/criu/pie/restorer.c
701bb3
@@ -149,7 +149,7 @@ static void sigchld_handler(int signal, siginfo_t *siginfo, void *data)
701bb3
 	sys_exit_group(1);
701bb3
 }
701bb3
 
701bb3
-static int lsm_set_label(char *label, int procfd)
701bb3
+static int lsm_set_label(char *label, char *type, int procfd)
701bb3
 {
701bb3
 	int ret = -1, len, lsmfd;
701bb3
 	char path[STD_LOG_SIMPLE_CHUNK];
701bb3
@@ -157,9 +157,9 @@ static int lsm_set_label(char *label, int procfd)
701bb3
 	if (!label)
701bb3
 		return 0;
701bb3
 
701bb3
-	pr_info("restoring lsm profile %s\n", label);
701bb3
+	pr_info("restoring lsm profile (%s) %s\n", type, label);
701bb3
 
701bb3
-	std_sprintf(path, "self/task/%ld/attr/current", sys_gettid());
701bb3
+	std_sprintf(path, "self/task/%ld/attr/%s", sys_gettid(), type);
701bb3
 
701bb3
 	lsmfd = sys_openat(procfd, path, O_WRONLY, 0);
701bb3
 	if (lsmfd < 0) {
701bb3
@@ -305,9 +305,14 @@ static int restore_creds(struct thread_creds_args *args, int procfd,
701bb3
 		 * SELinux and instead the process context is set before the
701bb3
 		 * threads are created.
701bb3
 		 */
701bb3
-		if (lsm_set_label(args->lsm_profile, procfd) < 0)
701bb3
+		if (lsm_set_label(args->lsm_profile, "current", procfd) < 0)
701bb3
 			return -1;
701bb3
 	}
701bb3
+
701bb3
+	/* Also set the sockcreate label for all threads */
701bb3
+	if (lsm_set_label(args->lsm_sockcreate, "sockcreate", procfd) < 0)
701bb3
+		return -1;
701bb3
+
701bb3
 	return 0;
701bb3
 }
701bb3
 
701bb3
@@ -1571,7 +1576,7 @@ long __export_restore_task(struct task_restore_args *args)
701bb3
 	if (args->lsm_type == LSMTYPE__SELINUX) {
701bb3
 		/* Only for SELinux */
701bb3
 		if (lsm_set_label(args->t->creds_args->lsm_profile,
701bb3
-				  args->proc_fd) < 0)
701bb3
+				  "current", args->proc_fd) < 0)
701bb3
 			goto core_restore_end;
701bb3
 	}
701bb3
 
701bb3
diff --git a/images/creds.proto b/images/creds.proto
701bb3
index 29fb8652eb..23b84c7e50 100644
701bb3
--- a/images/creds.proto
701bb3
+++ b/images/creds.proto
701bb3
@@ -20,4 +20,5 @@ message creds_entry {
701bb3
 	repeated uint32	groups	= 14;
701bb3
 
701bb3
 	optional string lsm_profile = 15;
701bb3
+	optional string lsm_sockcreate = 16;
701bb3
 }
701bb3
701bb3
From 495e6aa7ac51fcb36e6bc5f6c97f44cab7649b9c Mon Sep 17 00:00:00 2001
701bb3
From: Adrian Reber <areber@redhat.com>
701bb3
Date: Thu, 2 May 2019 13:47:29 +0000
701bb3
Subject: [PATCH 2/4] test: Verify that sockcreate does not change during
701bb3
 restore
701bb3
701bb3
This makes sure that sockcreate stays empty for selinux00 before and
701bb3
after checkpoint/restore.
701bb3
701bb3
Signed-off-by: Adrian Reber <areber@redhat.com>
701bb3
---
701bb3
 test/zdtm/static/selinux00.c | 34 ++++++++++++++++++++++++++++++++++
701bb3
 1 file changed, 34 insertions(+)
701bb3
701bb3
diff --git a/test/zdtm/static/selinux00.c b/test/zdtm/static/selinux00.c
701bb3
index dd9096a6fc..db8420eacb 100644
701bb3
--- a/test/zdtm/static/selinux00.c
701bb3
+++ b/test/zdtm/static/selinux00.c
701bb3
@@ -83,6 +83,31 @@ int checkprofile()
701bb3
 	return 0;
701bb3
 }
701bb3
 
701bb3
+int check_sockcreate()
701bb3
+{
701bb3
+	char *output = NULL;
701bb3
+	FILE *f = fopen("/proc/self/attr/sockcreate", "r");
701bb3
+	int ret = fscanf(f, "%ms", &output);
701bb3
+	fclose(f);
701bb3
+
701bb3
+	if (ret >= 1) {
701bb3
+		free(output);
701bb3
+		/* sockcreate should be empty, if fscanf found something
701bb3
+		 * it is wrong.*/
701bb3
+		fail("sockcreate should be empty\n");
701bb3
+		return -1;
701bb3
+	}
701bb3
+
701bb3
+	if (output) {
701bb3
+		free(output);
701bb3
+		/* Same here, output should still be NULL. */
701bb3
+		fail("sockcreate should be empty\n");
701bb3
+		return -1;
701bb3
+	}
701bb3
+
701bb3
+	return 0;
701bb3
+}
701bb3
+
701bb3
 int main(int argc, char **argv)
701bb3
 {
701bb3
 	test_init(argc, argv);
701bb3
@@ -95,12 +120,21 @@ int main(int argc, char **argv)
701bb3
 		return 0;
701bb3
 	}
701bb3
 
701bb3
+	if (check_sockcreate())
701bb3
+		return -1;
701bb3
+
701bb3
 	if (setprofile())
701bb3
 		return -1;
701bb3
 
701bb3
+	if (check_sockcreate())
701bb3
+		return -1;
701bb3
+
701bb3
 	test_daemon();
701bb3
 	test_waitsig();
701bb3
 
701bb3
+	if (check_sockcreate())
701bb3
+		return -1;
701bb3
+
701bb3
 	if (checkprofile() == 0)
701bb3
 		pass();
701bb3
 
701bb3
701bb3
From fe52cf66b38a261846ff40fc425085724b2acc15 Mon Sep 17 00:00:00 2001
701bb3
From: Adrian Reber <areber@redhat.com>
701bb3
Date: Mon, 29 Apr 2019 15:21:59 +0200
701bb3
Subject: [PATCH 3/4] sockets: dump and restore xattr security labels
701bb3
701bb3
Restoring a SELinux process also requires to correctly label sockets.
701bb3
701bb3
During checkpointing fgetxattr() is used to retrieve the
701bb3
"security.selinux" xattr and during restore setsockcreatecon() is used
701bb3
before a socket is created.
701bb3
701bb3
Previous commits are already restoring the sockcreate SELinux setting if
701bb3
set by the process.
701bb3
701bb3
Signed-off-by: Adrian Reber <areber@redhat.com>
701bb3
---
701bb3
 criu/include/lsm.h  | 18 +++++++++++++++
701bb3
 criu/lsm.c          | 56 +++++++++++++++++++++++++++++++++++++++++++++
701bb3
 criu/sk-inet.c      | 12 ++++++++++
701bb3
 criu/sockets.c      |  4 ++++
701bb3
 images/fdinfo.proto |  1 +
701bb3
 5 files changed, 91 insertions(+)
701bb3
701bb3
diff --git a/criu/include/lsm.h b/criu/include/lsm.h
701bb3
index b4fce13039..3b82712829 100644
701bb3
--- a/criu/include/lsm.h
701bb3
+++ b/criu/include/lsm.h
701bb3
@@ -3,6 +3,7 @@
701bb3
 
701bb3
 #include "images/inventory.pb-c.h"
701bb3
 #include "images/creds.pb-c.h"
701bb3
+#include "images/fdinfo.pb-c.h"
701bb3
 
701bb3
 #define AA_SECURITYFS_PATH "/sys/kernel/security/apparmor"
701bb3
 
701bb3
@@ -34,4 +35,21 @@ int validate_lsm(char *profile);
701bb3
 int render_lsm_profile(char *profile, char **val);
701bb3
 
701bb3
 extern int lsm_check_opts(void);
701bb3
+
701bb3
+#ifdef CONFIG_HAS_SELINUX
701bb3
+int dump_xattr_security_selinux(int fd, FdinfoEntry *e);
701bb3
+int run_setsockcreatecon(FdinfoEntry *e);
701bb3
+int reset_setsockcreatecon();
701bb3
+#else
701bb3
+static inline int dump_xattr_security_selinux(int fd, FdinfoEntry *e) {
701bb3
+	return 0;
701bb3
+}
701bb3
+static inline int run_setsockcreatecon(FdinfoEntry *e) {
701bb3
+	return 0;
701bb3
+}
701bb3
+static inline int reset_setsockcreatecon() {
701bb3
+	return 0;
701bb3
+}
701bb3
+#endif
701bb3
+
701bb3
 #endif /* __CR_LSM_H__ */
701bb3
diff --git a/criu/lsm.c b/criu/lsm.c
701bb3
index b0ef0c396c..ef6ba112b3 100644
701bb3
--- a/criu/lsm.c
701bb3
+++ b/criu/lsm.c
701bb3
@@ -3,6 +3,7 @@
701bb3
 #include <stdlib.h>
701bb3
 #include <fcntl.h>
701bb3
 #include <sys/types.h>
701bb3
+#include <sys/xattr.h>
701bb3
 #include <unistd.h>
701bb3
 
701bb3
 #include "common/config.h"
701bb3
@@ -11,10 +12,12 @@
701bb3
 #include "util.h"
701bb3
 #include "cr_options.h"
701bb3
 #include "lsm.h"
701bb3
+#include "fdstore.h"
701bb3
 
701bb3
 #include "protobuf.h"
701bb3
 #include "images/inventory.pb-c.h"
701bb3
 #include "images/creds.pb-c.h"
701bb3
+#include "images/fdinfo.pb-c.h"
701bb3
 
701bb3
 #ifdef CONFIG_HAS_SELINUX
701bb3
 #include <selinux/selinux.h>
701bb3
@@ -124,6 +127,59 @@ static int selinux_get_sockcreate_label(pid_t pid, char **output)
701bb3
 	fclose(f);
701bb3
 	return 0;
701bb3
 }
701bb3
+
701bb3
+int reset_setsockcreatecon()
701bb3
+{
701bb3
+	return setsockcreatecon_raw(NULL);
701bb3
+}
701bb3
+
701bb3
+int run_setsockcreatecon(FdinfoEntry *e)
701bb3
+{
701bb3
+	char *ctx = NULL;
701bb3
+
701bb3
+	/* Currently this only works for SELinux. */
701bb3
+	if (kdat.lsm != LSMTYPE__SELINUX)
701bb3
+		return 0;
701bb3
+
701bb3
+	ctx = e->xattr_security_selinux;
701bb3
+	/* Writing to the FD using fsetxattr() did not work for some reason. */
701bb3
+	return setsockcreatecon_raw(ctx);
701bb3
+}
701bb3
+
701bb3
+int dump_xattr_security_selinux(int fd, FdinfoEntry *e)
701bb3
+{
701bb3
+	char *ctx = NULL;
701bb3
+	int len;
701bb3
+	int ret;
701bb3
+
701bb3
+	/* Currently this only works for SELinux. */
701bb3
+	if (kdat.lsm != LSMTYPE__SELINUX)
701bb3
+		return 0;
701bb3
+
701bb3
+	/* Get the size of the xattr. */
701bb3
+	len = fgetxattr(fd, "security.selinux", ctx, 0);
701bb3
+	if (len == -1) {
701bb3
+		pr_err("Reading xattr %s to FD %d failed\n", ctx, fd);
701bb3
+		return -1;
701bb3
+	}
701bb3
+
701bb3
+	ctx = xmalloc(len);
701bb3
+	if (!ctx) {
701bb3
+		pr_err("xmalloc to read xattr for FD %d failed\n", fd);
701bb3
+		return -1;
701bb3
+	}
701bb3
+
701bb3
+	ret = fgetxattr(fd, "security.selinux", ctx, len);
701bb3
+	if (len != ret) {
701bb3
+		pr_err("Reading xattr %s to FD %d failed\n", ctx, fd);
701bb3
+		return -1;
701bb3
+	}
701bb3
+
701bb3
+	e->xattr_security_selinux = ctx;
701bb3
+
701bb3
+	return 0;
701bb3
+}
701bb3
+
701bb3
 #endif
701bb3
 
701bb3
 void kerndat_lsm(void)
701bb3
diff --git a/criu/sk-inet.c b/criu/sk-inet.c
701bb3
index 60ee4c3155..ca5c9bf2cd 100644
701bb3
--- a/criu/sk-inet.c
701bb3
+++ b/criu/sk-inet.c
701bb3
@@ -23,6 +23,9 @@
701bb3
 #include "files.h"
701bb3
 #include "image.h"
701bb3
 #include "log.h"
701bb3
+#include "lsm.h"
701bb3
+#include "kerndat.h"
701bb3
+#include "pstree.h"
701bb3
 #include "rst-malloc.h"
701bb3
 #include "sockets.h"
701bb3
 #include "sk-inet.h"
701bb3
@@ -30,6 +33,8 @@
701bb3
 #include "util.h"
701bb3
 #include "namespaces.h"
701bb3
 
701bb3
+#include "images/inventory.pb-c.h"
701bb3
+
701bb3
 #undef  LOG_PREFIX
701bb3
 #define LOG_PREFIX "inet: "
701bb3
 
701bb3
@@ -804,12 +809,18 @@ static int open_inet_sk(struct file_desc *d, int *new_fd)
701bb3
 	if (set_netns(ie->ns_id))
701bb3
 		return -1;
701bb3
 
701bb3
+	if (run_setsockcreatecon(fle->fe))
701bb3
+		return -1;
701bb3
+
701bb3
 	sk = socket(ie->family, ie->type, ie->proto);
701bb3
 	if (sk < 0) {
701bb3
 		pr_perror("Can't create inet socket");
701bb3
 		return -1;
701bb3
 	}
701bb3
 
701bb3
+	if (reset_setsockcreatecon())
701bb3
+		return -1;
701bb3
+
701bb3
 	if (ie->v6only) {
701bb3
 		if (restore_opt(sk, SOL_IPV6, IPV6_V6ONLY, &yes) == -1)
701bb3
 			goto err;
701bb3
@@ -895,6 +906,7 @@ static int open_inet_sk(struct file_desc *d, int *new_fd)
701bb3
 	}
701bb3
 
701bb3
 	*new_fd = sk;
701bb3
+
701bb3
 	return 1;
701bb3
 err:
701bb3
 	close(sk);
701bb3
diff --git a/criu/sockets.c b/criu/sockets.c
701bb3
index 30072ac737..7f7453ca1d 100644
701bb3
--- a/criu/sockets.c
701bb3
+++ b/criu/sockets.c
701bb3
@@ -22,6 +22,7 @@
701bb3
 #include "util-pie.h"
701bb3
 #include "sk-packet.h"
701bb3
 #include "namespaces.h"
701bb3
+#include "lsm.h"
701bb3
 #include "net.h"
701bb3
 #include "xmalloc.h"
701bb3
 #include "fs-magic.h"
701bb3
@@ -663,6 +664,9 @@ int dump_socket(struct fd_parms *p, int lfd, FdinfoEntry *e)
701bb3
 	int family;
701bb3
 	const struct fdtype_ops *ops;
701bb3
 
701bb3
+	if (dump_xattr_security_selinux(lfd, e))
701bb3
+		return -1;
701bb3
+
701bb3
 	if (dump_opt(lfd, SOL_SOCKET, SO_DOMAIN, &family))
701bb3
 		return -1;
701bb3
 
701bb3
diff --git a/images/fdinfo.proto b/images/fdinfo.proto
701bb3
index ed82ceffe7..77e375aa94 100644
701bb3
--- a/images/fdinfo.proto
701bb3
+++ b/images/fdinfo.proto
701bb3
@@ -47,6 +47,7 @@ message fdinfo_entry {
701bb3
 	required uint32		flags	= 2;
701bb3
 	required fd_types	type	= 3;
701bb3
 	required uint32		fd	= 4;
701bb3
+	optional string		xattr_security_selinux = 5;
701bb3
 }
701bb3
 
701bb3
 message file_entry {
701bb3
701bb3
From ba42d30fad82f17a66617a33f03d3da05cc73bfe Mon Sep 17 00:00:00 2001
701bb3
From: Adrian Reber <areber@redhat.com>
701bb3
Date: Tue, 30 Apr 2019 09:47:32 +0000
701bb3
Subject: [PATCH 4/4] selinux: add socket label test
701bb3
701bb3
This adds two more SELinux test to verfy that checkpointing and
701bb3
restoring SELinux socket labels works correctly, if the process uses
701bb3
setsockcreatecon() or if the process leaves the default context for
701bb3
newly created sockets.
701bb3
701bb3
Signed-off-by: Adrian Reber <areber@redhat.com>
701bb3
---
701bb3
 test/zdtm/static/Makefile            |   3 +
701bb3
 test/zdtm/static/selinux01.c         | 200 +++++++++++++++++++++++++++
701bb3
 test/zdtm/static/selinux01.checkskip |   1 +
701bb3
 test/zdtm/static/selinux01.desc      |   1 +
701bb3
 test/zdtm/static/selinux01.hook      |   1 +
701bb3
 test/zdtm/static/selinux02.c         |   1 +
701bb3
 test/zdtm/static/selinux02.checkskip |   1 +
701bb3
 test/zdtm/static/selinux02.desc      |   1 +
701bb3
 test/zdtm/static/selinux02.hook      |   1 +
701bb3
 9 files changed, 210 insertions(+)
701bb3
 create mode 100644 test/zdtm/static/selinux01.c
701bb3
 create mode 120000 test/zdtm/static/selinux01.checkskip
701bb3
 create mode 120000 test/zdtm/static/selinux01.desc
701bb3
 create mode 120000 test/zdtm/static/selinux01.hook
701bb3
 create mode 120000 test/zdtm/static/selinux02.c
701bb3
 create mode 120000 test/zdtm/static/selinux02.checkskip
701bb3
 create mode 120000 test/zdtm/static/selinux02.desc
701bb3
 create mode 120000 test/zdtm/static/selinux02.hook
701bb3
701bb3
diff --git a/test/zdtm/static/Makefile b/test/zdtm/static/Makefile
701bb3
index 8e3f39276a..1ffaa90394 100644
701bb3
--- a/test/zdtm/static/Makefile
701bb3
+++ b/test/zdtm/static/Makefile
701bb3
@@ -211,6 +211,8 @@ TST_NOFILE	:=				\
701bb3
 		thp_disable			\
701bb3
 		pid_file			\
701bb3
 		selinux00			\
701bb3
+		selinux01			\
701bb3
+		selinux02			\
701bb3
 #		jobctl00			\
701bb3
 
701bb3
 ifneq ($(SRCARCH),arm)
701bb3
@@ -513,6 +515,7 @@ unlink_fstat041:		CFLAGS += -DUNLINK_FSTAT041 -DUNLINK_FSTAT04
701bb3
 ghost_holes01:		CFLAGS += -DTAIL_HOLE
701bb3
 ghost_holes02:		CFLAGS += -DHEAD_HOLE
701bb3
 sk-freebind-false:	CFLAGS += -DZDTM_FREEBIND_FALSE
701bb3
+selinux02:		CFLAGS += -DUSING_SOCKCREATE
701bb3
 stopped01:		CFLAGS += -DZDTM_STOPPED_KILL
701bb3
 stopped02:		CFLAGS += -DZDTM_STOPPED_TKILL
701bb3
 stopped12:		CFLAGS += -DZDTM_STOPPED_KILL -DZDTM_STOPPED_TKILL
701bb3
diff --git a/test/zdtm/static/selinux01.c b/test/zdtm/static/selinux01.c
701bb3
new file mode 100644
701bb3
index 0000000000..9966455c47
701bb3
--- /dev/null
701bb3
+++ b/test/zdtm/static/selinux01.c
701bb3
@@ -0,0 +1,200 @@
701bb3
+#include <unistd.h>
701bb3
+#include <stdio.h>
701bb3
+#include <stdlib.h>
701bb3
+#include <string.h>
701bb3
+#include <fcntl.h>
701bb3
+#include <sys/stat.h>
701bb3
+#include <sys/types.h>
701bb3
+#include <sys/mount.h>
701bb3
+#include <sys/socket.h>
701bb3
+#include <sys/xattr.h>
701bb3
+#include <linux/limits.h>
701bb3
+#include <signal.h>
701bb3
+#include "zdtmtst.h"
701bb3
+
701bb3
+/* Enabling the right policy happens in selinux00.hook and selinx00.checkskip */
701bb3
+
701bb3
+const char *test_doc	= "Check that a SELinux socket context is restored";
701bb3
+const char *test_author	= "Adrian Reber <areber@redhat.com>";
701bb3
+
701bb3
+/* This is all based on Tycho's apparmor code */
701bb3
+
701bb3
+#define CONTEXT "unconfined_u:unconfined_r:unconfined_dbusd_t:s0"
701bb3
+
701bb3
+/*
701bb3
+ * This is used to store the state of SELinux. For this test
701bb3
+ * SELinux is switched to permissive mode and later the previous
701bb3
+ * SELinux state is restored.
701bb3
+ */
701bb3
+char state;
701bb3
+
701bb3
+int check_for_selinux()
701bb3
+{
701bb3
+	if (access("/sys/fs/selinux", F_OK) == 0)
701bb3
+		return 0;
701bb3
+	return 1;
701bb3
+}
701bb3
+
701bb3
+int setprofile()
701bb3
+{
701bb3
+	int fd, len;
701bb3
+
701bb3
+	fd = open("/proc/self/attr/current", O_WRONLY);
701bb3
+	if (fd < 0) {
701bb3
+		fail("Could not open /proc/self/attr/current\n");
701bb3
+		return -1;
701bb3
+	}
701bb3
+
701bb3
+	len = write(fd, CONTEXT, strlen(CONTEXT));
701bb3
+	close(fd);
701bb3
+
701bb3
+	if (len < 0) {
701bb3
+		fail("Could not write context\n");
701bb3
+		return -1;
701bb3
+	}
701bb3
+
701bb3
+	return 0;
701bb3
+}
701bb3
+
701bb3
+int set_sockcreate()
701bb3
+{
701bb3
+	int fd, len;
701bb3
+
701bb3
+	fd = open("/proc/self/attr/sockcreate", O_WRONLY);
701bb3
+	if (fd < 0) {
701bb3
+		fail("Could not open /proc/self/attr/sockcreate\n");
701bb3
+		return -1;
701bb3
+	}
701bb3
+
701bb3
+	len = write(fd, CONTEXT, strlen(CONTEXT));
701bb3
+	close(fd);
701bb3
+
701bb3
+	if (len < 0) {
701bb3
+		fail("Could not write context\n");
701bb3
+		return -1;
701bb3
+	}
701bb3
+
701bb3
+	return 0;
701bb3
+}
701bb3
+
701bb3
+int check_sockcreate()
701bb3
+{
701bb3
+	int fd;
701bb3
+	char context[1024];
701bb3
+	int len;
701bb3
+
701bb3
+
701bb3
+	fd = open("/proc/self/attr/sockcreate", O_RDONLY);
701bb3
+	if (fd < 0) {
701bb3
+		fail("Could not open /proc/self/attr/sockcreate\n");
701bb3
+		return -1;
701bb3
+	}
701bb3
+
701bb3
+	len = read(fd, context, strlen(CONTEXT));
701bb3
+	close(fd);
701bb3
+	if (len != strlen(CONTEXT)) {
701bb3
+		fail("SELinux context has unexpected length %d, expected %zd\n",
701bb3
+			len, strlen(CONTEXT));
701bb3
+		return -1;
701bb3
+	}
701bb3
+
701bb3
+	if (strncmp(context, CONTEXT, strlen(CONTEXT)) != 0) {
701bb3
+		fail("Wrong SELinux context %s expected %s\n", context, CONTEXT);
701bb3
+		return -1;
701bb3
+	}
701bb3
+
701bb3
+	return 0;
701bb3
+}
701bb3
+
701bb3
+int check_sockcreate_empty()
701bb3
+{
701bb3
+	char *output = NULL;
701bb3
+	FILE *f = fopen("/proc/self/attr/sockcreate", "r");
701bb3
+	int ret = fscanf(f, "%ms", &output);
701bb3
+	fclose(f);
701bb3
+
701bb3
+	if (ret >= 1) {
701bb3
+		free(output);
701bb3
+		/* sockcreate should be empty, if fscanf found something
701bb3
+		 * it is wrong.*/
701bb3
+		fail("sockcreate should be empty\n");
701bb3
+		return -1;
701bb3
+	}
701bb3
+
701bb3
+	if (output) {
701bb3
+		free(output);
701bb3
+		/* Same here, output should still be NULL. */
701bb3
+		fail("sockcreate should be empty\n");
701bb3
+		return -1;
701bb3
+	}
701bb3
+
701bb3
+	return 0;
701bb3
+}
701bb3
+
701bb3
+int main(int argc, char **argv)
701bb3
+{
701bb3
+	char ctx[1024];
701bb3
+	test_init(argc, argv);
701bb3
+
701bb3
+	if (check_for_selinux()) {
701bb3
+		skip("SELinux not found on this system.");
701bb3
+		test_daemon();
701bb3
+		test_waitsig();
701bb3
+		pass();
701bb3
+		return 0;
701bb3
+	}
701bb3
+
701bb3
+#ifdef USING_SOCKCREATE
701bb3
+	if (set_sockcreate())
701bb3
+		return -1;
701bb3
+#else
701bb3
+	if (check_sockcreate_empty())
701bb3
+		return -1;
701bb3
+
701bb3
+	if (setprofile())
701bb3
+		return -1;
701bb3
+
701bb3
+	if (check_sockcreate_empty())
701bb3
+		return -1;
701bb3
+#endif
701bb3
+
701bb3
+	/* Open our test socket */
701bb3
+	int sk = socket(AF_INET, SOCK_STREAM, 0);
701bb3
+	memset(ctx, 0, 1024);
701bb3
+	/* Read out the socket label */
701bb3
+	if (fgetxattr(sk, "security.selinux", ctx, 1024) == -1) {
701bb3
+		fail("Reading xattr 'security.selinux' failed.\n");
701bb3
+		return -1;
701bb3
+	}
701bb3
+	if (strncmp(ctx, CONTEXT, strlen(CONTEXT)) != 0) {
701bb3
+		fail("Wrong SELinux context %s expected %s\n", ctx, CONTEXT);
701bb3
+		return -1;
701bb3
+	}
701bb3
+	memset(ctx, 0, 1024);
701bb3
+
701bb3
+	test_daemon();
701bb3
+	test_waitsig();
701bb3
+
701bb3
+	/* Read out the socket label again */
701bb3
+
701bb3
+	if (fgetxattr(sk, "security.selinux", ctx, 1024) == -1) {
701bb3
+		fail("Reading xattr 'security.selinux' failed.\n");
701bb3
+		return -1;
701bb3
+	}
701bb3
+	if (strncmp(ctx, CONTEXT, strlen(CONTEXT)) != 0) {
701bb3
+		fail("Wrong SELinux context %s expected %s\n", ctx, CONTEXT);
701bb3
+		return -1;
701bb3
+	}
701bb3
+
701bb3
+#ifdef USING_SOCKCREATE
701bb3
+	if (check_sockcreate())
701bb3
+		return -1;
701bb3
+#else
701bb3
+	if (check_sockcreate_empty())
701bb3
+		return -1;
701bb3
+#endif
701bb3
+
701bb3
+	pass();
701bb3
+
701bb3
+	return 0;
701bb3
+}
701bb3
diff --git a/test/zdtm/static/selinux01.checkskip b/test/zdtm/static/selinux01.checkskip
701bb3
new file mode 120000
701bb3
index 0000000000..e8a172479e
701bb3
--- /dev/null
701bb3
+++ b/test/zdtm/static/selinux01.checkskip
701bb3
@@ -0,0 +1 @@
701bb3
+selinux00.checkskip
701bb3
\ No newline at end of file
701bb3
diff --git a/test/zdtm/static/selinux01.desc b/test/zdtm/static/selinux01.desc
701bb3
new file mode 120000
701bb3
index 0000000000..2d2961a764
701bb3
--- /dev/null
701bb3
+++ b/test/zdtm/static/selinux01.desc
701bb3
@@ -0,0 +1 @@
701bb3
+selinux00.desc
701bb3
\ No newline at end of file
701bb3
diff --git a/test/zdtm/static/selinux01.hook b/test/zdtm/static/selinux01.hook
701bb3
new file mode 120000
701bb3
index 0000000000..dd7ed6bb33
701bb3
--- /dev/null
701bb3
+++ b/test/zdtm/static/selinux01.hook
701bb3
@@ -0,0 +1 @@
701bb3
+selinux00.hook
701bb3
\ No newline at end of file
701bb3
diff --git a/test/zdtm/static/selinux02.c b/test/zdtm/static/selinux02.c
701bb3
new file mode 120000
701bb3
index 0000000000..5702677858
701bb3
--- /dev/null
701bb3
+++ b/test/zdtm/static/selinux02.c
701bb3
@@ -0,0 +1 @@
701bb3
+selinux01.c
701bb3
\ No newline at end of file
701bb3
diff --git a/test/zdtm/static/selinux02.checkskip b/test/zdtm/static/selinux02.checkskip
701bb3
new file mode 120000
701bb3
index 0000000000..2696e6e3de
701bb3
--- /dev/null
701bb3
+++ b/test/zdtm/static/selinux02.checkskip
701bb3
@@ -0,0 +1 @@
701bb3
+selinux01.checkskip
701bb3
\ No newline at end of file
701bb3
diff --git a/test/zdtm/static/selinux02.desc b/test/zdtm/static/selinux02.desc
701bb3
new file mode 120000
701bb3
index 0000000000..9c6802c4da
701bb3
--- /dev/null
701bb3
+++ b/test/zdtm/static/selinux02.desc
701bb3
@@ -0,0 +1 @@
701bb3
+selinux01.desc
701bb3
\ No newline at end of file
701bb3
diff --git a/test/zdtm/static/selinux02.hook b/test/zdtm/static/selinux02.hook
701bb3
new file mode 120000
701bb3
index 0000000000..e3ea0a6c80
701bb3
--- /dev/null
701bb3
+++ b/test/zdtm/static/selinux02.hook
701bb3
@@ -0,0 +1 @@
701bb3
+selinux01.hook
701bb3
\ No newline at end of file