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