Skip to content

Commit 2397074

Browse files
eparisLinus Torvalds
authored andcommitted
[PATCH] SELinux: change isec semaphore to a mutex
This patch converts the remaining isec->sem into a mutex. Very similar locking is provided as before only in the faster smaller mutex rather than a semaphore. An out_unlock path is introduced rather than the conditional unlocking found in the original code. Signed-off-by: Eric Paris <eparis@redhat.com> Acked-by: Stephen Smalley <sds@tycho.nsa.gov> Acked-by: James Morris <jmorris@namei.org> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 296fddf commit 2397074

3 files changed

Lines changed: 17 additions & 19 deletions

File tree

‎security/selinux/hooks.c‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
#include <linux/audit.h>
7272
#include <linux/string.h>
7373
#include <linux/selinux.h>
74+
#include <linux/mutex.h>
7475

7576
#include "avc.h"
7677
#include "objsec.h"
@@ -185,7 +186,7 @@ static int inode_alloc_security(struct inode *inode)
185186
return -ENOMEM;
186187

187188
memset(isec, 0, sizeof(*isec));
188-
init_MUTEX(&isec->sem);
189+
mutex_init(&isec->lock);
189190
INIT_LIST_HEAD(&isec->list);
190191
isec->inode = inode;
191192
isec->sid = SECINITSID_UNLABELED;
@@ -843,15 +844,13 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent
843844
char *context = NULL;
844845
unsigned len = 0;
845846
int rc = 0;
846-
int hold_sem = 0;
847847

848848
if (isec->initialized)
849849
goto out;
850850

851-
down(&isec->sem);
852-
hold_sem = 1;
851+
mutex_lock(&isec->lock);
853852
if (isec->initialized)
854-
goto out;
853+
goto out_unlock;
855854

856855
sbsec = inode->i_sb->s_security;
857856
if (!sbsec->initialized) {
@@ -862,7 +861,7 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent
862861
if (list_empty(&isec->list))
863862
list_add(&isec->list, &sbsec->isec_head);
864863
spin_unlock(&sbsec->isec_lock);
865-
goto out;
864+
goto out_unlock;
866865
}
867866

868867
switch (sbsec->behavior) {
@@ -885,15 +884,15 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent
885884
printk(KERN_WARNING "%s: no dentry for dev=%s "
886885
"ino=%ld\n", __FUNCTION__, inode->i_sb->s_id,
887886
inode->i_ino);
888-
goto out;
887+
goto out_unlock;
889888
}
890889

891890
len = INITCONTEXTLEN;
892891
context = kmalloc(len, GFP_KERNEL);
893892
if (!context) {
894893
rc = -ENOMEM;
895894
dput(dentry);
896-
goto out;
895+
goto out_unlock;
897896
}
898897
rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
899898
context, len);
@@ -903,15 +902,15 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent
903902
NULL, 0);
904903
if (rc < 0) {
905904
dput(dentry);
906-
goto out;
905+
goto out_unlock;
907906
}
908907
kfree(context);
909908
len = rc;
910909
context = kmalloc(len, GFP_KERNEL);
911910
if (!context) {
912911
rc = -ENOMEM;
913912
dput(dentry);
914-
goto out;
913+
goto out_unlock;
915914
}
916915
rc = inode->i_op->getxattr(dentry,
917916
XATTR_NAME_SELINUX,
@@ -924,7 +923,7 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent
924923
"%d for dev=%s ino=%ld\n", __FUNCTION__,
925924
-rc, inode->i_sb->s_id, inode->i_ino);
926925
kfree(context);
927-
goto out;
926+
goto out_unlock;
928927
}
929928
/* Map ENODATA to the default file SID */
930929
sid = sbsec->def_sid;
@@ -960,7 +959,7 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent
960959
isec->sclass,
961960
&sid);
962961
if (rc)
963-
goto out;
962+
goto out_unlock;
964963
isec->sid = sid;
965964
break;
966965
case SECURITY_FS_USE_MNTPOINT:
@@ -978,7 +977,7 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent
978977
isec->sclass,
979978
&sid);
980979
if (rc)
981-
goto out;
980+
goto out_unlock;
982981
isec->sid = sid;
983982
}
984983
}
@@ -987,12 +986,11 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent
987986

988987
isec->initialized = 1;
989988

989+
out_unlock:
990+
mutex_unlock(&isec->lock);
990991
out:
991992
if (isec->sclass == SECCLASS_FILE)
992993
isec->sclass = inode_mode_to_security_class(inode->i_mode);
993-
994-
if (hold_sem)
995-
up(&isec->sem);
996994
return rc;
997995
}
998996

‎security/selinux/include/objsec.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct inode_security_struct {
4444
u32 sid; /* SID of this object */
4545
u16 sclass; /* security class of this object */
4646
unsigned char initialized; /* initialization flag */
47-
struct semaphore sem;
47+
struct mutex lock;
4848
unsigned char inherit; /* inherit SID from parent entry */
4949
};
5050

‎security/selinux/ss/services.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2578,15 +2578,15 @@ int selinux_netlbl_inode_permission(struct inode *inode, int mask)
25782578
sock = SOCKET_I(inode);
25792579
isec = inode->i_security;
25802580
sksec = sock->sk->sk_security;
2581-
down(&isec->sem);
2581+
mutex_lock(&isec->lock);
25822582
if (unlikely(sksec->nlbl_state == NLBL_REQUIRE &&
25832583
(mask & (MAY_WRITE | MAY_APPEND)))) {
25842584
lock_sock(sock->sk);
25852585
rc = selinux_netlbl_socket_setsid(sock, sksec->sid);
25862586
release_sock(sock->sk);
25872587
} else
25882588
rc = 0;
2589-
up(&isec->sem);
2589+
mutex_unlock(&isec->lock);
25902590

25912591
return rc;
25922592
}

0 commit comments

Comments
 (0)