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