Skip to content
This repository has been archived by the owner on Sep 18, 2022. It is now read-only.

Commit

Permalink
selinux: enable per-file labeling for debugfs files.
Browse files Browse the repository at this point in the history
upstream commit 6f29997f4a3117169eeabd41dbea4c1bd94a739c

Add support for per-file labeling of debugfs files so that
we can distinguish them in policy.  This is particularly
important in Android where certain debugfs files have to be writable
by apps and therefore the debugfs directory tree can be read and
searched by all.

Since debugfs is entirely kernel-generated, the directory tree is
immutable by userspace, and the inodes are pinned in memory, we can
simply use the same approach as with proc and label the inodes from
policy based on pathname from the root of the debugfs filesystem.
Generalize the existing labeling support used for proc and reuse it
for debugfs too.

[sds:  Back-ported to 3.4.  superblock_security_struct flags field
is only unsigned char in 3.4 so we have to redefine SE_SBGENFS.
However, this definition is kernel-private, not exposed to userspace
or stored anywhere persistent.  Also depends on
I7742b4b7e53b45e4dd13d99c39553a927aa4a7e9.]

Change-Id: I6460fbed6bb6bd36eb8554ac8c4fdd574edf3b07
Signed-off-by: Stephen Smalley <[email protected]>
  • Loading branch information
stephensmalley authored and MSe1969 committed Nov 8, 2020
1 parent 588d67f commit 8d698cb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
43 changes: 21 additions & 22 deletions security/selinux/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,10 @@ static int selinux_set_mnt_opts(struct super_block *sb,
}

if (strcmp(sb->s_type->name, "proc") == 0)
sbsec->flags |= SE_SBPROC;
sbsec->flags |= SE_SBPROC | SE_SBGENFS;

if (strcmp(sb->s_type->name, "debugfs") == 0)
sbsec->flags |= SE_SBGENFS;

/* Determine the labeling behavior to use for this filesystem type. */
rc = security_fs_use((sbsec->flags & SE_SBPROC) ? "proc" : sb->s_type->name, &sbsec->behavior, &sbsec->sid);
Expand Down Expand Up @@ -1146,12 +1149,13 @@ static inline u16 socket_type_to_security_class(int family, int type, int protoc
return SECCLASS_SOCKET;
}

#ifdef CONFIG_PROC_FS
static int selinux_proc_get_sid(struct dentry *dentry,
u16 tclass,
u32 *sid)
static int selinux_genfs_get_sid(struct dentry *dentry,
u16 tclass,
u16 flags,
u32 *sid)
{
int rc;
struct super_block *sb = dentry->d_inode->i_sb;
char *buffer, *path;

buffer = (char *)__get_free_page(GFP_KERNEL);
Expand All @@ -1162,26 +1166,20 @@ static int selinux_proc_get_sid(struct dentry *dentry,
if (IS_ERR(path))
rc = PTR_ERR(path);
else {
/* each process gets a /proc/PID/ entry. Strip off the
* PID part to get a valid selinux labeling.
* e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
while (path[1] >= '0' && path[1] <= '9') {
path[1] = '/';
path++;
if (flags & SE_SBPROC) {
/* each process gets a /proc/PID/ entry. Strip off the
* PID part to get a valid selinux labeling.
* e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
while (path[1] >= '0' && path[1] <= '9') {
path[1] = '/';
path++;
}
}
rc = security_genfs_sid("proc", path, tclass, sid);
rc = security_genfs_sid(sb->s_type->name, path, tclass, sid);
}
free_page((unsigned long)buffer);
return rc;
}
#else
static int selinux_proc_get_sid(struct dentry *dentry,
u16 tclass,
u32 *sid)
{
return -EINVAL;
}
#endif

/* The inode's security attributes must be initialized before first use. */
static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
Expand Down Expand Up @@ -1336,7 +1334,7 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent
/* Default to the fs superblock SID. */
isec->sid = sbsec->sid;

if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) {
if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
/* We must have a dentry to determine the label on
* procfs inodes */
if (opt_dentry)
Expand All @@ -1359,7 +1357,8 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent
if (!dentry)
goto out_unlock;
isec->sclass = inode_mode_to_security_class(inode->i_mode);
rc = selinux_proc_get_sid(dentry, isec->sclass, &sid);
rc = selinux_genfs_get_sid(dentry, isec->sclass,
sbsec->flags, &sid);
dput(dentry);
if (rc)
goto out_unlock;
Expand Down
1 change: 1 addition & 0 deletions security/selinux/include/security.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#define SE_SBINITIALIZED 0x10
#define SE_SBPROC 0x20
#define SE_SBLABELSUPP 0x40
#define SE_SBGENFS 0x80

#define CONTEXT_STR "context="
#define FSCONTEXT_STR "fscontext="
Expand Down

0 comments on commit 8d698cb

Please sign in to comment.