Skip to content

Commit ecd5f82

Browse files
cschauflerkees
authored andcommitted
LSM: Infrastructure management of the ipc security blob
Move management of the kern_ipc_perm->security and msg_msg->security blobs out of the individual security modules and into the security infrastructure. Instead of allocating the blobs from within the modules the modules tell the infrastructure how much space is required, and the space is allocated there. Signed-off-by: Casey Schaufler <casey@schaufler-ca.com> Reviewed-by: Kees Cook <keescook@chromium.org> [kees: adjusted for ordered init series] Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent 019bcca commit ecd5f82

6 files changed

Lines changed: 110 additions & 121 deletions

File tree

‎include/linux/lsm_hooks.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,6 +2034,8 @@ struct lsm_blob_sizes {
20342034
int lbs_cred;
20352035
int lbs_file;
20362036
int lbs_inode;
2037+
int lbs_ipc;
2038+
int lbs_msg_msg;
20372039
int lbs_task;
20382040
};
20392041

‎security/security.c‎

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <linux/personality.h>
3131
#include <linux/backing-dev.h>
3232
#include <linux/string.h>
33+
#include <linux/msg.h>
3334
#include <net/flow.h>
3435

3536
#define MAX_LSM_EVM_XATTR 2
@@ -169,6 +170,8 @@ static void __init lsm_set_blob_sizes(struct lsm_blob_sizes *needed)
169170
if (needed->lbs_inode && blob_sizes.lbs_inode == 0)
170171
blob_sizes.lbs_inode = sizeof(struct rcu_head);
171172
lsm_set_blob_size(&needed->lbs_inode, &blob_sizes.lbs_inode);
173+
lsm_set_blob_size(&needed->lbs_ipc, &blob_sizes.lbs_ipc);
174+
lsm_set_blob_size(&needed->lbs_msg_msg, &blob_sizes.lbs_msg_msg);
172175
lsm_set_blob_size(&needed->lbs_task, &blob_sizes.lbs_task);
173176
}
174177

@@ -293,6 +296,8 @@ static void __init ordered_lsm_init(void)
293296
init_debug("cred blob size = %d\n", blob_sizes.lbs_cred);
294297
init_debug("file blob size = %d\n", blob_sizes.lbs_file);
295298
init_debug("inode blob size = %d\n", blob_sizes.lbs_inode);
299+
init_debug("ipc blob size = %d\n", blob_sizes.lbs_ipc);
300+
init_debug("msg_msg blob size = %d\n", blob_sizes.lbs_msg_msg);
296301
init_debug("task blob size = %d\n", blob_sizes.lbs_task);
297302

298303
/*
@@ -538,6 +543,48 @@ int lsm_task_alloc(struct task_struct *task)
538543
return 0;
539544
}
540545

546+
/**
547+
* lsm_ipc_alloc - allocate a composite ipc blob
548+
* @kip: the ipc that needs a blob
549+
*
550+
* Allocate the ipc blob for all the modules
551+
*
552+
* Returns 0, or -ENOMEM if memory can't be allocated.
553+
*/
554+
int lsm_ipc_alloc(struct kern_ipc_perm *kip)
555+
{
556+
if (blob_sizes.lbs_ipc == 0) {
557+
kip->security = NULL;
558+
return 0;
559+
}
560+
561+
kip->security = kzalloc(blob_sizes.lbs_ipc, GFP_KERNEL);
562+
if (kip->security == NULL)
563+
return -ENOMEM;
564+
return 0;
565+
}
566+
567+
/**
568+
* lsm_msg_msg_alloc - allocate a composite msg_msg blob
569+
* @mp: the msg_msg that needs a blob
570+
*
571+
* Allocate the ipc blob for all the modules
572+
*
573+
* Returns 0, or -ENOMEM if memory can't be allocated.
574+
*/
575+
int lsm_msg_msg_alloc(struct msg_msg *mp)
576+
{
577+
if (blob_sizes.lbs_msg_msg == 0) {
578+
mp->security = NULL;
579+
return 0;
580+
}
581+
582+
mp->security = kzalloc(blob_sizes.lbs_msg_msg, GFP_KERNEL);
583+
if (mp->security == NULL)
584+
return -ENOMEM;
585+
return 0;
586+
}
587+
541588
/**
542589
* lsm_early_task - during initialization allocate a composite task blob
543590
* @task: the task that needs a blob
@@ -1631,22 +1678,40 @@ void security_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
16311678

16321679
int security_msg_msg_alloc(struct msg_msg *msg)
16331680
{
1634-
return call_int_hook(msg_msg_alloc_security, 0, msg);
1681+
int rc = lsm_msg_msg_alloc(msg);
1682+
1683+
if (unlikely(rc))
1684+
return rc;
1685+
rc = call_int_hook(msg_msg_alloc_security, 0, msg);
1686+
if (unlikely(rc))
1687+
security_msg_msg_free(msg);
1688+
return rc;
16351689
}
16361690

16371691
void security_msg_msg_free(struct msg_msg *msg)
16381692
{
16391693
call_void_hook(msg_msg_free_security, msg);
1694+
kfree(msg->security);
1695+
msg->security = NULL;
16401696
}
16411697

16421698
int security_msg_queue_alloc(struct kern_ipc_perm *msq)
16431699
{
1644-
return call_int_hook(msg_queue_alloc_security, 0, msq);
1700+
int rc = lsm_ipc_alloc(msq);
1701+
1702+
if (unlikely(rc))
1703+
return rc;
1704+
rc = call_int_hook(msg_queue_alloc_security, 0, msq);
1705+
if (unlikely(rc))
1706+
security_msg_queue_free(msq);
1707+
return rc;
16451708
}
16461709

16471710
void security_msg_queue_free(struct kern_ipc_perm *msq)
16481711
{
16491712
call_void_hook(msg_queue_free_security, msq);
1713+
kfree(msq->security);
1714+
msq->security = NULL;
16501715
}
16511716

16521717
int security_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
@@ -1673,12 +1738,21 @@ int security_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
16731738

16741739
int security_shm_alloc(struct kern_ipc_perm *shp)
16751740
{
1676-
return call_int_hook(shm_alloc_security, 0, shp);
1741+
int rc = lsm_ipc_alloc(shp);
1742+
1743+
if (unlikely(rc))
1744+
return rc;
1745+
rc = call_int_hook(shm_alloc_security, 0, shp);
1746+
if (unlikely(rc))
1747+
security_shm_free(shp);
1748+
return rc;
16771749
}
16781750

16791751
void security_shm_free(struct kern_ipc_perm *shp)
16801752
{
16811753
call_void_hook(shm_free_security, shp);
1754+
kfree(shp->security);
1755+
shp->security = NULL;
16821756
}
16831757

16841758
int security_shm_associate(struct kern_ipc_perm *shp, int shmflg)
@@ -1698,12 +1772,21 @@ int security_shm_shmat(struct kern_ipc_perm *shp, char __user *shmaddr, int shmf
16981772

16991773
int security_sem_alloc(struct kern_ipc_perm *sma)
17001774
{
1701-
return call_int_hook(sem_alloc_security, 0, sma);
1775+
int rc = lsm_ipc_alloc(sma);
1776+
1777+
if (unlikely(rc))
1778+
return rc;
1779+
rc = call_int_hook(sem_alloc_security, 0, sma);
1780+
if (unlikely(rc))
1781+
security_sem_free(sma);
1782+
return rc;
17021783
}
17031784

17041785
void security_sem_free(struct kern_ipc_perm *sma)
17051786
{
17061787
call_void_hook(sem_free_security, sma);
1788+
kfree(sma->security);
1789+
sma->security = NULL;
17071790
}
17081791

17091792
int security_sem_associate(struct kern_ipc_perm *sma, int semflg)

‎security/selinux/hooks.c‎

Lines changed: 13 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -5626,51 +5626,22 @@ static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
56265626
return selinux_nlmsg_perm(sk, skb);
56275627
}
56285628

5629-
static int ipc_alloc_security(struct kern_ipc_perm *perm,
5630-
u16 sclass)
5629+
static void ipc_init_security(struct ipc_security_struct *isec, u16 sclass)
56315630
{
5632-
struct ipc_security_struct *isec;
5633-
5634-
isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
5635-
if (!isec)
5636-
return -ENOMEM;
5637-
56385631
isec->sclass = sclass;
56395632
isec->sid = current_sid();
5640-
perm->security = isec;
5641-
5642-
return 0;
5643-
}
5644-
5645-
static void ipc_free_security(struct kern_ipc_perm *perm)
5646-
{
5647-
struct ipc_security_struct *isec = perm->security;
5648-
perm->security = NULL;
5649-
kfree(isec);
56505633
}
56515634

56525635
static int msg_msg_alloc_security(struct msg_msg *msg)
56535636
{
56545637
struct msg_security_struct *msec;
56555638

5656-
msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
5657-
if (!msec)
5658-
return -ENOMEM;
5659-
5639+
msec = selinux_msg_msg(msg);
56605640
msec->sid = SECINITSID_UNLABELED;
5661-
msg->security = msec;
56625641

56635642
return 0;
56645643
}
56655644

5666-
static void msg_msg_free_security(struct msg_msg *msg)
5667-
{
5668-
struct msg_security_struct *msec = msg->security;
5669-
5670-
msg->security = NULL;
5671-
kfree(msec);
5672-
}
5673-
56745645
static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
56755646
u32 perms)
56765647
{
@@ -5692,11 +5663,6 @@ static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
56925663
return msg_msg_alloc_security(msg);
56935664
}
56945665

5695-
static void selinux_msg_msg_free_security(struct msg_msg *msg)
5696-
{
5697-
msg_msg_free_security(msg);
5698-
}
5699-
57005666
/* message queue security operations */
57015667
static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
57025668
{
@@ -5705,28 +5671,16 @@ static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
57055671
u32 sid = current_sid();
57065672
int rc;
57075673

5708-
rc = ipc_alloc_security(msq, SECCLASS_MSGQ);
5709-
if (rc)
5710-
return rc;
5711-
5712-
isec = msq->security;
5674+
isec = selinux_ipc(msq);
5675+
ipc_init_security(isec, SECCLASS_MSGQ);
57135676

57145677
ad.type = LSM_AUDIT_DATA_IPC;
57155678
ad.u.ipc_id = msq->key;
57165679

57175680
rc = avc_has_perm(&selinux_state,
57185681
sid, isec->sid, SECCLASS_MSGQ,
57195682
MSGQ__CREATE, &ad);
5720-
if (rc) {
5721-
ipc_free_security(msq);
5722-
return rc;
5723-
}
5724-
return 0;
5725-
}
5726-
5727-
static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq)
5728-
{
5729-
ipc_free_security(msq);
5683+
return rc;
57305684
}
57315685

57325686
static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
@@ -5856,28 +5810,16 @@ static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
58565810
u32 sid = current_sid();
58575811
int rc;
58585812

5859-
rc = ipc_alloc_security(shp, SECCLASS_SHM);
5860-
if (rc)
5861-
return rc;
5862-
5863-
isec = shp->security;
5813+
isec = selinux_ipc(shp);
5814+
ipc_init_security(isec, SECCLASS_SHM);
58645815

58655816
ad.type = LSM_AUDIT_DATA_IPC;
58665817
ad.u.ipc_id = shp->key;
58675818

58685819
rc = avc_has_perm(&selinux_state,
58695820
sid, isec->sid, SECCLASS_SHM,
58705821
SHM__CREATE, &ad);
5871-
if (rc) {
5872-
ipc_free_security(shp);
5873-
return rc;
5874-
}
5875-
return 0;
5876-
}
5877-
5878-
static void selinux_shm_free_security(struct kern_ipc_perm *shp)
5879-
{
5880-
ipc_free_security(shp);
5822+
return rc;
58815823
}
58825824

58835825
static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
@@ -5953,28 +5895,16 @@ static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
59535895
u32 sid = current_sid();
59545896
int rc;
59555897

5956-
rc = ipc_alloc_security(sma, SECCLASS_SEM);
5957-
if (rc)
5958-
return rc;
5959-
5960-
isec = sma->security;
5898+
isec = selinux_ipc(sma);
5899+
ipc_init_security(isec, SECCLASS_SEM);
59615900

59625901
ad.type = LSM_AUDIT_DATA_IPC;
59635902
ad.u.ipc_id = sma->key;
59645903

59655904
rc = avc_has_perm(&selinux_state,
59665905
sid, isec->sid, SECCLASS_SEM,
59675906
SEM__CREATE, &ad);
5968-
if (rc) {
5969-
ipc_free_security(sma);
5970-
return rc;
5971-
}
5972-
return 0;
5973-
}
5974-
5975-
static void selinux_sem_free_security(struct kern_ipc_perm *sma)
5976-
{
5977-
ipc_free_security(sma);
5907+
return rc;
59785908
}
59795909

59805910
static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
@@ -6607,6 +6537,8 @@ struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = {
66076537
.lbs_cred = sizeof(struct task_security_struct),
66086538
.lbs_file = sizeof(struct file_security_struct),
66096539
.lbs_inode = sizeof(struct inode_security_struct),
6540+
.lbs_ipc = sizeof(struct ipc_security_struct),
6541+
.lbs_msg_msg = sizeof(struct msg_security_struct),
66106542
};
66116543

66126544
static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
@@ -6718,24 +6650,20 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
67186650
LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
67196651

67206652
LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
6721-
LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security),
67226653

67236654
LSM_HOOK_INIT(msg_queue_alloc_security,
67246655
selinux_msg_queue_alloc_security),
6725-
LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security),
67266656
LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
67276657
LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
67286658
LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
67296659
LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
67306660

67316661
LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
6732-
LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security),
67336662
LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
67346663
LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
67356664
LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
67366665

67376666
LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
6738-
LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security),
67396667
LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
67406668
LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
67416669
LSM_HOOK_INIT(sem_semop, selinux_sem_semop),

‎security/selinux/include/objsec.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,13 @@ static inline struct inode_security_struct *selinux_inode(
179179
static inline struct msg_security_struct *selinux_msg_msg(
180180
const struct msg_msg *msg_msg)
181181
{
182-
return msg_msg->security;
182+
return msg_msg->security + selinux_blob_sizes.lbs_msg_msg;
183183
}
184184

185185
static inline struct ipc_security_struct *selinux_ipc(
186186
const struct kern_ipc_perm *ipc)
187187
{
188-
return ipc->security;
188+
return ipc->security + selinux_blob_sizes.lbs_ipc;
189189
}
190190

191191
#endif /* _SELINUX_OBJSEC_H_ */

‎security/smack/smack.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,12 +376,12 @@ static inline struct inode_smack *smack_inode(const struct inode *inode)
376376

377377
static inline struct smack_known **smack_msg_msg(const struct msg_msg *msg)
378378
{
379-
return (struct smack_known **)&msg->security;
379+
return msg->security + smack_blob_sizes.lbs_msg_msg;
380380
}
381381

382382
static inline struct smack_known **smack_ipc(const struct kern_ipc_perm *ipc)
383383
{
384-
return (struct smack_known **)&ipc->security;
384+
return ipc->security + smack_blob_sizes.lbs_ipc;
385385
}
386386

387387
/*

0 commit comments

Comments
 (0)