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