Skip to content

Commit 5e41ff9

Browse files
stephensmalleyLinus Torvalds
authored andcommitted
[PATCH] security: enable atomic inode security labeling
The following patch set enables atomic security labeling of newly created inodes by altering the fs code to invoke a new LSM hook to obtain the security attribute to apply to a newly created inode and to set up the incore inode security state during the inode creation transaction. This parallels the existing processing for setting ACLs on newly created inodes. Otherwise, it is possible for new inodes to be accessed by another thread via the dcache prior to complete security setup (presently handled by the post_create/mkdir/... LSM hooks in the VFS) and a newly created inode may be left unlabeled on the disk in the event of a crash. SELinux presently works around the issue by ensuring that the incore inode security label is initialized to a special SID that is inaccessible to unprivileged processes (in accordance with policy), thereby preventing inappropriate access but potentially causing false denials on legitimate accesses. A simple test program demonstrates such false denials on SELinux, and the patch solves the problem. Similar such false denials have been encountered in real applications. This patch defines a new inode_init_security LSM hook to obtain the security attribute to apply to a newly created inode and to set up the incore inode security state for it, and adds a corresponding hook function implementation to SELinux. Signed-off-by: Stephen Smalley <sds@tycho.nsa.gov> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent f5ee56c commit 5e41ff9

4 files changed

Lines changed: 108 additions & 0 deletions

File tree

‎include/linux/security.h‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,25 @@ struct swap_info_struct;
250250
* @inode contains the inode structure.
251251
* Deallocate the inode security structure and set @inode->i_security to
252252
* NULL.
253+
* @inode_init_security:
254+
* Obtain the security attribute name suffix and value to set on a newly
255+
* created inode and set up the incore security field for the new inode.
256+
* This hook is called by the fs code as part of the inode creation
257+
* transaction and provides for atomic labeling of the inode, unlike
258+
* the post_create/mkdir/... hooks called by the VFS. The hook function
259+
* is expected to allocate the name and value via kmalloc, with the caller
260+
* being responsible for calling kfree after using them.
261+
* If the security module does not use security attributes or does
262+
* not wish to put a security attribute on this particular inode,
263+
* then it should return -EOPNOTSUPP to skip this processing.
264+
* @inode contains the inode structure of the newly created inode.
265+
* @dir contains the inode structure of the parent directory.
266+
* @name will be set to the allocated name suffix (e.g. selinux).
267+
* @value will be set to the allocated attribute value.
268+
* @len will be set to the length of the value.
269+
* Returns 0 if @name and @value have been successfully set,
270+
* -EOPNOTSUPP if no security attribute is needed, or
271+
* -ENOMEM on memory allocation failure.
253272
* @inode_create:
254273
* Check permission to create a regular file.
255274
* @dir contains inode structure of the parent of the new file.
@@ -1080,6 +1099,8 @@ struct security_operations {
10801099

10811100
int (*inode_alloc_security) (struct inode *inode);
10821101
void (*inode_free_security) (struct inode *inode);
1102+
int (*inode_init_security) (struct inode *inode, struct inode *dir,
1103+
char **name, void **value, size_t *len);
10831104
int (*inode_create) (struct inode *dir,
10841105
struct dentry *dentry, int mode);
10851106
void (*inode_post_create) (struct inode *dir,
@@ -1442,6 +1463,17 @@ static inline void security_inode_free (struct inode *inode)
14421463
return;
14431464
security_ops->inode_free_security (inode);
14441465
}
1466+
1467+
static inline int security_inode_init_security (struct inode *inode,
1468+
struct inode *dir,
1469+
char **name,
1470+
void **value,
1471+
size_t *len)
1472+
{
1473+
if (unlikely (IS_PRIVATE (inode)))
1474+
return -EOPNOTSUPP;
1475+
return security_ops->inode_init_security (inode, dir, name, value, len);
1476+
}
14451477

14461478
static inline int security_inode_create (struct inode *dir,
14471479
struct dentry *dentry,
@@ -2171,6 +2203,15 @@ static inline int security_inode_alloc (struct inode *inode)
21712203

21722204
static inline void security_inode_free (struct inode *inode)
21732205
{ }
2206+
2207+
static inline int security_inode_init_security (struct inode *inode,
2208+
struct inode *dir,
2209+
char **name,
2210+
void **value,
2211+
size_t *len)
2212+
{
2213+
return -EOPNOTSUPP;
2214+
}
21742215

21752216
static inline int security_inode_create (struct inode *dir,
21762217
struct dentry *dentry,

‎security/dummy.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,12 @@ static void dummy_inode_free_security (struct inode *inode)
258258
return;
259259
}
260260

261+
static int dummy_inode_init_security (struct inode *inode, struct inode *dir,
262+
char **name, void **value, size_t *len)
263+
{
264+
return -EOPNOTSUPP;
265+
}
266+
261267
static int dummy_inode_create (struct inode *inode, struct dentry *dentry,
262268
int mask)
263269
{
@@ -886,6 +892,7 @@ void security_fixup_ops (struct security_operations *ops)
886892
set_to_dummy_if_null(ops, sb_post_pivotroot);
887893
set_to_dummy_if_null(ops, inode_alloc_security);
888894
set_to_dummy_if_null(ops, inode_free_security);
895+
set_to_dummy_if_null(ops, inode_init_security);
889896
set_to_dummy_if_null(ops, inode_create);
890897
set_to_dummy_if_null(ops, inode_post_create);
891898
set_to_dummy_if_null(ops, inode_link);

‎security/selinux/hooks.c‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,7 @@ static int post_create(struct inode *dir,
12741274
struct inode *inode;
12751275
struct inode_security_struct *dsec;
12761276
struct superblock_security_struct *sbsec;
1277+
struct inode_security_struct *isec;
12771278
u32 newsid;
12781279
char *context;
12791280
unsigned int len;
@@ -1293,6 +1294,11 @@ static int post_create(struct inode *dir,
12931294
return 0;
12941295
}
12951296

1297+
isec = inode->i_security;
1298+
1299+
if (isec->security_attr_init)
1300+
return 0;
1301+
12961302
if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) {
12971303
newsid = tsec->create_sid;
12981304
} else {
@@ -2018,6 +2024,58 @@ static void selinux_inode_free_security(struct inode *inode)
20182024
inode_free_security(inode);
20192025
}
20202026

2027+
static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
2028+
char **name, void **value,
2029+
size_t *len)
2030+
{
2031+
struct task_security_struct *tsec;
2032+
struct inode_security_struct *dsec;
2033+
struct superblock_security_struct *sbsec;
2034+
struct inode_security_struct *isec;
2035+
u32 newsid;
2036+
int rc;
2037+
char *namep, *context;
2038+
2039+
tsec = current->security;
2040+
dsec = dir->i_security;
2041+
sbsec = dir->i_sb->s_security;
2042+
isec = inode->i_security;
2043+
2044+
if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) {
2045+
newsid = tsec->create_sid;
2046+
} else {
2047+
rc = security_transition_sid(tsec->sid, dsec->sid,
2048+
inode_mode_to_security_class(inode->i_mode),
2049+
&newsid);
2050+
if (rc) {
2051+
printk(KERN_WARNING "%s: "
2052+
"security_transition_sid failed, rc=%d (dev=%s "
2053+
"ino=%ld)\n",
2054+
__FUNCTION__,
2055+
-rc, inode->i_sb->s_id, inode->i_ino);
2056+
return rc;
2057+
}
2058+
}
2059+
2060+
inode_security_set_sid(inode, newsid);
2061+
2062+
namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_KERNEL);
2063+
if (!namep)
2064+
return -ENOMEM;
2065+
*name = namep;
2066+
2067+
rc = security_sid_to_context(newsid, &context, len);
2068+
if (rc) {
2069+
kfree(namep);
2070+
return rc;
2071+
}
2072+
*value = context;
2073+
2074+
isec->security_attr_init = 1;
2075+
2076+
return 0;
2077+
}
2078+
20212079
static int selinux_inode_create(struct inode *dir, struct dentry *dentry, int mask)
20222080
{
20232081
return may_create(dir, dentry, SECCLASS_FILE);
@@ -4298,6 +4356,7 @@ static struct security_operations selinux_ops = {
42984356

42994357
.inode_alloc_security = selinux_inode_alloc_security,
43004358
.inode_free_security = selinux_inode_free_security,
4359+
.inode_init_security = selinux_inode_init_security,
43014360
.inode_create = selinux_inode_create,
43024361
.inode_post_create = selinux_inode_post_create,
43034362
.inode_link = selinux_inode_link,

‎security/selinux/include/objsec.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ struct inode_security_struct {
4646
unsigned char initialized; /* initialization flag */
4747
struct semaphore sem;
4848
unsigned char inherit; /* inherit SID from parent entry */
49+
unsigned char security_attr_init; /* security attributes init flag */
4950
};
5051

5152
struct file_security_struct {

0 commit comments

Comments
 (0)