Skip to contentSkip to conversation

SELinux: allow userspace to read policy back out of the kernel

PATCH In review Eric Paris 2 participants 21 messages Jul 26, 2010 – Oct 13, 2010 SELinux security module

lore
Share & tools
lore

Eric Paris

author
#0 lore raw

PATCH 1/2 SELinux: allow userspace to read policy back out of the kernel

There is interest in being able to see what the actual policy is that was loaded into the kernel. The patch creates a new selinuxfs file /selinux/policy which can be read by userspace. The actual policy that is loaded into the kernel will be written back out to userspace.

---

 include/linux/kernel.h              |    1 
 security/selinux/include/security.h |    2 
 security/selinux/selinuxfs.c        |   92 ++++
 security/selinux/ss/avtab.c         |   42 ++
 security/selinux/ss/avtab.h         |    2 
 security/selinux/ss/conditional.c   |  123 +++++
 security/selinux/ss/conditional.h   |    2 
 security/selinux/ss/ebitmap.c       |   81 +++
 security/selinux/ss/ebitmap.h       |    1 
 security/selinux/ss/policydb.c      |  838 +++++++++++++++++++++++++++++++++++
 security/selinux/ss/policydb.h      |   23 +
 security/selinux/ss/services.c      |   54 ++
 12 files changed, 1259 insertions(+), 2 deletions(-)
include/linux/kernel.h +1 -0
index 7d5b10f..d6092fd 100644
@@ -59,6 +59,7 @@ extern const char linux_proc_banner[];
 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
+#define rounddown(x, y) ((x) - ((x) % (y)))
 #define DIV_ROUND_CLOSEST(x, divisor)(			\
 {							\
 	typeof(divisor) __divisor = divisor;		\
security/selinux/include/security.h +2 -0
index 1f7c249..8a4501b 100644
@@ -82,6 +82,8 @@ extern int selinux_policycap_openperm;
 int security_mls_enabled(void);
 
 int security_load_policy(void *data, size_t len);
+int security_read_policy(void **data, ssize_t *len);
+size_t security_policydb_len(void);
 
 int security_policycap_supported(unsigned int req_cap);
 
security/selinux/selinuxfs.c +92 -0

110 lines not on this page · raw message

security/selinux/ss/avtab.c +42 -0

48 lines not on this page · raw message

security/selinux/ss/avtab.h +2 -0
index cd4f734..de1f8b6 100644
@@ -71,6 +71,8 @@ int avtab_read_item(struct avtab *a, void *fp, struct policydb *pol,
 		    void *p);
 
 int avtab_read(struct avtab *a, void *fp, struct policydb *pol);
+int avtab_write_item(struct policydb *p, struct avtab_node *cur, void *fp);
+int avtab_write(struct policydb *p, struct avtab *a, void *fp);
 
 struct avtab_node *avtab_insert_nonunique(struct avtab *h, struct avtab_key *key,
 					  struct avtab_datum *datum);
security/selinux/ss/conditional.c +123 -0

129 lines not on this page · raw message

security/selinux/ss/conditional.h +2 -0
index 53ddb01..3f209c6 100644
@@ -69,6 +69,8 @@ int cond_index_bool(void *key, void *datum, void *datap);
 
 int cond_read_bool(struct policydb *p, struct hashtab *h, void *fp);
 int cond_read_list(struct policydb *p, void *fp);
+int cond_write_bool(void *key, void *datum, void *ptr);
+int cond_write_list(struct policydb *p, struct cond_node *list, void *fp);
 
 void cond_compute_av(struct avtab *ctab, struct avtab_key *key, struct av_decision *avd);
 
security/selinux/ss/ebitmap.c +79 -2

98 lines not on this page · raw message

security/selinux/ss/ebitmap.h +1 -0
index f283b43..1f4e93c 100644
@@ -123,6 +123,7 @@ int ebitmap_get_bit(struct ebitmap *e, unsigned long bit);
 int ebitmap_set_bit(struct ebitmap *e, unsigned long bit, int value);
 void ebitmap_destroy(struct ebitmap *e);
 int ebitmap_read(struct ebitmap *e, void *fp);
+int ebitmap_write(struct ebitmap *e, void *fp);
 
 #ifdef CONFIG_NETLABEL
 int ebitmap_netlbl_export(struct ebitmap *ebmap,
security/selinux/ss/policydb.c +838 -0

847 lines not on this page · raw message

security/selinux/ss/policydb.h +23 -0
index 26d9adf..9cbf9df 100644
@@ -268,6 +268,7 @@ extern int policydb_class_isvalid(struct policydb *p, unsigned int class);
 extern int policydb_type_isvalid(struct policydb *p, unsigned int type);
 extern int policydb_role_isvalid(struct policydb *p, unsigned int role);
 extern int policydb_read(struct policydb *p, void *fp);
+extern int policydb_write(struct policydb *p, void *fp);
 
 #define PERM_SYMTAB_SIZE 32
 
@@ -288,6 +289,11 @@ struct policy_file {
 	size_t len;
 };
 
+struct policy_data {
+	struct policydb *p;
+	void *fp;
+};
+
 static inline int next_entry(void *buf, struct policy_file *fp, size_t bytes)
 {
 	if (bytes > fp->len)
@@ -299,6 +305,23 @@ static inline int next_entry(void *buf, struct policy_file *fp, size_t bytes)
 	return 0;
 }
 
+static inline int put_entry(void *buf, size_t bytes, int num, struct policy_file *fp)
+{
+	size_t len = bytes * num;
+
+	/*
+	 * this could happen if there was a policy load between the call to
+	 * security_policydb_len() and security_policydb_read()
+	 */
+	if (len > fp->len)
+		return -ENOMEM;
+
+	memcpy(fp->data, buf, len);
+	fp->data += len;
+	fp->len -= len;
+	return 0;
+}
+
 extern u16 string_to_security_class(struct policydb *p, const char *name);
 extern u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name);
 
security/selinux/ss/services.c +54 -0

81 lines not on this page · raw message

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

author
#1 lore raw

PATCH 2/2 selinux: implement mmap on /selinux/policy

/selinux/policy allows a user to copy the policy back out of the kernel. This patch allows userspace to actually mmap that file and use it directly.

---

 security/selinux/selinuxfs.c |   44 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 44 insertions(+), 0 deletions(-)
security/selinux/selinuxfs.c +44 -0
index e0c2bcf..864a74e 100644
@@ -68,6 +68,8 @@ static int *bool_pending_values;
 static struct dentry *class_dir;
 static unsigned long last_class_ino;
 
+static char policy_opened;
+
 /* global data for policy capabilities */
 static struct dentry *policycap_dir;
 
@@ -315,11 +317,23 @@ static int sel_open_policy(struct inode *inode, struct file *filp)
 	if (rc)
 		goto err;
 
+	if (policy_opened) {
+		mutex_unlock(&sel_mutex);
+		return -EBUSY;
+	}
+	policy_opened = 1;
+
 	rc = -ENOMEM;
 	plm = kzalloc(sizeof(*plm), GFP_KERNEL);
 	if (!plm)
 		goto err;
 
+	if (i_size_read(inode) != security_policydb_len()) {
+		mutex_lock(&inode->i_mutex);
+		i_size_write(inode, security_policydb_len());
+		mutex_unlock(&inode->i_mutex);
+	}
+
 	rc = security_read_policy(&plm->data, &plm->len);
 	if (rc)
 		goto err;
@@ -344,6 +358,8 @@ static int sel_release_policy(struct inode *inode, struct file *filp)
 
 	BUG_ON(!plm);
 
+	policy_opened = 0;
+
 	vfree(plm->data);
 	kfree(plm);
 
@@ -381,9 +397,37 @@ out:
 	return ret;
 }
 
+static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
+{
+	int ret;
+	long length = vma->vm_end - vma->vm_start;
+	unsigned long start = vma->vm_start;
+	struct policy_load_memory *plm = filp->private_data;
+	char *pos = plm->data;
+	unsigned long pfn;
+
+	if (vma->vm_pgoff != 0)
+		return -EIO;
+
+	if (length > roundup(plm->len, PAGE_SIZE))
+		return -EIO;
+
+	while (length > 0) {
+		pfn = vmalloc_to_pfn(pos);
+		ret = remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED);
+		if (ret)
+			return ret;
+		start += PAGE_SIZE;
+		pos += PAGE_SIZE;
+		length -= PAGE_SIZE;
+	}
+	return 0;
+}
+
 static const struct file_operations sel_policy_ops = {
 	.open		= sel_open_policy,
 	.read		= sel_read_policy,
+	.mmap		= sel_mmap_policy,
 	.release	= sel_release_policy,
 };
 

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#2 lore raw

Re: [PATCH 2/2] selinux: implement mmap on /selinux/policy

Quoted 2 lines · Eric Paris

/selinux/policy allows a user to copy the policy back out of the kernel. This patch allows userspace to actually mmap that file and use it directly.

# checkpolicy -b /selinux/policy
checkpolicy:  loading policy configuration from /selinux/policy
Can't map '/selinux/policy':  Invalid argument
# strace checkpolicy -b /selinux/policy
...
open("/selinux/policy", O_RDONLY)       = 3
fstat(3, {st_mode=S_IFREG|0400, st_size=5720516, ...}) = 0
mmap(NULL, 5720516, PROT_READ|PROT_WRITE, MAP_PRIVATE, 3, 0) = -1 EINVAL
(Invalid argument)
Quoted 91 lines

---

 security/selinux/selinuxfs.c |   44 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 44 insertions(+), 0 deletions(-)
security/selinux/selinuxfs.c +44 -0
index e0c2bcf..864a74e 100644
@@ -68,6 +68,8 @@ static int *bool_pending_values;
 static struct dentry *class_dir;
 static unsigned long last_class_ino;
 
+static char policy_opened;
+
 /* global data for policy capabilities */
 static struct dentry *policycap_dir;
 
@@ -315,11 +317,23 @@ static int sel_open_policy(struct inode *inode, struct file *filp)
 	if (rc)
 		goto err;
 
+	if (policy_opened) {
+		mutex_unlock(&sel_mutex);
+		return -EBUSY;
+	}
+	policy_opened = 1;
+
 	rc = -ENOMEM;
 	plm = kzalloc(sizeof(*plm), GFP_KERNEL);
 	if (!plm)
 		goto err;
 
+	if (i_size_read(inode) != security_policydb_len()) {
+		mutex_lock(&inode->i_mutex);
+		i_size_write(inode, security_policydb_len());
+		mutex_unlock(&inode->i_mutex);
+	}
+
 	rc = security_read_policy(&plm->data, &plm->len);
 	if (rc)
 		goto err;
@@ -344,6 +358,8 @@ static int sel_release_policy(struct inode *inode, struct file *filp)
 
 	BUG_ON(!plm);
 
+	policy_opened = 0;
+
 	vfree(plm->data);
 	kfree(plm);
 
@@ -381,9 +397,37 @@ out:
 	return ret;
 }
 
+static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
+{
+	int ret;
+	long length = vma->vm_end - vma->vm_start;
+	unsigned long start = vma->vm_start;
+	struct policy_load_memory *plm = filp->private_data;
+	char *pos = plm->data;
+	unsigned long pfn;
+
+	if (vma->vm_pgoff != 0)
+		return -EIO;
+
+	if (length > roundup(plm->len, PAGE_SIZE))
+		return -EIO;
+
+	while (length > 0) {
+		pfn = vmalloc_to_pfn(pos);
+		ret = remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED);
+		if (ret)
+			return ret;
+		start += PAGE_SIZE;
+		pos += PAGE_SIZE;
+		length -= PAGE_SIZE;
+	}
+	return 0;
+}
+
 static const struct file_operations sel_policy_ops = {
 	.open		= sel_open_policy,
 	.read		= sel_read_policy,
+	.mmap		= sel_mmap_policy,
 	.release	= sel_release_policy,
 };
 
Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
author
#3 lore raw

Re: [PATCH 2/2] selinux: implement mmap on /selinux/policy

Quoted 8 lines · Stephen Smalley
Quoted 2 lines · Eric Paris

/selinux/policy allows a user to copy the policy back out of the kernel. This patch allows userspace to actually mmap that file and use it directly.

# checkpolicy -b /selinux/policy
checkpolicy:  loading policy configuration from /selinux/policy
Can't map '/selinux/policy':  Invalid argument
Quoted 6 lines
# strace checkpolicy -b /selinux/policy
...
open("/selinux/policy", O_RDONLY)       = 3
fstat(3, {st_mode=S_IFREG|0400, st_size=5720516, ...}) = 0
mmap(NULL, 5720516, PROT_READ|PROT_WRITE, MAP_PRIVATE, 3, 0) = -1 EINVAL
(Invalid argument)

MAP_SHARED works. I didn't realize checkpolicy used mmap. I'll look at it.

-Eric

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#4 lore raw

Re: [PATCH 2/2] selinux: implement mmap on /selinux/policy

In reply to Eric Paris
Quoted 32 lines · Eric Paris

/selinux/policy allows a user to copy the policy back out of the kernel. This patch allows userspace to actually mmap that file and use it directly.

---

 security/selinux/selinuxfs.c |   44 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 44 insertions(+), 0 deletions(-)
security/selinux/selinuxfs.c +8 -0
index e0c2bcf..864a74e 100644
@@ -68,6 +68,8 @@ static int *bool_pending_values;
 static struct dentry *class_dir;
 static unsigned long last_class_ino;
 
+static char policy_opened;
+
 /* global data for policy capabilities */
 static struct dentry *policycap_dir;
 
@@ -315,11 +317,23 @@ static int sel_open_policy(struct inode *inode, struct file *filp)
 	if (rc)
 		goto err;
 
+	if (policy_opened) {
+		mutex_unlock(&sel_mutex);
+		return -EBUSY;
+	}
+	policy_opened = 1;
+

security/selinux/selinuxfs.c static int sel_open_policy(struct inode *inode, struct file *filp) · line 335

Quoted 10 lines
security/selinux/selinuxfs.c +5 -0
 	rc = -ENOMEM;
 	plm = kzalloc(sizeof(*plm), GFP_KERNEL);
 	if (!plm)
 		goto err;
 
+	if (i_size_read(inode) != security_policydb_len()) {
+		mutex_lock(&inode->i_mutex);
+		i_size_write(inode, security_policydb_len());
+		mutex_unlock(&inode->i_mutex);
+	}

What exactly are we doing here?
Do we truly want to limit /selinux/policy to one process at a time? Why is that necessary?

Quoted 32 lines
security/selinux/selinuxfs.c +18 -0
 	rc = security_read_policy(&plm->data, &plm->len);
 	if (rc)
 		goto err;
@@ -344,6 +358,8 @@ static int sel_release_policy(struct inode *inode, struct file *filp)
 
 	BUG_ON(!plm);
 
+	policy_opened = 0;
+
 	vfree(plm->data);
 	kfree(plm);
 
@@ -381,9 +397,37 @@ out:
 	return ret;
 }
 
+static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
+{
+	int ret;
+	long length = vma->vm_end - vma->vm_start;
+	unsigned long start = vma->vm_start;
+	struct policy_load_memory *plm = filp->private_data;
+	char *pos = plm->data;
+	unsigned long pfn;
+
+	if (vma->vm_pgoff != 0)
+		return -EIO;
+
+	if (length > roundup(plm->len, PAGE_SIZE))
+		return -EIO;
+
+	while (length > 0) {

security/selinux/selinuxfs.c out: · line 425

Quoted 10 lines
security/selinux/selinuxfs.c +10 -0
+		pfn = vmalloc_to_pfn(pos);
+		ret = remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED);
+		if (ret)
+			return ret;
+		start += PAGE_SIZE;
+		pos += PAGE_SIZE;
+		length -= PAGE_SIZE;
+	}
+	return 0;
+}

I'm not overly familiar with this area - can you point to an example .mmap handler that you used as the basis for your implementation?

Quoted 8 lines
security/selinux/selinuxfs.c +2 -0
+
 static const struct file_operations sel_policy_ops = {
 	.open		= sel_open_policy,
 	.read		= sel_read_policy,
+	.mmap		= sel_mmap_policy,
 	.release	= sel_release_policy,
 };
 
Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
author
#5 lore raw

Re: [PATCH 2/2] selinux: implement mmap on /selinux/policy

Quoted 38 lines · Stephen Smalley
Quoted 37 lines · Eric Paris

/selinux/policy allows a user to copy the policy back out of the kernel. This patch allows userspace to actually mmap that file and use it directly.

---

 security/selinux/selinuxfs.c |   44 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 44 insertions(+), 0 deletions(-)
security/selinux/selinuxfs.c +8 -0
index e0c2bcf..864a74e 100644
@@ -68,6 +68,8 @@ static int *bool_pending_values;
 static struct dentry *class_dir;
 static unsigned long last_class_ino;
 
+static char policy_opened;
+
 /* global data for policy capabilities */
 static struct dentry *policycap_dir;
 
@@ -315,11 +317,23 @@ static int sel_open_policy(struct inode *inode, struct file *filp)
 	if (rc)
 		goto err;
 
+	if (policy_opened) {
+		mutex_unlock(&sel_mutex);
+		return -EBUSY;
+	}
+	policy_opened = 1;
+
 	rc = -ENOMEM;
 	plm = kzalloc(sizeof(*plm), GFP_KERNEL);
 	if (!plm)
 		goto err;
 
Quoted 9 lines

security/selinux/selinuxfs.c static int sel_open_policy(struct inode *inode, struct file *filp) · line 335

Quoted 5 lines
security/selinux/selinuxfs.c +5 -0
+	if (i_size_read(inode) != security_policydb_len()) {
+		mutex_lock(&inode->i_mutex);
+		i_size_write(inode, security_policydb_len());
+		mutex_unlock(&inode->i_mutex);
+	}

What exactly are we doing here?
Do we truly want to limit /selinux/policy to one process at a time? Why is that necessary?

My fear was:

Process A Process B
------------------------ -------------------------
open("/selinux/policy");
plm->data = p1
plm->len = l1
i_size = l1

				load_policy
				open("/selinux/policy");
				  plm->data = p2;
				  plm->len = l2;
				  i_size = l2;
				stat("/selinux/policy");
				  we get i_size=l2

stat("/selinux/policy");
we get i_size=l2 WHOOPS.

Quoted 38 lines
Quoted 38 lines
security/selinux/selinuxfs.c +24 -0
 	rc = security_read_policy(&plm->data, &plm->len);
 	if (rc)
 		goto err;
@@ -344,6 +358,8 @@ static int sel_release_policy(struct inode *inode, struct file *filp)
 
 	BUG_ON(!plm);
 
+	policy_opened = 0;
+
 	vfree(plm->data);
 	kfree(plm);
 
@@ -381,9 +397,37 @@ out:
 	return ret;
 }
 
+static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
+{
+	int ret;
+	long length = vma->vm_end - vma->vm_start;
+	unsigned long start = vma->vm_start;
+	struct policy_load_memory *plm = filp->private_data;
+	char *pos = plm->data;
+	unsigned long pfn;
+
+	if (vma->vm_pgoff != 0)
+		return -EIO;
+
+	if (length > roundup(plm->len, PAGE_SIZE))
+		return -EIO;
+
+	while (length > 0) {
+		pfn = vmalloc_to_pfn(pos);
+		ret = remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED);
+		if (ret)
+			return ret;
+		start += PAGE_SIZE;
+		pos += PAGE_SIZE;
Quoted 8 lines

security/selinux/selinuxfs.c out: · line 425

Quoted 4 lines
security/selinux/selinuxfs.c +4 -0
+		length -= PAGE_SIZE;
+	}
+	return 0;
+}

I'm not overly familiar with this area - can you point to an example .mmap handler that you used as the basis for your implementation?

I basically stole from:
http://www.scs.ch/~frey/linux/memorymap.html
http://www.scs.ch/~frey/linux/mmap.example.tar

Although seeing as how remap_pfn_range() only works for MAP_SHARED I'm reading http://www.xml.com/ldd/chapter/book/ch13.html as a hopefully better way to do it.

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#6 lore raw

Re: [PATCH 2/2] selinux: implement mmap on /selinux/policy

Quoted 64 lines · Eric Paris
Quoted 38 lines · Stephen Smalley
Quoted 37 lines · Eric Paris

/selinux/policy allows a user to copy the policy back out of the kernel. This patch allows userspace to actually mmap that file and use it directly.

---

 security/selinux/selinuxfs.c |   44 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 44 insertions(+), 0 deletions(-)
security/selinux/selinuxfs.c +8 -0
index e0c2bcf..864a74e 100644
@@ -68,6 +68,8 @@ static int *bool_pending_values;
 static struct dentry *class_dir;
 static unsigned long last_class_ino;
 
+static char policy_opened;
+
 /* global data for policy capabilities */
 static struct dentry *policycap_dir;
 
@@ -315,11 +317,23 @@ static int sel_open_policy(struct inode *inode, struct file *filp)
 	if (rc)
 		goto err;
 
+	if (policy_opened) {
+		mutex_unlock(&sel_mutex);
+		return -EBUSY;
+	}
+	policy_opened = 1;
+
 	rc = -ENOMEM;
 	plm = kzalloc(sizeof(*plm), GFP_KERNEL);
 	if (!plm)
 		goto err;
 
Quoted 9 lines

security/selinux/selinuxfs.c static int sel_open_policy(struct inode *inode, struct file *filp) · line 335

Quoted 5 lines
security/selinux/selinuxfs.c +5 -0
+	if (i_size_read(inode) != security_policydb_len()) {
+		mutex_lock(&inode->i_mutex);
+		i_size_write(inode, security_policydb_len());
+		mutex_unlock(&inode->i_mutex);
+	}

What exactly are we doing here?
Do we truly want to limit /selinux/policy to one process at a time? Why is that necessary?

My fear was:

Process A Process B
------------------------ -------------------------
open("/selinux/policy");
plm->data = p1
plm->len = l1
i_size = l1

				load_policy
				open("/selinux/policy");
				  plm->data = p2;
				  plm->len = l2;
				  i_size = l2;
				stat("/selinux/policy");
				  we get i_size=l2
Quoted 2 lines

stat("/selinux/policy");
we get i_size=l2 WHOOPS.

How is that different from corresponding situation for a regular file? Doesn't seem like the sort of thing the kernel should worry about. I thought the check was to prevent arbitrary allocation of kernel memory by spinning in a loop opening /selinux/policy forever. But that doesn't require worrying about the inode size changing.

Quoted 54 lines
Quoted 38 lines
Quoted 38 lines
security/selinux/selinuxfs.c +24 -0
 	rc = security_read_policy(&plm->data, &plm->len);
 	if (rc)
 		goto err;
@@ -344,6 +358,8 @@ static int sel_release_policy(struct inode *inode, struct file *filp)
 
 	BUG_ON(!plm);
 
+	policy_opened = 0;
+
 	vfree(plm->data);
 	kfree(plm);
 
@@ -381,9 +397,37 @@ out:
 	return ret;
 }
 
+static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
+{
+	int ret;
+	long length = vma->vm_end - vma->vm_start;
+	unsigned long start = vma->vm_start;
+	struct policy_load_memory *plm = filp->private_data;
+	char *pos = plm->data;
+	unsigned long pfn;
+
+	if (vma->vm_pgoff != 0)
+		return -EIO;
+
+	if (length > roundup(plm->len, PAGE_SIZE))
+		return -EIO;
+
+	while (length > 0) {
+		pfn = vmalloc_to_pfn(pos);
+		ret = remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED);
+		if (ret)
+			return ret;
+		start += PAGE_SIZE;
+		pos += PAGE_SIZE;
Quoted 8 lines

security/selinux/selinuxfs.c out: · line 425

Quoted 4 lines
security/selinux/selinuxfs.c +4 -0
+		length -= PAGE_SIZE;
+	}
+	return 0;
+}

I'm not overly familiar with this area - can you point to an example .mmap handler that you used as the basis for your implementation?

I basically stole from:
http://www.scs.ch/~frey/linux/memorymap.html
http://www.scs.ch/~frey/linux/mmap.example.tar

Although seeing as how remap_pfn_range() only works for MAP_SHARED I'm reading http://www.xml.com/ldd/chapter/book/ch13.html as a hopefully better way to do it.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
author
#7 lore raw

Re: [PATCH 2/2] selinux: implement mmap on /selinux/policy

Quoted 20 lines · Stephen Smalley
Quoted 15 lines · Eric Paris

My fear was:

Process A Process B
------------------------ -------------------------
open("/selinux/policy");
plm->data = p1
plm->len = l1
i_size = l1

				load_policy
				open("/selinux/policy");
				  plm->data = p2;
				  plm->len = l2;
				  i_size = l2;
				stat("/selinux/policy");
				  we get i_size=l2
Quoted 2 lines

stat("/selinux/policy");
we get i_size=l2 WHOOPS.

Quoted 5 lines

How is that different from corresponding situation for a regular file? Doesn't seem like the sort of thing the kernel should worry about. I thought the check was to prevent arbitrary allocation of kernel memory by spinning in a loop opening /selinux/policy forever. But that doesn't require worrying about the inode size changing.

This is actually more the problem with files in /proc. The difference is that the plm->data and plm->len are still p1 and l1. It's just stat() that is going to lie to you. So you will either mmap to much or too little space and you have no idea how long the data you are going to be reading is...

read() works ok, because it is going to go till the EOF, but you have no idea where that is with mmap.....

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#8 lore raw

Re: [PATCH 2/2] selinux: implement mmap on /selinux/policy

Quoted 27 lines · Eric Paris
Quoted 20 lines · Stephen Smalley
Quoted 15 lines · Eric Paris

My fear was:

Process A Process B
------------------------ -------------------------
open("/selinux/policy");
plm->data = p1
plm->len = l1
i_size = l1

				load_policy
				open("/selinux/policy");
				  plm->data = p2;
				  plm->len = l2;
				  i_size = l2;
				stat("/selinux/policy");
				  we get i_size=l2
Quoted 2 lines

stat("/selinux/policy");
we get i_size=l2 WHOOPS.

Quoted 5 lines

How is that different from corresponding situation for a regular file? Doesn't seem like the sort of thing the kernel should worry about. I thought the check was to prevent arbitrary allocation of kernel memory by spinning in a loop opening /selinux/policy forever. But that doesn't require worrying about the inode size changing.

Quoted 8 lines

This is actually more the problem with files in /proc. The difference is that the plm->data and plm->len are still p1 and l1. It's just stat() that is going to lie to you. So you will either mmap to much or too little space and you have no idea how long the data you are going to be reading is...

read() works ok, because it is going to go till the EOF, but you have no idea where that is with mmap.....

I see. It appears that setools and coreutils use read() rather than mmap() since I could run seinfo, sediff, and cp on /selinux/policy, so maybe nothing other than checkpolicy -b would even use the mmap support? And that can be worked around by first copying to a regular file. checkpolicy -b isn't exactly a typical user command; most people don't even know it exists.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
author
#9 lore raw

Re: [PATCH 2/2] selinux: implement mmap on /selinux/policy

Quoted 37 lines · Stephen Smalley
Quoted 27 lines · Eric Paris
Quoted 20 lines · Stephen Smalley
Quoted 15 lines · Eric Paris

My fear was:

Process A Process B
------------------------ -------------------------
open("/selinux/policy");
plm->data = p1
plm->len = l1
i_size = l1

				load_policy
				open("/selinux/policy");
				  plm->data = p2;
				  plm->len = l2;
				  i_size = l2;
				stat("/selinux/policy");
				  we get i_size=l2
Quoted 2 lines

stat("/selinux/policy");
we get i_size=l2 WHOOPS.

Quoted 5 lines

How is that different from corresponding situation for a regular file? Doesn't seem like the sort of thing the kernel should worry about. I thought the check was to prevent arbitrary allocation of kernel memory by spinning in a loop opening /selinux/policy forever. But that doesn't require worrying about the inode size changing.

Quoted 8 lines

This is actually more the problem with files in /proc. The difference is that the plm->data and plm->len are still p1 and l1. It's just stat() that is going to lie to you. So you will either mmap to much or too little space and you have no idea how long the data you are going to be reading is...

read() works ok, because it is going to go till the EOF, but you have no idea where that is with mmap.....

Quoted 6 lines

I see. It appears that setools and coreutils use read() rather than mmap() since I could run seinfo, sediff, and cp on /selinux/policy, so maybe nothing other than checkpolicy -b would even use the mmap support? And that can be worked around by first copying to a regular file. checkpolicy -b isn't exactly a typical user command; most people don't even know it exists.

Another horrific option would be to create a new inode for each open() so your fd would always have your i_size info....

I'm going to finish my second mmap implementation attempt just to learn, but maybe we want to just throw it all away? If so, thoughts on addressing the memory usage issue?

-Eric

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#10 lore raw

Re: [PATCH 2/2] selinux: implement mmap on /selinux/policy

Quoted 45 lines · Eric Paris
Quoted 37 lines · Stephen Smalley
Quoted 27 lines · Eric Paris
Quoted 20 lines · Stephen Smalley
Quoted 15 lines · Eric Paris

My fear was:

Process A Process B
------------------------ -------------------------
open("/selinux/policy");
plm->data = p1
plm->len = l1
i_size = l1

				load_policy
				open("/selinux/policy");
				  plm->data = p2;
				  plm->len = l2;
				  i_size = l2;
				stat("/selinux/policy");
				  we get i_size=l2
Quoted 2 lines

stat("/selinux/policy");
we get i_size=l2 WHOOPS.

Quoted 5 lines

How is that different from corresponding situation for a regular file? Doesn't seem like the sort of thing the kernel should worry about. I thought the check was to prevent arbitrary allocation of kernel memory by spinning in a loop opening /selinux/policy forever. But that doesn't require worrying about the inode size changing.

Quoted 8 lines

This is actually more the problem with files in /proc. The difference is that the plm->data and plm->len are still p1 and l1. It's just stat() that is going to lie to you. So you will either mmap to much or too little space and you have no idea how long the data you are going to be reading is...

read() works ok, because it is going to go till the EOF, but you have no idea where that is with mmap.....

Quoted 6 lines

I see. It appears that setools and coreutils use read() rather than mmap() since I could run seinfo, sediff, and cp on /selinux/policy, so maybe nothing other than checkpolicy -b would even use the mmap support? And that can be worked around by first copying to a regular file. checkpolicy -b isn't exactly a typical user command; most people don't even know it exists.

Quoted 2 lines

Another horrific option would be to create a new inode for each open() so your fd would always have your i_size info....

That would indeed be horrific ;)

Quoted 3 lines

I'm going to finish my second mmap implementation attempt just to learn, but maybe we want to just throw it all away? If so, thoughts on addressing the memory usage issue?

I don't care about userspace getting confused by an interleaving of policy load and policy read - that is a userspace problem IMO. So I wouldn't be opposed to having the mmap handler upstreamed, although I don't feel qualified to review the implementation - might want some -fsdevel or -kernel eyes on it.

Limiting /selinux/policy to one user at a time might be reasonable as a way of limiting memory consumption. But you don't need the i_size hacks as part of that.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
author
#11 lore raw

Re: [PATCH 2/2] selinux: implement mmap on /selinux/policy

Quoted 61 lines · Stephen Smalley
Quoted 45 lines · Eric Paris
Quoted 37 lines · Stephen Smalley
Quoted 27 lines · Eric Paris
Quoted 20 lines · Stephen Smalley
Quoted 15 lines · Eric Paris

My fear was:

Process A Process B
------------------------ -------------------------
open("/selinux/policy");
plm->data = p1
plm->len = l1
i_size = l1

				load_policy
				open("/selinux/policy");
				  plm->data = p2;
				  plm->len = l2;
				  i_size = l2;
				stat("/selinux/policy");
				  we get i_size=l2
Quoted 2 lines

stat("/selinux/policy");
we get i_size=l2 WHOOPS.

Quoted 5 lines

How is that different from corresponding situation for a regular file? Doesn't seem like the sort of thing the kernel should worry about. I thought the check was to prevent arbitrary allocation of kernel memory by spinning in a loop opening /selinux/policy forever. But that doesn't require worrying about the inode size changing.

Quoted 8 lines

This is actually more the problem with files in /proc. The difference is that the plm->data and plm->len are still p1 and l1. It's just stat() that is going to lie to you. So you will either mmap to much or too little space and you have no idea how long the data you are going to be reading is...

read() works ok, because it is going to go till the EOF, but you have no idea where that is with mmap.....

Quoted 6 lines

I see. It appears that setools and coreutils use read() rather than mmap() since I could run seinfo, sediff, and cp on /selinux/policy, so maybe nothing other than checkpolicy -b would even use the mmap support? And that can be worked around by first copying to a regular file. checkpolicy -b isn't exactly a typical user command; most people don't even know it exists.

Quoted 2 lines

Another horrific option would be to create a new inode for each open() so your fd would always have your i_size info....

That would indeed be horrific ;)

Quoted 3 lines

I'm going to finish my second mmap implementation attempt just to learn, but maybe we want to just throw it all away? If so, thoughts on addressing the memory usage issue?

I don't care about userspace getting confused by an interleaving of policy load and policy read - that is a userspace problem IMO. So I wouldn't be opposed to having the mmap handler upstreamed, although I don't feel qualified to review the implementation - might want some -fsdevel or -kernel eyes on it.

Quoted 3 lines

Limiting /selinux/policy to one user at a time might be reasonable as a way of limiting memory consumption. But you don't need the i_size hacks as part of that.

My new mmap implementation (a whole lot easier to review even by an idiot like me) gives:

# checkpolicy -M -b /selinux/policy
checkpolicy:  loading policy configuration from /selinux/policy
libsepol.policydb_index_others: security:  9 users, 13 roles, 3341 types, 170 bools
libsepol.policydb_index_others: security: 1 sens, 1024 cats
libsepol.policydb_index_others: security:  77 classes, 195841 rules, 236706 cond rules
checkpolicy:  policy configuration loaded

-----

commit 7e9368a5d130e6caa9b914cc60da7bc3791e04a5

Author: Eric Paris <[email protected]>
Date:   Tue Jul 27 16:22:33 2010 -0400

selinux: implement mmap on /selinux/policy

/selinux/policy allows a user to copy the policy back out of the kernel. This patch allows userspace to actually mmap that file and use it directly.

security/selinux/selinuxfs.c +51 -0
index 8f64eec..6562ecd 100644
@@ -68,6 +68,8 @@ static int *bool_pending_values;
 static struct dentry *class_dir;
 static unsigned long last_class_ino;
 
+static char policy_opened;
+
 /* global data for policy capabilities */
 static struct dentry *policycap_dir;
 
@@ -315,11 +317,23 @@ static int sel_open_policy(struct inode *inode, struct file *filp)
 	if (rc)
 		goto err;
 
+	if (policy_opened) {
+		mutex_unlock(&sel_mutex);
+		return -EBUSY;
+	}
+	policy_opened = 1;
+
 	rc = -ENOMEM;
 	plm = kzalloc(sizeof(*plm), GFP_KERNEL);
 	if (!plm)
 		goto err;
 
+	if (i_size_read(inode) != security_policydb_len()) {
+		mutex_lock(&inode->i_mutex);
+		i_size_write(inode, security_policydb_len());
+		mutex_unlock(&inode->i_mutex);
+	}
+
 	rc = security_read_policy(&plm->data, &plm->len);
 	if (rc)
 		goto err;
@@ -344,6 +358,8 @@ static int sel_release_policy(struct inode *inode, struct file *filp)
 
 	BUG_ON(!plm);
 
+	policy_opened = 0;
+
 	vfree(plm->data);
 	kfree(plm);
 
@@ -380,9 +396,44 @@ out:
 	return ret;
 }
 
+static int sel_mmap_policy_fault(struct vm_area_struct *vma,
+				 struct vm_fault *vmf)
+{
+	struct policy_load_memory *plm = vma->vm_file->private_data;
+	unsigned long offset;
+	struct page *page;
+
+	if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
+		return VM_FAULT_SIGBUS;
+
+	offset = vmf->pgoff << PAGE_SHIFT;
+	if (offset >= roundup(plm->len, PAGE_SIZE))
+		return VM_FAULT_SIGBUS;
+
+	page = vmalloc_to_page(plm->data + offset);
+	get_page(page);
+
+	vmf->page = page;
+
+	return 0;
+}
+
+static struct vm_operations_struct sel_mmap_policy_ops = {
+	.fault = sel_mmap_policy_fault,
+};
+
+int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
+{
+	vma->vm_flags |= VM_RESERVED;
+	vma->vm_ops = &sel_mmap_policy_ops;
+
+	return 0;
+}
+
 static const struct file_operations sel_policy_ops = {
 	.open		= sel_open_policy,
 	.read		= sel_read_policy,
+	.mmap		= sel_mmap_policy,
 	.release	= sel_release_policy,
 };
 

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#12 lore raw

Re: [PATCH 2/2] selinux: implement mmap on /selinux/policy

Quoted 103 lines · Eric Paris
Quoted 61 lines · Stephen Smalley
Quoted 45 lines · Eric Paris
Quoted 37 lines · Stephen Smalley
Quoted 27 lines · Eric Paris
Quoted 20 lines · Stephen Smalley
Quoted 15 lines · Eric Paris

My fear was:

Process A Process B
------------------------ -------------------------
open("/selinux/policy");
plm->data = p1
plm->len = l1
i_size = l1

				load_policy
				open("/selinux/policy");
				  plm->data = p2;
				  plm->len = l2;
				  i_size = l2;
				stat("/selinux/policy");
				  we get i_size=l2
Quoted 2 lines

stat("/selinux/policy");
we get i_size=l2 WHOOPS.

Quoted 5 lines

How is that different from corresponding situation for a regular file? Doesn't seem like the sort of thing the kernel should worry about. I thought the check was to prevent arbitrary allocation of kernel memory by spinning in a loop opening /selinux/policy forever. But that doesn't require worrying about the inode size changing.

Quoted 8 lines

This is actually more the problem with files in /proc. The difference is that the plm->data and plm->len are still p1 and l1. It's just stat() that is going to lie to you. So you will either mmap to much or too little space and you have no idea how long the data you are going to be reading is...

read() works ok, because it is going to go till the EOF, but you have no idea where that is with mmap.....

Quoted 6 lines

I see. It appears that setools and coreutils use read() rather than mmap() since I could run seinfo, sediff, and cp on /selinux/policy, so maybe nothing other than checkpolicy -b would even use the mmap support? And that can be worked around by first copying to a regular file. checkpolicy -b isn't exactly a typical user command; most people don't even know it exists.

Quoted 2 lines

Another horrific option would be to create a new inode for each open() so your fd would always have your i_size info....

That would indeed be horrific ;)

Quoted 3 lines

I'm going to finish my second mmap implementation attempt just to learn, but maybe we want to just throw it all away? If so, thoughts on addressing the memory usage issue?

I don't care about userspace getting confused by an interleaving of policy load and policy read - that is a userspace problem IMO. So I wouldn't be opposed to having the mmap handler upstreamed, although I don't feel qualified to review the implementation - might want some -fsdevel or -kernel eyes on it.

Quoted 3 lines

Limiting /selinux/policy to one user at a time might be reasonable as a way of limiting memory consumption. But you don't need the i_size hacks as part of that.

My new mmap implementation (a whole lot easier to review even by an idiot like me) gives:

# checkpolicy -M -b /selinux/policy
checkpolicy:  loading policy configuration from /selinux/policy
libsepol.policydb_index_others: security:  9 users, 13 roles, 3341 types, 170 bools
libsepol.policydb_index_others: security: 1 sens, 1024 cats
libsepol.policydb_index_others: security:  77 classes, 195841 rules, 236706 cond rules
checkpolicy:  policy configuration loaded

-----

commit 7e9368a5d130e6caa9b914cc60da7bc3791e04a5

Author: Eric Paris <[email protected]>
Date:   Tue Jul 27 16:22:33 2010 -0400

selinux: implement mmap on /selinux/policy

/selinux/policy allows a user to copy the policy back out of the kernel. This patch allows userspace to actually mmap that file and use it directly.

security/selinux/selinuxfs.c +2 -0
index 8f64eec..6562ecd 100644
@@ -68,6 +68,8 @@ static int *bool_pending_values;
 static struct dentry *class_dir;
 static unsigned long last_class_ino;
 
+static char policy_opened;
+
 /* global data for policy capabilities */
 static struct dentry *policycap_dir;
 

security/selinux/selinuxfs.c static int sel_open_policy(struct inode *inode, struct file *filp) · line 324

Quoted 8 lines
security/selinux/selinuxfs.c +5 -0
@@ -315,11 +317,23 @@ static int sel_open_policy(struct inode *inode, struct file *filp)
 	if (rc)
 		goto err;
 
+	if (policy_opened) {
+		mutex_unlock(&sel_mutex);
+		return -EBUSY;
+	}
+	policy_opened = 1;

I would take the policy_opened flag to the first patch to prevent arbitrary kernel memory allocation just by opening /selinux/policy repeatedly. Note however my other comments on the first patch as well.

security/selinux/selinuxfs.c static int sel_open_policy(struct inode *inode, struct file *filp) · line 335

Quoted 11 lines
security/selinux/selinuxfs.c +6 -0
+
 	rc = -ENOMEM;
 	plm = kzalloc(sizeof(*plm), GFP_KERNEL);
 	if (!plm)
 		goto err;
 
+	if (i_size_read(inode) != security_policydb_len()) {
+		mutex_lock(&inode->i_mutex);
+		i_size_write(inode, security_policydb_len());
+		mutex_unlock(&inode->i_mutex);
+	}

Do you really think this is necessary? Does any other filesystem do this? I'd drop it otherwise.

Quoted 28 lines
security/selinux/selinuxfs.c +14 -0
+
 	rc = security_read_policy(&plm->data, &plm->len);
 	if (rc)
 		goto err;
@@ -344,6 +358,8 @@ static int sel_release_policy(struct inode *inode, struct file *filp)
 
 	BUG_ON(!plm);
 
+	policy_opened = 0;
+
 	vfree(plm->data);
 	kfree(plm);
 
@@ -380,9 +396,44 @@ out:
 	return ret;
 }
 
+static int sel_mmap_policy_fault(struct vm_area_struct *vma,
+				 struct vm_fault *vmf)
+{
+	struct policy_load_memory *plm = vma->vm_file->private_data;
+	unsigned long offset;
+	struct page *page;
+
+	if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
+		return VM_FAULT_SIGBUS;
+
+	offset = vmf->pgoff << PAGE_SHIFT;

security/selinux/selinuxfs.c out: · line 419

Quoted 10 lines
security/selinux/selinuxfs.c +10 -0
+	if (offset >= roundup(plm->len, PAGE_SIZE))
+		return VM_FAULT_SIGBUS;
+
+	page = vmalloc_to_page(plm->data + offset);
+	get_page(page);
+
+	vmf->page = page;
+
+	return 0;
+}

This looks fine to me but I can't say that I've written one of these handlers before. Did you derive this from some example in an existing filesystem?

Quoted 27 lines
security/selinux/selinuxfs.c +14 -1
+
+static struct vm_operations_struct sel_mmap_policy_ops = {
+	.fault = sel_mmap_policy_fault,
+};
+
+int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
+{
+	vma->vm_flags |= VM_RESERVED;
+	vma->vm_ops = &sel_mmap_policy_ops;
+
+	return 0;
+}
+
 static const struct file_operations sel_policy_ops = {
 	.open		= sel_open_policy,
 	.read		= sel_read_policy,
+	.mmap		= sel_mmap_policy,
 	.release	= sel_release_policy,
 };
 
 
 
 
--

This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
author
#13 lore raw

Re: [PATCH 2/2] selinux: implement mmap on /selinux/policy

Quoted 11 lines · Stephen Smalley
Quoted 9 lines · Eric Paris
security/selinux/selinuxfs.c +0 -0
index 8f64eec..6562ecd 100644
@@ -315,11 +317,23 @@ static int sel_open_policy(struct inode *inode, struct file *filp)
 	if (rc)
 		goto err;
 
Quoted 9 lines

security/selinux/selinuxfs.c static int sel_open_policy(struct inode *inode, struct file *filp) · line 324

Quoted 5 lines
security/selinux/selinuxfs.c +5 -0
+	if (policy_opened) {
+		mutex_unlock(&sel_mutex);
+		return -EBUSY;
+	}
+	policy_opened = 1;

I would take the policy_opened flag to the first patch to prevent arbitrary kernel memory allocation just by opening /selinux/policy repeatedly. Note however my other comments on the first patch as well.

Done.

Quoted 8 lines

security/selinux/selinuxfs.c static int sel_open_policy(struct inode *inode, struct file *filp) · line 335

Quoted 5 lines
security/selinux/selinuxfs.c +5 -0
+	if (i_size_read(inode) != security_policydb_len()) {
+		mutex_lock(&inode->i_mutex);
+		i_size_write(inode, security_policydb_len());
+		mutex_unlock(&inode->i_mutex);
+	}

Do you really think this is necessary? Does any other filesystem do this? I'd drop it otherwise.

I think it is necessary. Lets look at your strace:

# strace checkpolicy -b /selinux/policy
...
open("/selinux/policy", O_RDONLY)       = 3
fstat(3, {st_mode=S_IFREG|0400, st_size=5720516, ...}) = 0
mmap(NULL, 5720516, PROT_READ|PROT_WRITE, MAP_PRIVATE, 3, 0) = -1 EINVAL
(Invalid argument)

Notice that after the open we use fstat to find out the length and then use that as the argument to mmap. Without this set stat would return 0 length. I stole the idea from fs/proc/kcore.c::open_kcore()

Quoted 17 lines
Quoted 17 lines
security/selinux/selinuxfs.c +17 -0
+static int sel_mmap_policy_fault(struct vm_area_struct *vma,
+				 struct vm_fault *vmf)
+{
+	struct policy_load_memory *plm = vma->vm_file->private_data;
+	unsigned long offset;
+	struct page *page;
+
+	if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
+		return VM_FAULT_SIGBUS;
+
+	offset = vmf->pgoff << PAGE_SHIFT;
+	if (offset >= roundup(plm->len, PAGE_SIZE))
+		return VM_FAULT_SIGBUS;
+
+	page = vmalloc_to_page(plm->data + offset);
+	get_page(page);
+
Quoted 8 lines

security/selinux/selinuxfs.c out: · line 419

Quoted 4 lines
security/selinux/selinuxfs.c +4 -0
+	vmf->page = page;
+
+	return 0;
+}

This looks fine to me but I can't say that I've written one of these handlers before. Did you derive this from some example in an existing filesystem?

Looking at a bunch of them. But it doesn't really work exactly the way I thought it would. I'm not getting a signal when I write to a page. So I plan to poke it some more.

Unrelated note, I'm also afraid I'm possibly leaking some information. I have no idea what information is on the last page of the vmalloc() block after the end of the policy. And I bet userspace could read that bit of information. I need to check on that too....

-Eric

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#14 lore raw
In reply to Eric Paris
Quoted 4 lines · Eric Paris

There is interest in being able to see what the actual policy is that was loaded into the kernel. The patch creates a new selinuxfs file /selinux/policy which can be read by userspace. The actual policy that is loaded into the kernel will be written back out to userspace.

Can you clarify exactly how comparable the output is to the original policy file that was loaded? Last time you mentioned range transition rules may be reordered, KaiGai mentioned that ebitmaps might not be identical after conversion (losing the original startbit), and Chris pointed out that sediff isn't sufficient for comparison as it doesn't yet handle constraints.

Quoted 1 line
Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
author
#15 lore raw
Quoted 6 lines · Stephen Smalley
Quoted 4 lines · Eric Paris

There is interest in being able to see what the actual policy is that was loaded into the kernel. The patch creates a new selinuxfs file /selinux/policy which can be read by userspace. The actual policy that is loaded into the kernel will be written back out to userspace.

Quoted 9 lines

Can you clarify exactly how comparable the output is to the original policy file that was loaded? Last time you mentioned range transition rules may be reordered, KaiGai mentioned that ebitmaps might not be identical after conversion (losing the original startbit), and Chris pointed out that sediff isn't sufficient for comparison as it doesn't yet handle constraints.

Quoted 1 line

The only two areas that I had to deviate in any reasonable way from the userspace policy write code was ebitmaps and range transition rules.

I'm not certain that we actually lose the startbit since if we look at ebitmap_read()

#define MAPTYPE uint64_t /* portion of bitmap in each node */
#define MAPSIZE (sizeof(MAPTYPE) * 8) /* number of bits in node bitmap */

if (n->startbit & (MAPSIZE - 1)) {
                        printf
                            ("security: ebitmap start bit (%d) is not a multiple of the map size (%zu)\n",
                             n->startbit, MAPSIZE);
                        goto bad_free;
                }

Which indicated to me that it has to always be aligned to u64.

If you load the policy you pulled out you should get back bit for bit the exact same policy. The only issue I knew of was the range transition rules being stored in kernel in a hashtab instead of the random order they are stored in userspace policy. I wonder if I could do better testing by sorting the range transition rules in userspace into the same order I expect to get them back out of the kernel hashtab to prove things.

-Eric

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#16 lore raw
Quoted 35 lines · Eric Paris
Quoted 6 lines · Stephen Smalley
Quoted 4 lines · Eric Paris

There is interest in being able to see what the actual policy is that was loaded into the kernel. The patch creates a new selinuxfs file /selinux/policy which can be read by userspace. The actual policy that is loaded into the kernel will be written back out to userspace.

Quoted 9 lines

Can you clarify exactly how comparable the output is to the original policy file that was loaded? Last time you mentioned range transition rules may be reordered, KaiGai mentioned that ebitmaps might not be identical after conversion (losing the original startbit), and Chris pointed out that sediff isn't sufficient for comparison as it doesn't yet handle constraints.

Quoted 1 line

The only two areas that I had to deviate in any reasonable way from the userspace policy write code was ebitmaps and range transition rules.

I'm not certain that we actually lose the startbit since if we look at ebitmap_read()

#define MAPTYPE uint64_t /* portion of bitmap in each node */
#define MAPSIZE (sizeof(MAPTYPE) * 8) /* number of bits in node bitmap */

if (n->startbit & (MAPSIZE - 1)) {
                        printf
                            ("security: ebitmap start bit (%d) is not a multiple of the map size (%zu)\n",
                             n->startbit, MAPSIZE);
                        goto bad_free;
                }

Which indicated to me that it has to always be aligned to u64.

Quoted 7 lines

If you load the policy you pulled out you should get back bit for bit the exact same policy. The only issue I knew of was the range transition rules being stored in kernel in a hashtab instead of the random order they are stored in userspace policy. I wonder if I could do better testing by sorting the range transition rules in userspace into the same order I expect to get them back out of the kernel hashtab to prove things.

Yes, I'd be in favor of that. Just define the rangetr_cmp function in the kernel to truly order the entries at load time and sort them in the same manner in libsepol before writing.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
author
#17 lore raw
Quoted 44 lines · Stephen Smalley
Quoted 35 lines · Eric Paris
Quoted 6 lines · Stephen Smalley
Quoted 4 lines · Eric Paris

There is interest in being able to see what the actual policy is that was loaded into the kernel. The patch creates a new selinuxfs file /selinux/policy which can be read by userspace. The actual policy that is loaded into the kernel will be written back out to userspace.

Quoted 9 lines

Can you clarify exactly how comparable the output is to the original policy file that was loaded? Last time you mentioned range transition rules may be reordered, KaiGai mentioned that ebitmaps might not be identical after conversion (losing the original startbit), and Chris pointed out that sediff isn't sufficient for comparison as it doesn't yet handle constraints.

Quoted 1 line

The only two areas that I had to deviate in any reasonable way from the userspace policy write code was ebitmaps and range transition rules.

I'm not certain that we actually lose the startbit since if we look at ebitmap_read()

#define MAPTYPE uint64_t /* portion of bitmap in each node */
#define MAPSIZE (sizeof(MAPTYPE) * 8) /* number of bits in node bitmap */

if (n->startbit & (MAPSIZE - 1)) {
                        printf
                            ("security: ebitmap start bit (%d) is not a multiple of the map size (%zu)\n",
                             n->startbit, MAPSIZE);
                        goto bad_free;
                }

Which indicated to me that it has to always be aligned to u64.

Quoted 7 lines

If you load the policy you pulled out you should get back bit for bit the exact same policy. The only issue I knew of was the range transition rules being stored in kernel in a hashtab instead of the random order they are stored in userspace policy. I wonder if I could do better testing by sorting the range transition rules in userspace into the same order I expect to get them back out of the kernel hashtab to prove things.

Quoted 3 lines

Yes, I'd be in favor of that. Just define the rangetr_cmp function in the kernel to truly order the entries at load time and sort them in the same manner in libsepol before writing.

Started working on this yesterday and still don't have a bit for bit identical policy.

#cat /selinux/policy > /tmp/policy
#dmesg
[snip]

[  128.977564] starting symtab: 224
[  128.978321] starting symtab: 1419
[  128.979366] starting symtab: 29892
[  128.980187] starting symtab: 34841
[  128.981490] starting symtab: 172150
[  128.982291] starting symtab: 173917
[  128.983133] starting symtab: 179548
[  128.983933] starting symtab: 179774
[  128.984825] starting avtab: 196080
[  128.990442] starting cond: 2657920
[  129.001478] starting role_trans: 5652888

[snip]

Where is a program which compares two files byte by byte between start and end and reports the first byte that is different.

# ./where /tmp/policy /etc/selinux/targeted/policy/policy.25 0 5967956
i=196147

The above says the first byte that is different is byte 196147 which we know is in the avtab (thanks to the instrumented kernel output)

# ./where /tmp/policy /etc/selinux/targeted/policy/policy.25 0 196080
# echo $?
0

#./where /tmp/policy /etc/selinux/targeted/policy/policy.25 2657920 5967956

# echo $?
0

These two show that the files are now identical outside of the avtab entries. Now I'm trying to figure out why the avtab entries are not the same. Anyone have guesses off the top of their head?

-Eric

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

author
#18 lore raw
Quoted 8 lines · Eric Paris
Quoted 3 lines · Stephen Smalley

Yes, I'd be in favor of that. Just define the rangetr_cmp function in the kernel to truly order the entries at load time and sort them in the same manner in libsepol before writing.

Started working on this yesterday and still don't have a bit for bit identical policy.

[snip]

Quoted 3 lines

These two show that the files are now identical outside of the avtab entries. Now I'm trying to figure out why the avtab entries are not the same. Anyone have guesses off the top of their head?

My first thought is that the avtab was allocated in expand_avtab() for the policy.25 and thus was done with an expected # of rules equal to MAX_AVTAB_SIZE, whereas the kernel builds a 'correctly' sized avtab since it knows the correct number of rules. If this is the case it explains how things would get put in different buckets and we end up with the same policy, but different ordering.

If this is the case (which seems likely) I'm wondering the best way to fix this. I don't really want to have to rebuild the userspace avtable a second time just to get final ordering (as if userspace wasn't slow enough) but we can't size the avtab correctly during expand either...

-Eric

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

author
#19 lore raw
Quoted 23 lines · Eric Paris
Quoted 8 lines · Eric Paris
Quoted 3 lines · Stephen Smalley

Yes, I'd be in favor of that. Just define the rangetr_cmp function in the kernel to truly order the entries at load time and sort them in the same manner in libsepol before writing.

Started working on this yesterday and still don't have a bit for bit identical policy.

[snip]

Quoted 3 lines

These two show that the files are now identical outside of the avtab entries. Now I'm trying to figure out why the avtab entries are not the same. Anyone have guesses off the top of their head?

My first thought is that the avtab was allocated in expand_avtab() for the policy.25 and thus was done with an expected # of rules equal to MAX_AVTAB_SIZE, whereas the kernel builds a 'correctly' sized avtab since it knows the correct number of rules. If this is the case it explains how things would get put in different buckets and we end up with the same policy, but different ordering.

Quoted 4 lines

If this is the case (which seems likely) I'm wondering the best way to fix this. I don't really want to have to rebuild the userspace avtable a second time just to get final ordering (as if userspace wasn't slow enough) but we can't size the avtab correctly during expand either...

Easy enough to fix. The kernel has MAX_HASH_BUCKETS = 1<<10 (I think it was intended to be 11) whereas usespace was doing MAX_HASH_BUCKETS = 1<<12 (again I think the intention was 13)

But maxing userspace out at 10 like the kernel I now have a bit for bit exact replica coming back out of the kernel with my policydb write patch. I'll work on cleaning everything up (including the MAX_HASH_BUCKETS thing I don't quite understand) and post some patches.

-Eric

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#20 lore raw
In reply to Eric Paris
Quoted 27 lines · Eric Paris

There is interest in being able to see what the actual policy is that was loaded into the kernel. The patch creates a new selinuxfs file /selinux/policy which can be read by userspace. The actual policy that is loaded into the kernel will be written back out to userspace.

---

 include/linux/kernel.h              |    1 
 security/selinux/include/security.h |    2 
 security/selinux/selinuxfs.c        |   92 ++++
 security/selinux/ss/avtab.c         |   42 ++
 security/selinux/ss/avtab.h         |    2 
 security/selinux/ss/conditional.c   |  123 +++++
 security/selinux/ss/conditional.h   |    2 
 security/selinux/ss/ebitmap.c       |   81 +++
 security/selinux/ss/ebitmap.h       |    1 
 security/selinux/ss/policydb.c      |  838 +++++++++++++++++++++++++++++++++++
 security/selinux/ss/policydb.h      |   23 +
 security/selinux/ss/services.c      |   54 ++
 12 files changed, 1259 insertions(+), 2 deletions(-)
include/linux/kernel.h +0 -0
index 7d5b10f..d6092fd 100644

include/linux/kernel.h extern const char linux_proc_banner[]; · line 65

Quoted 7 lines
include/linux/kernel.h +1 -0
@@ -59,6 +59,7 @@ extern const char linux_proc_banner[];
 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
+#define rounddown(x, y) ((x) - ((x) % (y)))
 #define DIV_ROUND_CLOSEST(x, divisor)(			\
 {							\
 	typeof(divisor) __divisor = divisor;

I guess this hunk needs to go to linux-kernel.

Quoted 21 lines
security/selinux/selinuxfs.c +6 -0
index 79a1bb6..e0c2bcf 100644
@@ -110,6 +110,7 @@ enum sel_inos {
 	SEL_COMPAT_NET,	/* whether to use old compat network packet controls */
 	SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
 	SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
+	SEL_POLICY,	/* allow userspace to read the in kernel policy */
 	SEL_INO_NEXT,	/* The next inode number to use */
 };
 
@@ -296,6 +297,96 @@ static const struct file_operations sel_mls_ops = {
 	.llseek		= generic_file_llseek,
 };
 
+struct policy_load_memory {
+	size_t len;
+	void *data;
+};
+

security/selinux/selinuxfs.c static const struct file_operations sel_mls_ops = { · line 314

Quoted 10 lines
security/selinux/selinuxfs.c +10 -0
+static int sel_open_policy(struct inode *inode, struct file *filp)
+{
+	struct policy_load_memory *plm = NULL;
+	int rc;
+
+	BUG_ON(filp->private_data);
+
+	mutex_lock(&sel_mutex);
+
+	rc = task_has_security(current, SECURITY__LOAD_POLICY);

I'd introduce a new permission for this operation - reading the active from the kernel isn't equivalent to being able to load a different policy.

security/selinux/selinuxfs.c static const struct file_operations sel_mls_ops = { · line 364

Quoted 11 lines
security/selinux/selinuxfs.c +11 -0
+static ssize_t sel_read_policy(struct file *filp, char __user *buf,
+			       size_t count, loff_t *ppos)
+{
+	struct policy_load_memory *plm = filp->private_data;
+	int ret;
+
+	mutex_lock(&sel_mutex);
+
+	ret = task_has_security(current, SECURITY__LOAD_POLICY);
+	if (ret)
+		goto out;

Likewise.

security/selinux/selinuxfs.c static const struct file_operations sel_mls_ops = { · line 377

Quoted 13 lines
security/selinux/selinuxfs.c +13 -0
+
+	ret = 0;
+	if (*ppos >= plm->len)
+		goto out;
+
+	if (*ppos + count >= plm->len)
+		count = plm->len - *ppos;
+
+	ret = -EFAULT;
+	if (copy_to_user(buf, plm->data + *ppos, count))
+		goto out;
+
+	*ppos += count;

I'd use simple_read_from_buffer() for this logic; it handles the range checking and copying for you.

security/selinux/selinuxfs.c static const struct file_operations sel_mls_ops = { · line 383

Quoted 6 lines
security/selinux/selinuxfs.c +6 -0
+	ret = count;
+out:
+	mutex_unlock(&sel_mutex);
+	return ret;
+}
+

Do we care that the caller can hold policy-size kernel memory allocated per open file instance? I guess your next patch introduces some limit on that.

Quoted 8 lines
security/selinux/ss/policydb.h +0 -0
index 26d9adf..9cbf9df 100644
@@ -299,6 +305,23 @@ static inline int next_entry(void *buf, struct policy_file *fp, size_t bytes)
 	return 0;
 }
 

security/selinux/ss/policydb.h static inline int next_entry(void *buf, struct policy_file *fp, size_t bytes) · line 317

Quoted 10 lines
security/selinux/ss/policydb.h +10 -0
+static inline int put_entry(void *buf, size_t bytes, int num, struct policy_file *fp)
+{
+	size_t len = bytes * num;
+
+	/*
+	 * this could happen if there was a policy load between the call to
+	 * security_policydb_len() and security_policydb_read()
+	 */
+	if (len > fp->len)
+		return -ENOMEM;

Not if you are holding sel_mutex in selinuxfs around the entire operation. No possible interleaving of load with read in that situation, right?

Quoted 15 lines
security/selinux/ss/policydb.h +7 -0
+
+	memcpy(fp->data, buf, len);
+	fp->data += len;
+	fp->len -= len;
+	return 0;
+}
+
 extern u16 string_to_security_class(struct policydb *p, const char *name);
 extern u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name);
 
security/selinux/ss/services.c +0 -0
index 1de60ce..4d4bc46 100644

security/selinux/ss/services.c static DEFINE_RWLOCK(policy_rwlock); · line 80

Quoted 4 lines
security/selinux/ss/services.c +1 -0
@@ -77,6 +77,7 @@ static DEFINE_RWLOCK(policy_rwlock);
 
 static struct sidtab sidtab;
 struct policydb policydb;
+static size_t policydb_len;

Why not just make it a field within the policydb? Longer term I want sidtab, policydb, and any other global policy state (e.g. current_mapping) wrapped up in a single structure with a single global pointer used to dereference it. Then policy switch can be done as a single pointer change.

Quoted 30 lines
security/selinux/ss/services.c +26 -0
@@ -3126,3 +3134,49 @@ netlbl_sid_to_secattr_failure:
 	return rc;
 }
 #endif /* CONFIG_NETLABEL */
+
+/**
+ * security_read_policy - read the policy.
+ * @data: binary policy data
+ * @len: length of data in bytes
+ *
+ */
+int security_read_policy(void **data, ssize_t *len)
+{
+	int rc;
+	struct policy_file fp;
+	u32 last_grant;
+
+	if (!ss_initialized)
+		return -EINVAL;
+again:
+	read_lock(&policy_rwlock);
+	*len = security_policydb_len();
+	last_grant = latest_granting;
+	read_unlock(&policy_rwlock);
+
+	*data = vmalloc(*len);
+	if (!*data)
+		return -ENOMEM;
+
+	fp.data = *data;

security/selinux/ss/services.c netlbl_sid_to_secattr_failure: · line 3172

Quoted 10 lines
security/selinux/ss/services.c +10 -0
+	fp.len = *len;
+
+	read_lock(&policy_rwlock);
+
+	/* check if a policy load happened while we were allocating memory */
+	if (last_grant != latest_granting) {
+		read_unlock(&policy_rwlock);
+		vfree(*data);
+		goto again;
+	}

Not possible if holding sel_mutex across the entire operation, right?

Quoted 10 lines
security/selinux/ss/services.c +10 -0
+
+	rc = policydb_write(&policydb, &fp);
+	read_unlock(&policy_rwlock);
+	if (rc)
+		return rc;
+
+	*len = (unsigned long)fp.data - (unsigned long)*data;
+	return 0;
+
+}
Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.