Skip to content

Commit c312feb

Browse files
eparisLinus Torvalds
authored andcommitted
[PATCH] SELinux: decouple fscontext/context mount options
Remove the conflict between fscontext and context mount options. If context= is specified without fscontext it will operate just as before, if both are specified we will use mount point labeling and all inodes will get the label specified by context=. The superblock will be labeled with the label of fscontext=, thus affecting operations which check the superblock security context, such as associate permissions. Signed-off-by: Eric Paris <eparis@parisplace.org> Acked-by: Stephen Smalley <sds@tycho.nsa.gov> Signed-off-by: James Morris <jmorris@namei.org> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 2ed6e34 commit c312feb

2 files changed

Lines changed: 56 additions & 16 deletions

File tree

‎security/selinux/hooks.c‎

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ static int superblock_alloc_security(struct super_block *sb)
246246
sbsec->sb = sb;
247247
sbsec->sid = SECINITSID_UNLABELED;
248248
sbsec->def_sid = SECINITSID_FILE;
249+
sbsec->mntpoint_sid = SECINITSID_UNLABELED;
249250
sb->s_security = sbsec;
250251

251252
return 0;
@@ -329,9 +330,26 @@ static match_table_t tokens = {
329330

330331
#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
331332

333+
static int may_context_mount_sb_relabel(u32 sid,
334+
struct superblock_security_struct *sbsec,
335+
struct task_security_struct *tsec)
336+
{
337+
int rc;
338+
339+
rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
340+
FILESYSTEM__RELABELFROM, NULL);
341+
if (rc)
342+
return rc;
343+
344+
rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
345+
FILESYSTEM__RELABELTO, NULL);
346+
return rc;
347+
}
348+
332349
static int try_context_mount(struct super_block *sb, void *data)
333350
{
334351
char *context = NULL, *defcontext = NULL;
352+
char *fscontext = NULL;
335353
const char *name;
336354
u32 sid;
337355
int alloc = 0, rc = 0, seen = 0;
@@ -374,7 +392,7 @@ static int try_context_mount(struct super_block *sb, void *data)
374392

375393
switch (token) {
376394
case Opt_context:
377-
if (seen) {
395+
if (seen & (Opt_context|Opt_defcontext)) {
378396
rc = -EINVAL;
379397
printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
380398
goto out_free;
@@ -390,13 +408,13 @@ static int try_context_mount(struct super_block *sb, void *data)
390408
break;
391409

392410
case Opt_fscontext:
393-
if (seen & (Opt_context|Opt_fscontext)) {
411+
if (seen & Opt_fscontext) {
394412
rc = -EINVAL;
395413
printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
396414
goto out_free;
397415
}
398-
context = match_strdup(&args[0]);
399-
if (!context) {
416+
fscontext = match_strdup(&args[0]);
417+
if (!fscontext) {
400418
rc = -ENOMEM;
401419
goto out_free;
402420
}
@@ -441,29 +459,46 @@ static int try_context_mount(struct super_block *sb, void *data)
441459
if (!seen)
442460
goto out;
443461

444-
if (context) {
445-
rc = security_context_to_sid(context, strlen(context), &sid);
462+
/* sets the context of the superblock for the fs being mounted. */
463+
if (fscontext) {
464+
rc = security_context_to_sid(fscontext, strlen(fscontext), &sid);
446465
if (rc) {
447466
printk(KERN_WARNING "SELinux: security_context_to_sid"
448467
"(%s) failed for (dev %s, type %s) errno=%d\n",
449-
context, sb->s_id, name, rc);
468+
fscontext, sb->s_id, name, rc);
450469
goto out_free;
451470
}
452471

453-
rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
454-
FILESYSTEM__RELABELFROM, NULL);
472+
rc = may_context_mount_sb_relabel(sid, sbsec, tsec);
455473
if (rc)
456474
goto out_free;
457475

458-
rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
459-
FILESYSTEM__RELABELTO, NULL);
476+
sbsec->sid = sid;
477+
}
478+
479+
/*
480+
* Switch to using mount point labeling behavior.
481+
* sets the label used on all file below the mountpoint, and will set
482+
* the superblock context if not already set.
483+
*/
484+
if (context) {
485+
rc = security_context_to_sid(context, strlen(context), &sid);
486+
if (rc) {
487+
printk(KERN_WARNING "SELinux: security_context_to_sid"
488+
"(%s) failed for (dev %s, type %s) errno=%d\n",
489+
context, sb->s_id, name, rc);
490+
goto out_free;
491+
}
492+
493+
rc = may_context_mount_sb_relabel(sid, sbsec, tsec);
460494
if (rc)
461495
goto out_free;
462496

463-
sbsec->sid = sid;
497+
if (!fscontext)
498+
sbsec->sid = sid;
499+
sbsec->mntpoint_sid = sid;
464500

465-
if (seen & Opt_context)
466-
sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
501+
sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
467502
}
468503

469504
if (defcontext) {
@@ -495,6 +530,7 @@ static int try_context_mount(struct super_block *sb, void *data)
495530
if (alloc) {
496531
kfree(context);
497532
kfree(defcontext);
533+
kfree(fscontext);
498534
}
499535
out:
500536
return rc;
@@ -876,8 +912,11 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent
876912
goto out;
877913
isec->sid = sid;
878914
break;
915+
case SECURITY_FS_USE_MNTPOINT:
916+
isec->sid = sbsec->mntpoint_sid;
917+
break;
879918
default:
880-
/* Default to the fs SID. */
919+
/* Default to the fs superblock SID. */
881920
isec->sid = sbsec->sid;
882921

883922
if (sbsec->proc) {

‎security/selinux/include/objsec.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ struct file_security_struct {
5757
struct superblock_security_struct {
5858
struct super_block *sb; /* back pointer to sb object */
5959
struct list_head list; /* list of superblock_security_struct */
60-
u32 sid; /* SID of file system */
60+
u32 sid; /* SID of file system superblock */
6161
u32 def_sid; /* default SID for labeling */
62+
u32 mntpoint_sid; /* SECURITY_FS_USE_MNTPOINT context for files */
6263
unsigned int behavior; /* labeling behavior */
6364
unsigned char initialized; /* initialization flag */
6465
unsigned char proc; /* proc fs */

0 commit comments

Comments
 (0)