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