Skip to content

Commit 4f0b919

Browse files
committed
fs: Rename anon_inode_getfile_secure() and anon_inode_getfd_secure()
The call to the inode_init_security_anon() LSM hook is not the sole reason to use anon_inode_getfile_secure() or anon_inode_getfd_secure(). For example, the functions also allow one to create a file with non-zero size, without needing a full-blown filesystem. In this case, you don't need a "secure" version, just unique inodes; the current name of the functions is confusing and does not explain well the difference with the more "standard" anon_inode_getfile() and anon_inode_getfd(). Of course, there is another side of the coin; neither io_uring nor userfaultfd strictly speaking need distinct inodes, and it is not that clear anymore that anon_inode_create_get{file,fd}() allow the LSM to intercept and block the inode's creation. If one was so inclined, anon_inode_getfile_secure() and anon_inode_getfd_secure() could be kept, using the shared inode or a new one depending on CONFIG_SECURITY. However, this is probably overkill, and potentially a cause of bugs in different configurations. Therefore, just add a comment to io_uring and userfaultfd explaining the choice of the function. While at it, remove the export for what is now anon_inode_create_getfd(). There is no in-tree module that uses it, and the old name is gone anyway. If anybody actually needs the symbol, they can ask or they can just use anon_inode_create_getfile(), which will be exported very soon for use in KVM. Suggested-by: Christian Brauner <[email protected]> Reviewed-by: Christian Brauner <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 0003e2a commit 4f0b919

File tree

4 files changed

+40
-22
lines changed

4 files changed

+40
-22
lines changed

fs/anon_inodes.c

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ static struct file *__anon_inode_getfile(const char *name,
7979
const struct file_operations *fops,
8080
void *priv, int flags,
8181
const struct inode *context_inode,
82-
bool secure)
82+
bool make_inode)
8383
{
8484
struct inode *inode;
8585
struct file *file;
8686

8787
if (fops->owner && !try_module_get(fops->owner))
8888
return ERR_PTR(-ENOENT);
8989

90-
if (secure) {
90+
if (make_inode) {
9191
inode = anon_inode_make_secure_inode(name, context_inode);
9292
if (IS_ERR(inode)) {
9393
file = ERR_CAST(inode);
@@ -149,13 +149,10 @@ struct file *anon_inode_getfile(const char *name,
149149
EXPORT_SYMBOL_GPL(anon_inode_getfile);
150150

151151
/**
152-
* anon_inode_getfile_secure - Like anon_inode_getfile(), but creates a new
152+
* anon_inode_create_getfile - Like anon_inode_getfile(), but creates a new
153153
* !S_PRIVATE anon inode rather than reuse the
154154
* singleton anon inode and calls the
155-
* inode_init_security_anon() LSM hook. This
156-
* allows for both the inode to have its own
157-
* security context and for the LSM to enforce
158-
* policy on the inode's creation.
155+
* inode_init_security_anon() LSM hook.
159156
*
160157
* @name: [in] name of the "class" of the new file
161158
* @fops: [in] file operations for the new file
@@ -164,11 +161,21 @@ EXPORT_SYMBOL_GPL(anon_inode_getfile);
164161
* @context_inode:
165162
* [in] the logical relationship with the new inode (optional)
166163
*
164+
* Create a new anonymous inode and file pair. This can be done for two
165+
* reasons:
166+
*
167+
* - for the inode to have its own security context, so that LSMs can enforce
168+
* policy on the inode's creation;
169+
*
170+
* - if the caller needs a unique inode, for example in order to customize
171+
* the size returned by fstat()
172+
*
167173
* The LSM may use @context_inode in inode_init_security_anon(), but a
168-
* reference to it is not held. Returns the newly created file* or an error
169-
* pointer. See the anon_inode_getfile() documentation for more information.
174+
* reference to it is not held.
175+
*
176+
* Returns the newly created file* or an error pointer.
170177
*/
171-
struct file *anon_inode_getfile_secure(const char *name,
178+
struct file *anon_inode_create_getfile(const char *name,
172179
const struct file_operations *fops,
173180
void *priv, int flags,
174181
const struct inode *context_inode)
@@ -181,7 +188,7 @@ static int __anon_inode_getfd(const char *name,
181188
const struct file_operations *fops,
182189
void *priv, int flags,
183190
const struct inode *context_inode,
184-
bool secure)
191+
bool make_inode)
185192
{
186193
int error, fd;
187194
struct file *file;
@@ -192,7 +199,7 @@ static int __anon_inode_getfd(const char *name,
192199
fd = error;
193200

194201
file = __anon_inode_getfile(name, fops, priv, flags, context_inode,
195-
secure);
202+
make_inode);
196203
if (IS_ERR(file)) {
197204
error = PTR_ERR(file);
198205
goto err_put_unused_fd;
@@ -231,10 +238,9 @@ int anon_inode_getfd(const char *name, const struct file_operations *fops,
231238
EXPORT_SYMBOL_GPL(anon_inode_getfd);
232239

233240
/**
234-
* anon_inode_getfd_secure - Like anon_inode_getfd(), but creates a new
241+
* anon_inode_create_getfd - Like anon_inode_getfd(), but creates a new
235242
* !S_PRIVATE anon inode rather than reuse the singleton anon inode, and calls
236-
* the inode_init_security_anon() LSM hook. This allows the inode to have its
237-
* own security context and for a LSM to reject creation of the inode.
243+
* the inode_init_security_anon() LSM hook.
238244
*
239245
* @name: [in] name of the "class" of the new file
240246
* @fops: [in] file operations for the new file
@@ -243,16 +249,26 @@ EXPORT_SYMBOL_GPL(anon_inode_getfd);
243249
* @context_inode:
244250
* [in] the logical relationship with the new inode (optional)
245251
*
252+
* Create a new anonymous inode and file pair. This can be done for two
253+
* reasons:
254+
*
255+
* - for the inode to have its own security context, so that LSMs can enforce
256+
* policy on the inode's creation;
257+
*
258+
* - if the caller needs a unique inode, for example in order to customize
259+
* the size returned by fstat()
260+
*
246261
* The LSM may use @context_inode in inode_init_security_anon(), but a
247262
* reference to it is not held.
263+
*
264+
* Returns a newly created file descriptor or an error code.
248265
*/
249-
int anon_inode_getfd_secure(const char *name, const struct file_operations *fops,
266+
int anon_inode_create_getfd(const char *name, const struct file_operations *fops,
250267
void *priv, int flags,
251268
const struct inode *context_inode)
252269
{
253270
return __anon_inode_getfd(name, fops, priv, flags, context_inode, true);
254271
}
255-
EXPORT_SYMBOL_GPL(anon_inode_getfd_secure);
256272

257273
static int __init anon_inode_init(void)
258274
{

fs/userfaultfd.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ static int resolve_userfault_fork(struct userfaultfd_ctx *new,
10331033
{
10341034
int fd;
10351035

1036-
fd = anon_inode_getfd_secure("[userfaultfd]", &userfaultfd_fops, new,
1036+
fd = anon_inode_create_getfd("[userfaultfd]", &userfaultfd_fops, new,
10371037
O_RDONLY | (new->flags & UFFD_SHARED_FCNTL_FLAGS), inode);
10381038
if (fd < 0)
10391039
return fd;
@@ -2205,7 +2205,8 @@ static int new_userfaultfd(int flags)
22052205
/* prevent the mm struct to be freed */
22062206
mmgrab(ctx->mm);
22072207

2208-
fd = anon_inode_getfd_secure("[userfaultfd]", &userfaultfd_fops, ctx,
2208+
/* Create a new inode so that the LSM can block the creation. */
2209+
fd = anon_inode_create_getfd("[userfaultfd]", &userfaultfd_fops, ctx,
22092210
O_RDONLY | (flags & UFFD_SHARED_FCNTL_FLAGS), NULL);
22102211
if (fd < 0) {
22112212
mmdrop(ctx->mm);

include/linux/anon_inodes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ struct inode;
1515
struct file *anon_inode_getfile(const char *name,
1616
const struct file_operations *fops,
1717
void *priv, int flags);
18-
struct file *anon_inode_getfile_secure(const char *name,
18+
struct file *anon_inode_create_getfile(const char *name,
1919
const struct file_operations *fops,
2020
void *priv, int flags,
2121
const struct inode *context_inode);
2222
int anon_inode_getfd(const char *name, const struct file_operations *fops,
2323
void *priv, int flags);
24-
int anon_inode_getfd_secure(const char *name,
24+
int anon_inode_create_getfd(const char *name,
2525
const struct file_operations *fops,
2626
void *priv, int flags,
2727
const struct inode *context_inode);

io_uring/io_uring.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3835,7 +3835,8 @@ static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
38353835
return ERR_PTR(ret);
38363836
#endif
38373837

3838-
file = anon_inode_getfile_secure("[io_uring]", &io_uring_fops, ctx,
3838+
/* Create a new inode so that the LSM can block the creation. */
3839+
file = anon_inode_create_getfile("[io_uring]", &io_uring_fops, ctx,
38393840
O_RDWR | O_CLOEXEC, NULL);
38403841
#if defined(CONFIG_UNIX)
38413842
if (IS_ERR(file)) {

0 commit comments

Comments
 (0)