Blame view

security/inode.c 6.69 KB
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  /*
   *  inode.c - securityfs
   *
   *  Copyright (C) 2005 Greg Kroah-Hartman <gregkh@suse.de>
   *
   *	This program is free software; you can redistribute it and/or
   *	modify it under the terms of the GNU General Public License version
   *	2 as published by the Free Software Foundation.
   *
   *  Based on fs/debugfs/inode.c which had the following copyright notice:
   *    Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
   *    Copyright (C) 2004 IBM Inc.
   */
  
  /* #define DEBUG */
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
16
17
18
19
20
21
22
  #include <linux/module.h>
  #include <linux/fs.h>
  #include <linux/mount.h>
  #include <linux/pagemap.h>
  #include <linux/init.h>
  #include <linux/namei.h>
  #include <linux/security.h>
925629278   Mimi Zohar   integrity: specia...
23
  #include <linux/magic.h>
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
24
25
26
  
  static struct vfsmount *mount;
  static int mount_count;
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
27
28
  static inline int positive(struct dentry *dentry)
  {
ce0b16ddf   David Howells   VFS: security/: d...
29
  	return d_really_is_positive(dentry) && !d_unhashed(dentry);
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
30
31
32
33
34
35
36
37
  }
  
  static int fill_super(struct super_block *sb, void *data, int silent)
  {
  	static struct tree_descr files[] = {{""}};
  
  	return simple_fill_super(sb, SECURITYFS_MAGIC, files);
  }
fc14f2fef   Al Viro   convert get_sb_si...
38
  static struct dentry *get_sb(struct file_system_type *fs_type,
454e2398b   David Howells   [PATCH] VFS: Perm...
39
  		  int flags, const char *dev_name,
fc14f2fef   Al Viro   convert get_sb_si...
40
  		  void *data)
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
41
  {
fc14f2fef   Al Viro   convert get_sb_si...
42
  	return mount_single(fs_type, flags, data, fill_super);
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
43
44
45
46
47
  }
  
  static struct file_system_type fs_type = {
  	.owner =	THIS_MODULE,
  	.name =		"securityfs",
fc14f2fef   Al Viro   convert get_sb_si...
48
  	.mount =	get_sb,
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
49
50
  	.kill_sb =	kill_litter_super,
  };
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
51
52
53
54
55
56
  /**
   * securityfs_create_file - create a file in the securityfs filesystem
   *
   * @name: a pointer to a string containing the name of the file to create.
   * @mode: the permission that the file should have
   * @parent: a pointer to the parent dentry for this file.  This should be a
3f23d815c   Randy Dunlap   security: add/fix...
57
   *          directory dentry if set.  If this parameter is %NULL, then the
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
58
59
   *          file will be created in the root of the securityfs filesystem.
   * @data: a pointer to something that the caller will want to get to later
8e18e2941   Theodore Ts'o   [PATCH] inode_die...
60
   *        on.  The inode.i_private pointer will point to this value on
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
61
62
63
64
65
   *        the open() call.
   * @fops: a pointer to a struct file_operations that should be used for
   *        this file.
   *
   * This is the basic "create a file" function for securityfs.  It allows for a
3f23d815c   Randy Dunlap   security: add/fix...
66
   * wide range of flexibility in creating a file, or a directory (if you
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
67
   * want to create a directory, the securityfs_create_dir() function is
3f23d815c   Randy Dunlap   security: add/fix...
68
   * recommended to be used instead).
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
69
   *
3f23d815c   Randy Dunlap   security: add/fix...
70
   * This function returns a pointer to a dentry if it succeeds.  This
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
71
72
   * pointer must be passed to the securityfs_remove() function when the file is
   * to be removed (no automatic cleanup happens if your module is unloaded,
faa3aad75   Serge E. Hallyn   securityfs: fix l...
73
   * you are responsible here).  If an error occurs, the function will return
da3dae54e   Masanari Iida   Documentation: Do...
74
   * the error value (via ERR_PTR).
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
75
   *
3f23d815c   Randy Dunlap   security: add/fix...
76
   * If securityfs is not enabled in the kernel, the value %-ENODEV is
faa3aad75   Serge E. Hallyn   securityfs: fix l...
77
   * returned.
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
78
   */
52ef0c042   Al Viro   switch securityfs...
79
  struct dentry *securityfs_create_file(const char *name, umode_t mode,
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
80
  				   struct dentry *parent, void *data,
9c2e08c59   Arjan van de Ven   [PATCH] mark stru...
81
  				   const struct file_operations *fops)
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
82
  {
3e25eb9c4   Al Viro   securityfs: fix o...
83
84
85
  	struct dentry *dentry;
  	int is_dir = S_ISDIR(mode);
  	struct inode *dir, *inode;
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
86
  	int error;
3e25eb9c4   Al Viro   securityfs: fix o...
87
88
89
90
  	if (!is_dir) {
  		BUG_ON(!fops);
  		mode = (mode & S_IALLUGO) | S_IFREG;
  	}
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
91
92
  	pr_debug("securityfs: creating file '%s'
  ",name);
1f5ce9e93   Trond Myklebust   VFS: Unexport do_...
93
  	error = simple_pin_fs(&fs_type, &mount, &mount_count);
3e25eb9c4   Al Viro   securityfs: fix o...
94
95
96
97
98
  	if (error)
  		return ERR_PTR(error);
  
  	if (!parent)
  		parent = mount->mnt_root;
ce0b16ddf   David Howells   VFS: security/: d...
99
  	dir = d_inode(parent);
3e25eb9c4   Al Viro   securityfs: fix o...
100
101
102
103
104
  
  	mutex_lock(&dir->i_mutex);
  	dentry = lookup_one_len(name, parent, strlen(name));
  	if (IS_ERR(dentry))
  		goto out;
ce0b16ddf   David Howells   VFS: security/: d...
105
  	if (d_really_is_positive(dentry)) {
3e25eb9c4   Al Viro   securityfs: fix o...
106
107
  		error = -EEXIST;
  		goto out1;
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
108
  	}
3e25eb9c4   Al Viro   securityfs: fix o...
109
110
111
112
  	inode = new_inode(dir->i_sb);
  	if (!inode) {
  		error = -ENOMEM;
  		goto out1;
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
113
  	}
3e25eb9c4   Al Viro   securityfs: fix o...
114
115
116
117
118
119
120
121
122
123
124
  	inode->i_ino = get_next_ino();
  	inode->i_mode = mode;
  	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  	inode->i_private = data;
  	if (is_dir) {
  		inode->i_op = &simple_dir_inode_operations;
  		inode->i_fop = &simple_dir_operations;
  		inc_nlink(inode);
  		inc_nlink(dir);
  	} else {
  		inode->i_fop = fops;
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
125
  	}
3e25eb9c4   Al Viro   securityfs: fix o...
126
127
128
129
130
131
132
133
134
135
136
  	d_instantiate(dentry, inode);
  	dget(dentry);
  	mutex_unlock(&dir->i_mutex);
  	return dentry;
  
  out1:
  	dput(dentry);
  	dentry = ERR_PTR(error);
  out:
  	mutex_unlock(&dir->i_mutex);
  	simple_release_fs(&mount, &mount_count);
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
137
138
139
140
141
142
143
144
145
146
  	return dentry;
  }
  EXPORT_SYMBOL_GPL(securityfs_create_file);
  
  /**
   * securityfs_create_dir - create a directory in the securityfs filesystem
   *
   * @name: a pointer to a string containing the name of the directory to
   *        create.
   * @parent: a pointer to the parent dentry for this file.  This should be a
3f23d815c   Randy Dunlap   security: add/fix...
147
   *          directory dentry if set.  If this parameter is %NULL, then the
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
148
149
   *          directory will be created in the root of the securityfs filesystem.
   *
3f23d815c   Randy Dunlap   security: add/fix...
150
   * This function creates a directory in securityfs with the given @name.
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
151
   *
3f23d815c   Randy Dunlap   security: add/fix...
152
   * This function returns a pointer to a dentry if it succeeds.  This
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
153
154
   * pointer must be passed to the securityfs_remove() function when the file is
   * to be removed (no automatic cleanup happens if your module is unloaded,
3f23d815c   Randy Dunlap   security: add/fix...
155
   * you are responsible here).  If an error occurs, %NULL will be returned.
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
156
   *
3f23d815c   Randy Dunlap   security: add/fix...
157
   * If securityfs is not enabled in the kernel, the value %-ENODEV is
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
158
   * returned.  It is not wise to check for this value, but rather, check for
3f23d815c   Randy Dunlap   security: add/fix...
159
   * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
160
161
162
163
164
165
166
167
168
169
170
171
172
   * code.
   */
  struct dentry *securityfs_create_dir(const char *name, struct dentry *parent)
  {
  	return securityfs_create_file(name,
  				      S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
  				      parent, NULL, NULL);
  }
  EXPORT_SYMBOL_GPL(securityfs_create_dir);
  
  /**
   * securityfs_remove - removes a file or directory from the securityfs filesystem
   *
3f23d815c   Randy Dunlap   security: add/fix...
173
   * @dentry: a pointer to a the dentry of the file or directory to be removed.
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
174
175
176
177
178
179
   *
   * This function removes a file or directory in securityfs that was previously
   * created with a call to another securityfs function (like
   * securityfs_create_file() or variants thereof.)
   *
   * This function is required to be called in order for the file to be
3f23d815c   Randy Dunlap   security: add/fix...
180
181
   * removed. No automatic cleanup of files will happen when a module is
   * removed; you are responsible here.
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
182
183
184
185
   */
  void securityfs_remove(struct dentry *dentry)
  {
  	struct dentry *parent;
d93e4c940   Eric Paris   securityfs: secur...
186
  	if (!dentry || IS_ERR(dentry))
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
187
188
189
  		return;
  
  	parent = dentry->d_parent;
ce0b16ddf   David Howells   VFS: security/: d...
190
  	if (!parent || d_really_is_negative(parent))
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
191
  		return;
ce0b16ddf   David Howells   VFS: security/: d...
192
  	mutex_lock(&d_inode(parent)->i_mutex);
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
193
  	if (positive(dentry)) {
ce0b16ddf   David Howells   VFS: security/: d...
194
  		if (d_really_is_positive(dentry)) {
e36cb0b89   David Howells   VFS: (Scripted) C...
195
  			if (d_is_dir(dentry))
ce0b16ddf   David Howells   VFS: security/: d...
196
  				simple_rmdir(d_inode(parent), dentry);
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
197
  			else
ce0b16ddf   David Howells   VFS: security/: d...
198
  				simple_unlink(d_inode(parent), dentry);
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
199
200
201
  			dput(dentry);
  		}
  	}
ce0b16ddf   David Howells   VFS: security/: d...
202
  	mutex_unlock(&d_inode(parent)->i_mutex);
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
203
204
205
  	simple_release_fs(&mount, &mount_count);
  }
  EXPORT_SYMBOL_GPL(securityfs_remove);
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
206
207
208
  static int __init securityfs_init(void)
  {
  	int retval;
28dd1f346   Eric W. Biederman   sysfs: Create mou...
209
210
211
  	retval = sysfs_create_mount_point(kernel_kobj, "security");
  	if (retval)
  		return retval;
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
212
213
214
  
  	retval = register_filesystem(&fs_type);
  	if (retval)
28dd1f346   Eric W. Biederman   sysfs: Create mou...
215
  		sysfs_remove_mount_point(kernel_kobj, "security");
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
216
217
  	return retval;
  }
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
218
  core_initcall(securityfs_init);
b67dbf9d4   Greg Kroah-Hartman   [PATCH] add secur...
219
  MODULE_LICENSE("GPL");