kernel: rounddown helper function
PATCH 1/6 kernel: rounddown helper function
The roundup() helper function will round a given value up to a multiple of another given value. aka roundup(11, 7) would give 14 = 7 * 2. This new function does the opposite. It will round a given number down to the nearest multiple of the second number: rounddown(11, 7) would give 7.
I need this in some future SELinux code and can carry the macro myself, but figured I would put it in the core kernel so others might find and use it if need be.
---
include/linux/kernel.h | 6 ++++++ 1 files changed, 6 insertions(+), 0 deletions(-)
include/linux/kernel.h +6 -0
@@ -59,6 +59,12 @@ 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) ( \ +{ \ + typeof(x) __x = (x); \ + __x - (__x % (y)); \ +} \ +)#define DIV_ROUND_CLOSEST (x, divisor)( \ { \ typeof(divisor) __divisor = divisor; \
--
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.
PATCH 2/6 kernel: roundup should only reference arguments once
Currently the roundup macro references it's arguments more than one time. This patch changes it so it will only use its arguments once.
---
include/linux/kernel.h | 7 ++++++- 1 files changed, 6 insertions(+), 1 deletions(-)
include/linux/kernel.h +6 -1
@@ -58,7 +58,12 @@ 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 roundup (x, y) ( \ +{ \ + typeof(y) __y = y; \ + (((x) + (__y - 1)) / __y) * __y; \ +} \ +)#define rounddown (x, y) ( \ { \ typeof(x) __x = (x); \
--
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.
PATCH 3/6 SELinux: deterministic ordering of range transition rules
In reply to Eric ParisRange transition rules are placed in the hash table in an (almost) arbitrary order. This patch inserts them in a fixed order to make policy retrival more predictable.
---
security/selinux/ss/policydb.c | 16 +++++++++++++--- 1 files changed, 13 insertions(+), 3 deletions(-)
security/selinux/ss/policydb.c +13 -3
@@ -185,9 +185,19 @@ static u32 rangetr_hash(struct hashtab *h, const void *k)static int rangetr_cmp (struct hashtab *h,const void *k1,const void *k2) {const struct range_trans *key1 = k1, *key2 = k2; -return (key1->source_type != key2->source_type || - key1->target_type != key2->target_type || - key1->target_class != key2->target_class); +int v; + + v = key1->source_type - key2->source_type; +if (v) +return v; + + v = key1->target_type - key2->target_type; +if (v) +return v; + + v = key1->target_class - key2->target_class; + +return v; }/*
--
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.
PATCH 4/6 SELinux: drop useless (and incorrect) AVTAB_MAX_SIZE
In reply to Eric ParisAVTAB_MAX_SIZE was a define which was supposed to be used in userspace to define a maximally sized avtab when userspace wasn't sure how big of a table it needed. It doesn't make sense in the kernel since we always know our table sizes. The only place it is used we have a more appropiately named define called AVTAB_MAX_HASH_BUCKETS, use that instead.
---
security/selinux/ss/avtab.c | 4 ++-- security/selinux/ss/avtab.h | 1 - 2 files changed, 2 insertions(+), 3 deletions(-)
security/selinux/ss/avtab.c +2 -2
@@ -266,8 +266,8 @@ int avtab_alloc(struct avtab *h, u32 nrules)if (shift >2 ) shift = shift -2 ; nslot =1 << shift; -if (nslot >MAX_AVTAB_SIZE ) - nslot =MAX_AVTAB_SIZE ; +if (nslot >MAX_AVTAB_HASH_BUCKETS ) + nslot =MAX_AVTAB_HASH_BUCKETS ; mask = nslot -1 ; h->htable =kcalloc (nslot,sizeof (*(h->htable)),GFP_KERNEL );
security/selinux/ss/avtab.h +0 -1
@@ -85,7 +85,6 @@ void avtab_cache_destroy(void);#define MAX_AVTAB_HASH_BITS 11#define MAX_AVTAB_HASH_BUCKETS (1 << MAX_AVTAB_HASH_BITS)#define MAX_AVTAB_HASH_MASK (MAX_AVTAB_HASH_BUCKETS-1) -#define MAX_AVTAB_SIZE MAX_AVTAB_HASH_BUCKETS#endif /* _SS_AVTAB_H_ */
--
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.
PATCH 5/6 SELinux: allow userspace to read policy back out of the kernel
In reply to Eric ParisThere 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.
---
security/selinux/include/classmap.h | 2 security/selinux/include/security.h | 2 security/selinux/selinuxfs.c | 95 ++++ 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 | 841 +++++++++++++++++++++++++++++++++++ security/selinux/ss/policydb.h | 20 + security/selinux/ss/services.c | 48 ++ 12 files changed, 1256 insertions(+), 3 deletions(-)
security/selinux/include/classmap.h +1 -1
@@ -17,7 +17,7 @@ struct security_class_mapping secclass_map[] = { {"compute_av" ,"compute_create" ,"compute_member" ,"check_context" ,"load_policy" ,"compute_relabel" ,"compute_user" ,"setenforce" ,"setbool" ,"setsecparam" , - "setcheckreqprot", NULL } }, + "setcheckreqprot", "read_policy", NULL } }, { "process", { "fork", "transition", "sigchld", "sigkill", "sigstop", "signull", "signal", "ptrace", "getsched", "setsched",
security/selinux/include/security.h +2 -0
@@ -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 +95 -0
119 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
@@ -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
@@ -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
@@ -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 +841 -0
850 lines not on this page · raw message
security/selinux/ss/policydb.h +20 -0
@@ -254,6 +254,9 @@ struct policydb {struct ebitmap permissive_map; +/* length of this policy when it was loaded */ +size_t len; +unsigned int policyvers; unsigned int reject_unknown : 1;
@@ -270,6 +273,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
@@ -290,6 +294,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)
@@ -301,6 +310,17 @@ 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; + +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 +48 -0
69 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.
Re: [PATCH 5/6] SELinux: allow userspace to read policy back out of the kernel
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.
To get bit-for-bit copies of the policy back from the kernel you have to patch userspace so it sends objects in the order the kernel expects and uses them, thus the kernel can write them back in that same order.
The two patches I used to make this situation true are attached.
-Eric
include/sepol/policydb/policydb.h +1 -3
@@ -201,8 +201,6 @@ typedef struct range_trans {uint32_t source_type;uint32_t target_type;uint32_t target_class; -mls_range_t target_range; -struct range_trans *next; } range_trans_t;/* Boolean data type */
@@ -527,7 +525,7 @@ typedef struct policydb {genfs_t *genfs;/* range transitions */ -range_trans_t *range_tr; +hashtab_t range_tr;ebitmap_t *type_attr_map;
src/expand.c +35 -35
98 lines not on this page · raw message
src/mls.c +10 -10
@@ -608,22 +608,22 @@ int mls_compute_sid(policydb_t * policydsepol_security_class_t tclass, uint32_t specified, context_struct_t * newcontext) { - range_trans_t *rtr; + range_trans_t rt; +mls_range_t *r;if (!policydb->mls)return 0 ;switch (specified) {case AVTAB_TRANSITION : + rt.source_type = scontext->type; + rt.target_type = tcontext->type; + rt.target_class = tclass; +/* Look for a range transition rule. */ -for (rtr = policydb->range_tr; rtr; rtr = rtr->next) { -if (rtr->source_type == scontext->type && - rtr->target_type == tcontext->type && - rtr->target_class == tclass) { -/* Set the range from the rule */ -return mls_range_set (newcontext, - &rtr->target_range); - } - } + r =hashtab_search (policydb->range_tr, (hashtab_key_t)&rt); +/* Set the range from the rule */ +if (r) +return mls_range_set (newcontext, r);/* Fallthrough */ case AVTAB_CHANGE :if (tclass ==SECCLASS_PROCESS )
src/policydb.c +122 -70
258 lines not on this page · raw message
src/write.c +83 -38
133 lines not on this page · raw message
Re: [PATCH 5/6] SELinux: allow userspace to read policy back out of the kernel
In reply to Eric Parissecurity/selinux/ss/policydb.c bad: · line 3032
Quoted 6 lines · Eric Paris
security/selinux/ss/policydb.c +6
-0+/* +* Write the configuration data in a policy database +* structure to a policy database binary representation +* file. +*/ +int policydb_write (struct policydb *p,void *fp)
Perhaps check that fp->len is still the same as policydb.len here before writing to the buffer.
Signature · 7 lines
James Morris <[email protected]> -- 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.
PATCH 6/6 selinux: implement mmap on /selinux/policy
In reply to 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 ++++++++++++++++++++++++++++++++++++++++ security/selinux/ss/services.c | 2 +- 2 files changed, 45 insertions(+), 1 deletions(-)
security/selinux/selinuxfs.c +44 -0
@@ -439,9 +439,53 @@ 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, + .page_mkwrite = sel_mmap_policy_fault, +}; + +int sel_mmap_policy (struct file *filp,struct vm_area_struct *vma) +{ +if (vma->vm_flags &VM_SHARED ) { +/* do not allow mprotect to make mapping writable */ + vma->vm_flags &= ~VM_MAYWRITE ; + +if (vma->vm_flags &VM_WRITE ) +return -EACCES ; + } + + 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, };
security/selinux/ss/services.c +1 -1
@@ -3162,7 +3162,7 @@ int security_read_policy(void **data, ssize_t *len) *len =security_policydb_len (); - *data =vmalloc (*len); + *data =vmalloc_user (*len);if (!*data)return -ENOMEM ;
--
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.
Quoted 10 lines · Eric Paris
The
roundup()helper function will round a given value up to a multiple of another given value. akaroundup(11, 7)would give 14 = 7 * 2. This new function does the opposite. It will round a given number down to the nearest multiple of the second number:rounddown(11, 7)would give 7.I need this in some future SELinux code and can carry the macro myself, but figured I would put it in the core kernel so others might find and use it if need be.
Signed-off-by Eric Paris <[email protected]>
All applied togit://git.kernel.org/pub/scm/linux/kernel/git/jmorris/security-testing-2.6#next
Signature · 7 lines
James Morris <[email protected]> -- 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.