2
* Copyright (C) 2005 Novell/SUSE
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License as
6
* published by the Free Software Foundation, version 2 of the
9
* AppArmor filesystem (part of securityfs)
12
#include <linux/security.h>
13
#include <linux/vmalloc.h>
14
#include <linux/module.h>
15
#include <linux/seq_file.h>
16
#include <asm/uaccess.h>
20
#include "aamatch/match.h"
22
#define SECFS_SD "apparmor"
23
static struct dentry *sdfs_dentry = NULL;
26
extern struct seq_operations subdomainfs_profiles_op;
27
static int sd_prof_open(struct inode *inode, struct file *file);
28
static int sd_prof_release(struct inode *inode, struct file *file);
30
static struct file_operations subdomainfs_profiles_fops = {
34
.release = sd_prof_release,
38
static ssize_t sd_version_read(struct file *file, char __user *buf,
39
size_t size, loff_t *ppos);
41
static struct file_operations subdomainfs_version_fops = {
42
.read = sd_version_read,
46
static ssize_t sd_matching_read(struct file *file, char __user *buf,
47
size_t size, loff_t *ppos);
49
static struct file_operations subdomainfs_matching_fops = {
50
.read = sd_matching_read,
55
extern ssize_t sd_file_prof_add(void *, size_t);
56
extern ssize_t sd_file_prof_repl(void *, size_t);
57
extern ssize_t sd_file_prof_remove(const char *, int);
59
static ssize_t sd_profile_load(struct file *f, const char __user *buf,
60
size_t size, loff_t *pos);
61
static ssize_t sd_profile_replace(struct file *f, const char __user *buf,
62
size_t size, loff_t *pos);
63
static ssize_t sd_profile_remove(struct file *f, const char __user *buf,
64
size_t size, loff_t *pos);
66
static struct file_operations subdomainfs_profile_load = {
67
.write = sd_profile_load
70
static struct file_operations subdomainfs_profile_replace = {
71
.write = sd_profile_replace
74
static struct file_operations subdomainfs_profile_remove = {
75
.write = sd_profile_remove
80
static u64 sd_control_get(void *data);
81
static void sd_control_set(void *data, u64 val);
83
DEFINE_SIMPLE_ATTRIBUTE(subdomainfs_control_fops, sd_control_get,
84
sd_control_set, "%lld\n");
88
/* table of static entries */
90
static struct root_entry {
94
struct file_operations *fops;
98
struct dentry *dentry;
101
/* our root, normally /sys/kernel/security/subdomain */
102
{SECFS_SD, S_IFDIR, 0550}, /* DO NOT EDIT/MOVE */
104
/* interface for obtaining list of profiles currently loaded */
105
{"profiles", S_IFREG, 0440, &subdomainfs_profiles_fops,
108
/* interface for obtaining version# of subdomain */
109
{"version", S_IFREG, 0440, &subdomainfs_version_fops,
112
/* interface for obtaining matching features supported */
113
{"matching", S_IFREG, 0440, &subdomainfs_matching_fops,
116
/* interface for loading/removing/replacing profiles */
117
{".load", S_IFREG, 0640, &subdomainfs_profile_load,
119
{".replace", S_IFREG, 0640, &subdomainfs_profile_replace,
121
{".remove", S_IFREG, 0640, &subdomainfs_profile_remove,
124
/* interface for setting binary config values */
125
{"control", S_IFDIR, 0550},
126
{"complain", S_IFREG, 0640, &subdomainfs_control_fops,
127
&subdomain_complain},
128
{"audit", S_IFREG, 0640, &subdomainfs_control_fops,
130
{"debug", S_IFREG, 0640, &subdomainfs_control_fops,
132
{"logsyscall", S_IFREG, 0640, &subdomainfs_control_fops,
133
&subdomain_logsyscall},
140
#define SDFS_DENTRY root_entries[0].dentry
142
static const unsigned int num_entries =
143
sizeof(root_entries) / sizeof(struct root_entry);
147
static int sd_prof_open(struct inode *inode, struct file *file)
149
return seq_open(file, &subdomainfs_profiles_op);
153
static int sd_prof_release(struct inode *inode, struct file *file)
155
return seq_release(inode, file);
158
static ssize_t sd_version_read(struct file *file, char __user *buf,
159
size_t size, loff_t *ppos)
161
const char *version = apparmor_version_nl();
163
return simple_read_from_buffer(buf, size, ppos, version,
167
static ssize_t sd_matching_read(struct file *file, char __user *buf,
168
size_t size, loff_t *ppos)
170
const char *matching = sdmatch_features();
172
return simple_read_from_buffer(buf, size, ppos, matching,
176
static char *sd_simple_write_to_buffer(const char __user *userbuf,
177
size_t alloc_size, size_t copy_size,
178
loff_t *pos, const char *msg)
183
/* only writes from pos 0, that is complete writes */
184
data = ERR_PTR(-ESPIPE);
188
/* Don't allow confined processes to load/replace/remove profiles.
189
* No sane person would add rules allowing this to a profile
190
* but we enforce the restriction anyways.
192
if (sd_is_confined()) {
193
struct subdomain *sd = SD_SUBDOMAIN(current->security);
195
SD_WARN("REJECTING access to profile %s (%s(%d) "
196
"profile %s active %s)\n",
197
msg, current->comm, current->pid,
198
sd->profile->name, sd->active->name);
200
data = ERR_PTR(-EPERM);
204
data = vmalloc(alloc_size);
206
data = ERR_PTR(-ENOMEM);
210
if (copy_from_user(data, userbuf, copy_size)) {
212
data = ERR_PTR(-EFAULT);
220
static ssize_t sd_profile_load(struct file *f, const char __user *buf,
221
size_t size, loff_t *pos)
226
data = sd_simple_write_to_buffer(buf, size, size, pos, "load");
229
error = sd_file_prof_add(data, size);
232
error = PTR_ERR(data);
238
static ssize_t sd_profile_replace(struct file *f, const char __user *buf,
239
size_t size, loff_t *pos)
244
data = sd_simple_write_to_buffer(buf, size, size, pos, "replacement");
247
error = sd_file_prof_repl(data, size);
250
error = PTR_ERR(data);
256
static ssize_t sd_profile_remove(struct file *f, const char __user *buf,
257
size_t size, loff_t *pos)
262
/* sd_file_prof_remove needs a null terminated string so 1 extra
263
* byte is allocated and null the copied data is then null terminated
265
data = sd_simple_write_to_buffer(buf, size+1, size, pos, "removal");
269
error = sd_file_prof_remove(data, size);
272
error = PTR_ERR(data);
278
static u64 sd_control_get(void *data)
283
static void sd_control_set(void *data, u64 val)
288
*(int*)data = (int)val;
291
static void clear_subdomainfs(void)
295
for (i=0; i < num_entries;i++) {
298
if (root_entries[i].mode == S_IFDIR) {
299
if (root_entries[i].name)
300
/* defer dir free till all sub-entries freed */
304
index = root_entries[i].parent_index;
309
if (root_entries[index].dentry) {
310
securityfs_remove(root_entries[index].dentry);
312
SD_DEBUG("%s: deleted subdomainfs entry name=%s "
315
root_entries[index].name,
316
root_entries[index].dentry);
318
root_entries[index].dentry = NULL;
319
root_entries[index].parent_index = 0;
324
static int populate_subdomainfs(struct dentry *root)
326
unsigned int i, parent_index, depth;
328
#define ENT root_entries[i]
330
for (i = 0; i < num_entries; i++) {
331
root_entries[i].dentry = NULL;
332
root_entries[i].parent_index = 0;
335
/* 1. Verify entry 0 is valid [sanity check] */
336
if (num_entries == 0 ||
337
!root_entries[0].name ||
338
strcmp(root_entries[0].name, SECFS_SD) != 0 ||
339
root_entries[0].mode != S_IFDIR) {
340
SD_ERROR("%s: root entry 0 is not SECFS_SD/dir\n",
345
/* 2. Verify table structure */
349
for (i = 1; i < num_entries; i++) {
350
ENT.parent_index = parent_index;
352
if (ENT.name && ENT.mode == S_IFDIR) {
355
} else if (!ENT.name) {
356
if (ENT.mode != S_IFDIR || depth == 0) {
357
SD_ERROR("%s: root_entry %d invalid (%u %d)",
359
ENT.mode, ENT.parent_index);
364
parent_index = root_entries[parent_index].parent_index;
369
SD_ERROR("%s: root_entry table not correctly terminated\n",
374
/* 3. Create root (parent=NULL) */
377
ENT.dentry = securityfs_create_file(ENT.name,
378
ENT.mode | ENT.access,
382
SD_DEBUG("%s: created securityfs/subdomain [dentry=%p]\n",
383
__FUNCTION__, ENT.dentry);
388
/* 4. create remaining nodes */
389
for (i = 1; i < num_entries; i++) {
390
struct dentry *parent;
392
/* end of directory ? */
396
parent = root_entries[ENT.parent_index].dentry;
398
ENT.dentry = securityfs_create_file(ENT.name,
399
ENT.mode | ENT.access,
401
ENT.mode != S_IFDIR ? ENT.data : NULL,
402
ENT.mode != S_IFDIR ? ENT.fops : NULL);
407
SD_DEBUG("%s: added subdomainfs entry "
408
"name=%s mode=%x dentry=%p [parent %p]\n",
409
__FUNCTION__, ENT.name, ENT.mode|ENT.access,
422
int create_subdomainfs(void)
425
SD_ERROR("%s: Subdomain securityfs already exists\n",
427
else if (!populate_subdomainfs(sdfs_dentry))
428
SD_ERROR("%s: Error populating Subdomain securityfs\n",
431
return (SDFS_DENTRY != NULL);
434
int destroy_subdomainfs(void)