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