ytakagi / rpms / libselinux

Forked from rpms/libselinux 4 years ago
Clone

Blame SOURCES/0002-Verify-context-input-to-funtions-to-make-sure-the-co.patch

b2442d
From ad3d3a0bf819f5895a6884357c2d0e18ea1ef314 Mon Sep 17 00:00:00 2001
b2442d
From: Dan Walsh <dwalsh@redhat.com>
b2442d
Date: Mon, 23 Dec 2013 09:50:54 -0500
b2442d
Subject: [PATCH 2/5] Verify context input to funtions to make sure the context
b2442d
 field is not null.
b2442d
b2442d
Return errno EINVAL, to prevent segfault.
b2442d
b2442d
Rejected by upstream https://marc.info/?l=selinux&m=145036088424584&w=2
b2442d
b2442d
FIXME: use __attribute__(nonnull (arg-index, ...))
b2442d
---
b2442d
 libselinux/src/avc_sidtab.c           | 5 +++++
b2442d
 libselinux/src/canonicalize_context.c | 5 +++++
b2442d
 libselinux/src/check_context.c        | 5 +++++
b2442d
 libselinux/src/compute_av.c           | 5 +++++
b2442d
 libselinux/src/compute_create.c       | 5 +++++
b2442d
 libselinux/src/compute_member.c       | 5 +++++
b2442d
 libselinux/src/compute_relabel.c      | 5 +++++
b2442d
 libselinux/src/compute_user.c         | 5 +++++
b2442d
 libselinux/src/fsetfilecon.c          | 8 ++++++--
b2442d
 libselinux/src/lsetfilecon.c          | 9 +++++++--
b2442d
 libselinux/src/setfilecon.c           | 8 ++++++--
b2442d
 11 files changed, 59 insertions(+), 6 deletions(-)
b2442d
b2442d
diff --git a/libselinux/src/avc_sidtab.c b/libselinux/src/avc_sidtab.c
b2442d
index 9669264d..c7754305 100644
b2442d
--- a/libselinux/src/avc_sidtab.c
b2442d
+++ b/libselinux/src/avc_sidtab.c
b2442d
@@ -81,6 +81,11 @@ sidtab_context_to_sid(struct sidtab *s,
b2442d
 	int hvalue, rc = 0;
b2442d
 	struct sidtab_node *cur;
b2442d
 
b2442d
+	if (! ctx) {
b2442d
+		errno=EINVAL;
b2442d
+		return -1;
b2442d
+	}
b2442d
+
b2442d
 	*sid = NULL;
b2442d
 	hvalue = sidtab_hash(ctx);
b2442d
 
b2442d
diff --git a/libselinux/src/canonicalize_context.c b/libselinux/src/canonicalize_context.c
b2442d
index ba4c9a2c..c8158725 100644
b2442d
--- a/libselinux/src/canonicalize_context.c
b2442d
+++ b/libselinux/src/canonicalize_context.c
b2442d
@@ -17,6 +17,11 @@ int security_canonicalize_context_raw(const char * con,
b2442d
 	size_t size;
b2442d
 	int fd, ret;
b2442d
 
b2442d
+	if (! con) {
b2442d
+		errno=EINVAL;
b2442d
+		return -1;
b2442d
+	}
b2442d
+
b2442d
 	if (!selinux_mnt) {
b2442d
 		errno = ENOENT;
b2442d
 		return -1;
b2442d
diff --git a/libselinux/src/check_context.c b/libselinux/src/check_context.c
b2442d
index 8a7997f0..5be84348 100644
b2442d
--- a/libselinux/src/check_context.c
b2442d
+++ b/libselinux/src/check_context.c
b2442d
@@ -14,6 +14,11 @@ int security_check_context_raw(const char * con)
b2442d
 	char path[PATH_MAX];
b2442d
 	int fd, ret;
b2442d
 
b2442d
+	if (! con) {
b2442d
+		errno=EINVAL;
b2442d
+		return -1;
b2442d
+	}
b2442d
+
b2442d
 	if (!selinux_mnt) {
b2442d
 		errno = ENOENT;
b2442d
 		return -1;
b2442d
diff --git a/libselinux/src/compute_av.c b/libselinux/src/compute_av.c
b2442d
index a47cffe9..6d285a2e 100644
b2442d
--- a/libselinux/src/compute_av.c
b2442d
+++ b/libselinux/src/compute_av.c
b2442d
@@ -27,6 +27,11 @@ int security_compute_av_flags_raw(const char * scon,
b2442d
 		return -1;
b2442d
 	}
b2442d
 
b2442d
+	if ((! scon) || (! tcon)) {
b2442d
+		errno=EINVAL;
b2442d
+		return -1;
b2442d
+	}
b2442d
+
b2442d
 	snprintf(path, sizeof path, "%s/access", selinux_mnt);
b2442d
 	fd = open(path, O_RDWR | O_CLOEXEC);
b2442d
 	if (fd < 0)
b2442d
diff --git a/libselinux/src/compute_create.c b/libselinux/src/compute_create.c
b2442d
index 0975aeac..3e6a48c1 100644
b2442d
--- a/libselinux/src/compute_create.c
b2442d
+++ b/libselinux/src/compute_create.c
b2442d
@@ -64,6 +64,11 @@ int security_compute_create_name_raw(const char * scon,
b2442d
 		return -1;
b2442d
 	}
b2442d
 
b2442d
+	if ((! scon) || (! tcon)) {
b2442d
+		errno=EINVAL;
b2442d
+		return -1;
b2442d
+	}
b2442d
+
b2442d
 	snprintf(path, sizeof path, "%s/create", selinux_mnt);
b2442d
 	fd = open(path, O_RDWR | O_CLOEXEC);
b2442d
 	if (fd < 0)
b2442d
diff --git a/libselinux/src/compute_member.c b/libselinux/src/compute_member.c
b2442d
index 4e2d221e..d1dd9772 100644
b2442d
--- a/libselinux/src/compute_member.c
b2442d
+++ b/libselinux/src/compute_member.c
b2442d
@@ -25,6 +25,11 @@ int security_compute_member_raw(const char * scon,
b2442d
 		return -1;
b2442d
 	}
b2442d
 
b2442d
+	if ((! scon) || (! tcon)) {
b2442d
+		errno=EINVAL;
b2442d
+		return -1;
b2442d
+	}
b2442d
+
b2442d
 	snprintf(path, sizeof path, "%s/member", selinux_mnt);
b2442d
 	fd = open(path, O_RDWR | O_CLOEXEC);
b2442d
 	if (fd < 0)
b2442d
diff --git a/libselinux/src/compute_relabel.c b/libselinux/src/compute_relabel.c
b2442d
index 49f77ef3..c3db7c0a 100644
b2442d
--- a/libselinux/src/compute_relabel.c
b2442d
+++ b/libselinux/src/compute_relabel.c
b2442d
@@ -25,6 +25,11 @@ int security_compute_relabel_raw(const char * scon,
b2442d
 		return -1;
b2442d
 	}
b2442d
 
b2442d
+	if ((! scon) || (! tcon)) {
b2442d
+		errno=EINVAL;
b2442d
+		return -1;
b2442d
+	}
b2442d
+
b2442d
 	snprintf(path, sizeof path, "%s/relabel", selinux_mnt);
b2442d
 	fd = open(path, O_RDWR | O_CLOEXEC);
b2442d
 	if (fd < 0)
b2442d
diff --git a/libselinux/src/compute_user.c b/libselinux/src/compute_user.c
b2442d
index 7b881215..401fd107 100644
b2442d
--- a/libselinux/src/compute_user.c
b2442d
+++ b/libselinux/src/compute_user.c
b2442d
@@ -24,6 +24,11 @@ int security_compute_user_raw(const char * scon,
b2442d
 		return -1;
b2442d
 	}
b2442d
 
b2442d
+	if (! scon) {
b2442d
+		errno=EINVAL;
b2442d
+		return -1;
b2442d
+	}
b2442d
+
b2442d
 	snprintf(path, sizeof path, "%s/user", selinux_mnt);
b2442d
 	fd = open(path, O_RDWR | O_CLOEXEC);
b2442d
 	if (fd < 0)
b2442d
diff --git a/libselinux/src/fsetfilecon.c b/libselinux/src/fsetfilecon.c
b2442d
index 52707d05..0cbe12d8 100644
b2442d
--- a/libselinux/src/fsetfilecon.c
b2442d
+++ b/libselinux/src/fsetfilecon.c
b2442d
@@ -9,8 +9,12 @@
b2442d
 
b2442d
 int fsetfilecon_raw(int fd, const char * context)
b2442d
 {
b2442d
-	int rc = fsetxattr(fd, XATTR_NAME_SELINUX, context, strlen(context) + 1,
b2442d
-			 0);
b2442d
+	int rc;
b2442d
+	if (! context) {
b2442d
+		errno=EINVAL;
b2442d
+		return -1;
b2442d
+	}
b2442d
+	rc = fsetxattr(fd, XATTR_NAME_SELINUX, context, strlen(context) + 1, 0);
b2442d
 	if (rc < 0 && errno == ENOTSUP) {
b2442d
 		char * ccontext = NULL;
b2442d
 		int err = errno;
b2442d
diff --git a/libselinux/src/lsetfilecon.c b/libselinux/src/lsetfilecon.c
b2442d
index 1d3b28a1..ea6d70b7 100644
b2442d
--- a/libselinux/src/lsetfilecon.c
b2442d
+++ b/libselinux/src/lsetfilecon.c
b2442d
@@ -9,8 +9,13 @@
b2442d
 
b2442d
 int lsetfilecon_raw(const char *path, const char * context)
b2442d
 {
b2442d
-	int rc = lsetxattr(path, XATTR_NAME_SELINUX, context, strlen(context) + 1,
b2442d
-			 0);
b2442d
+	int rc;
b2442d
+	if (! context) {
b2442d
+		errno=EINVAL;
b2442d
+		return -1;
b2442d
+	}
b2442d
+
b2442d
+	rc = lsetxattr(path, XATTR_NAME_SELINUX, context, strlen(context) + 1, 0);
b2442d
 	if (rc < 0 && errno == ENOTSUP) {
b2442d
 		char * ccontext = NULL;
b2442d
 		int err = errno;
b2442d
diff --git a/libselinux/src/setfilecon.c b/libselinux/src/setfilecon.c
b2442d
index d05969c6..3f0200e8 100644
b2442d
--- a/libselinux/src/setfilecon.c
b2442d
+++ b/libselinux/src/setfilecon.c
b2442d
@@ -9,8 +9,12 @@
b2442d
 
b2442d
 int setfilecon_raw(const char *path, const char * context)
b2442d
 {
b2442d
-	int rc = setxattr(path, XATTR_NAME_SELINUX, context, strlen(context) + 1,
b2442d
-			0);
b2442d
+	int rc;
b2442d
+	if (! context) {
b2442d
+		errno=EINVAL;
b2442d
+		return -1;
b2442d
+	}
b2442d
+	rc = setxattr(path, XATTR_NAME_SELINUX, context, strlen(context) + 1, 0);
b2442d
 	if (rc < 0 && errno == ENOTSUP) {
b2442d
 		char * ccontext = NULL;
b2442d
 		int err = errno;
b2442d
-- 
b2442d
2.21.0
b2442d