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