Skip to content

Commit 8e6c969

Browse files
luciangeparis
authored andcommitted
security/selinux: fix /proc/sys/ labeling
This fixes an old (2007) selinux regression: filesystem labeling for /proc/sys returned -r--r--r-- unknown /proc/sys/fs/file-nr instead of -r--r--r-- system_u:object_r:sysctl_fs_t:s0 /proc/sys/fs/file-nr Events that lead to breaking of /proc/sys/ selinux labeling: 1) sysctl was reimplemented to route all calls through /proc/sys/ commit 77b14db [PATCH] sysctl: reimplement the sysctl proc support 2) proc_dir_entry was removed from ctl_table: commit 3fbfa98 [PATCH] sysctl: remove the proc_dir_entry member for the sysctl tables 3) selinux still walked the proc_dir_entry tree to apply labeling. Because ctl_tables don't have a proc_dir_entry, we did not label /proc/sys/ inodes any more. To achieve this the /proc/sys/ inodes were marked private and private inodes were ignored by selinux. commit bbaca6c [PATCH] selinux: enhance selinux to always ignore private inodes commit 86a71db [PATCH] sysctl: hide the sysctl proc inodes from selinux Access control checks have been done by means of a special sysctl hook that was called for read/write accesses to any /proc/sys/ entry. We don't have to do this because, instead of walking the proc_dir_entry tree we can walk the dentry tree (as done in this patch). With this patch: * we don't mark /proc/sys/ inodes as private * we don't need the sysclt security hook * we walk the dentry tree to find the path to the inode. We have to strip the PID in /proc/PID/ entries that have a proc_dir_entry because selinux does not know how to label paths like '/1/net/rpc/nfsd.fh' (and defaults to 'proc_t' labeling). Selinux does know of '/net/rpc/nfsd.fh' (and applies the 'sysctl_rpc_t' label). PID stripping from the path was done implicitly in the previous code because the proc_dir_entry tree had the root in '/net' in the example from above. The dentry tree has the root in '/1'. Signed-off-by: Eric W. Biederman <ebiederm@xmission.com> Signed-off-by: Lucian Adrian Grijincu <lucian.grijincu@gmail.com> Signed-off-by: Eric Paris <eparis@redhat.com>
1 parent 652bb9b commit 8e6c969

2 files changed

Lines changed: 18 additions & 103 deletions

File tree

‎fs/proc/proc_sysctl.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ static struct inode *proc_sys_make_inode(struct super_block *sb,
3232
ei->sysctl_entry = table;
3333

3434
inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
35-
inode->i_flags |= S_PRIVATE; /* tell selinux to ignore this inode */
3635
inode->i_mode = table->mode;
3736
if (!table->child) {
3837
inode->i_mode |= S_IFREG;

‎security/selinux/hooks.c‎

Lines changed: 18 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
#include <linux/fdtable.h>
4545
#include <linux/namei.h>
4646
#include <linux/mount.h>
47-
#include <linux/proc_fs.h>
4847
#include <linux/netfilter_ipv4.h>
4948
#include <linux/netfilter_ipv6.h>
5049
#include <linux/tty.h>
@@ -71,7 +70,6 @@
7170
#include <net/ipv6.h>
7271
#include <linux/hugetlb.h>
7372
#include <linux/personality.h>
74-
#include <linux/sysctl.h>
7573
#include <linux/audit.h>
7674
#include <linux/string.h>
7775
#include <linux/selinux.h>
@@ -1121,39 +1119,35 @@ static inline u16 socket_type_to_security_class(int family, int type, int protoc
11211119
}
11221120

11231121
#ifdef CONFIG_PROC_FS
1124-
static int selinux_proc_get_sid(struct proc_dir_entry *de,
1122+
static int selinux_proc_get_sid(struct dentry *dentry,
11251123
u16 tclass,
11261124
u32 *sid)
11271125
{
1128-
int buflen, rc;
1129-
char *buffer, *path, *end;
1126+
int rc;
1127+
char *buffer, *path;
11301128

11311129
buffer = (char *)__get_free_page(GFP_KERNEL);
11321130
if (!buffer)
11331131
return -ENOMEM;
11341132

1135-
buflen = PAGE_SIZE;
1136-
end = buffer+buflen;
1137-
*--end = '\0';
1138-
buflen--;
1139-
path = end-1;
1140-
*path = '/';
1141-
while (de && de != de->parent) {
1142-
buflen -= de->namelen + 1;
1143-
if (buflen < 0)
1144-
break;
1145-
end -= de->namelen;
1146-
memcpy(end, de->name, de->namelen);
1147-
*--end = '/';
1148-
path = end;
1149-
de = de->parent;
1133+
path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1134+
if (IS_ERR(path))
1135+
rc = PTR_ERR(path);
1136+
else {
1137+
/* each process gets a /proc/PID/ entry. Strip off the
1138+
* PID part to get a valid selinux labeling.
1139+
* e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1140+
while (path[1] >= '0' && path[1] <= '9') {
1141+
path[1] = '/';
1142+
path++;
1143+
}
1144+
rc = security_genfs_sid("proc", path, tclass, sid);
11501145
}
1151-
rc = security_genfs_sid("proc", path, tclass, sid);
11521146
free_page((unsigned long)buffer);
11531147
return rc;
11541148
}
11551149
#else
1156-
static int selinux_proc_get_sid(struct proc_dir_entry *de,
1150+
static int selinux_proc_get_sid(struct dentry *dentry,
11571151
u16 tclass,
11581152
u32 *sid)
11591153
{
@@ -1315,10 +1309,9 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent
13151309
isec->sid = sbsec->sid;
13161310

13171311
if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) {
1318-
struct proc_inode *proci = PROC_I(inode);
1319-
if (proci->pde) {
1312+
if (opt_dentry) {
13201313
isec->sclass = inode_mode_to_security_class(inode->i_mode);
1321-
rc = selinux_proc_get_sid(proci->pde,
1314+
rc = selinux_proc_get_sid(opt_dentry,
13221315
isec->sclass,
13231316
&sid);
13241317
if (rc)
@@ -1861,82 +1854,6 @@ static int selinux_capable(struct task_struct *tsk, const struct cred *cred,
18611854
return task_has_capability(tsk, cred, cap, audit);
18621855
}
18631856

1864-
static int selinux_sysctl_get_sid(ctl_table *table, u16 tclass, u32 *sid)
1865-
{
1866-
int buflen, rc;
1867-
char *buffer, *path, *end;
1868-
1869-
rc = -ENOMEM;
1870-
buffer = (char *)__get_free_page(GFP_KERNEL);
1871-
if (!buffer)
1872-
goto out;
1873-
1874-
buflen = PAGE_SIZE;
1875-
end = buffer+buflen;
1876-
*--end = '\0';
1877-
buflen--;
1878-
path = end-1;
1879-
*path = '/';
1880-
while (table) {
1881-
const char *name = table->procname;
1882-
size_t namelen = strlen(name);
1883-
buflen -= namelen + 1;
1884-
if (buflen < 0)
1885-
goto out_free;
1886-
end -= namelen;
1887-
memcpy(end, name, namelen);
1888-
*--end = '/';
1889-
path = end;
1890-
table = table->parent;
1891-
}
1892-
buflen -= 4;
1893-
if (buflen < 0)
1894-
goto out_free;
1895-
end -= 4;
1896-
memcpy(end, "/sys", 4);
1897-
path = end;
1898-
rc = security_genfs_sid("proc", path, tclass, sid);
1899-
out_free:
1900-
free_page((unsigned long)buffer);
1901-
out:
1902-
return rc;
1903-
}
1904-
1905-
static int selinux_sysctl(ctl_table *table, int op)
1906-
{
1907-
int error = 0;
1908-
u32 av;
1909-
u32 tsid, sid;
1910-
int rc;
1911-
1912-
sid = current_sid();
1913-
1914-
rc = selinux_sysctl_get_sid(table, (op == 0001) ?
1915-
SECCLASS_DIR : SECCLASS_FILE, &tsid);
1916-
if (rc) {
1917-
/* Default to the well-defined sysctl SID. */
1918-
tsid = SECINITSID_SYSCTL;
1919-
}
1920-
1921-
/* The op values are "defined" in sysctl.c, thereby creating
1922-
* a bad coupling between this module and sysctl.c */
1923-
if (op == 001) {
1924-
error = avc_has_perm(sid, tsid,
1925-
SECCLASS_DIR, DIR__SEARCH, NULL);
1926-
} else {
1927-
av = 0;
1928-
if (op & 004)
1929-
av |= FILE__READ;
1930-
if (op & 002)
1931-
av |= FILE__WRITE;
1932-
if (av)
1933-
error = avc_has_perm(sid, tsid,
1934-
SECCLASS_FILE, av, NULL);
1935-
}
1936-
1937-
return error;
1938-
}
1939-
19401857
static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
19411858
{
19421859
const struct cred *cred = current_cred();
@@ -5398,7 +5315,6 @@ static struct security_operations selinux_ops = {
53985315
.ptrace_traceme = selinux_ptrace_traceme,
53995316
.capget = selinux_capget,
54005317
.capset = selinux_capset,
5401-
.sysctl = selinux_sysctl,
54025318
.capable = selinux_capable,
54035319
.quotactl = selinux_quotactl,
54045320
.quota_on = selinux_quota_on,

0 commit comments

Comments
 (0)