Thomas Gleixner | 2b27bdc | 2019-05-29 16:57:50 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 2 | /* |
| 3 | * This file is part of UBIFS. |
| 4 | * |
| 5 | * Copyright (C) 2006-2008 Nokia Corporation. |
| 6 | * Copyright (C) 2006, 2007 University of Szeged, Hungary |
| 7 | * |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 8 | * Authors: Zoltan Sogor |
| 9 | * Artem Bityutskiy (Битюцкий Артём) |
| 10 | * Adrian Hunter |
| 11 | */ |
| 12 | |
| 13 | /* This file implements EXT2-compatible extended attribute ioctl() calls */ |
| 14 | |
| 15 | #include <linux/compat.h> |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 16 | #include <linux/mount.h> |
| 17 | #include "ubifs.h" |
| 18 | |
Hou Tao | 2fe8b2d | 2019-02-09 16:54:20 +0800 | [diff] [blame] | 19 | /* Need to be kept consistent with checked flags in ioctl2ubifs() */ |
Eric Biggers | 219b0e2 | 2019-12-09 14:23:25 -0800 | [diff] [blame] | 20 | #define UBIFS_SETTABLE_IOCTL_FLAGS \ |
Hou Tao | 2fe8b2d | 2019-02-09 16:54:20 +0800 | [diff] [blame] | 21 | (FS_COMPR_FL | FS_SYNC_FL | FS_APPEND_FL | \ |
| 22 | FS_IMMUTABLE_FL | FS_DIRSYNC_FL) |
| 23 | |
Eric Biggers | 219b0e2 | 2019-12-09 14:23:25 -0800 | [diff] [blame] | 24 | /* Need to be kept consistent with checked flags in ubifs2ioctl() */ |
| 25 | #define UBIFS_GETTABLE_IOCTL_FLAGS \ |
| 26 | (UBIFS_SETTABLE_IOCTL_FLAGS | FS_ENCRYPT_FL) |
| 27 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 28 | /** |
| 29 | * ubifs_set_inode_flags - set VFS inode flags. |
| 30 | * @inode: VFS inode to set flags for |
| 31 | * |
| 32 | * This function propagates flags from UBIFS inode object to VFS inode object. |
| 33 | */ |
| 34 | void ubifs_set_inode_flags(struct inode *inode) |
| 35 | { |
| 36 | unsigned int flags = ubifs_inode(inode)->flags; |
| 37 | |
Eric Biggers | 2ee6a57 | 2017-10-09 12:15:35 -0700 | [diff] [blame] | 38 | inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_DIRSYNC | |
| 39 | S_ENCRYPTED); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 40 | if (flags & UBIFS_SYNC_FL) |
| 41 | inode->i_flags |= S_SYNC; |
| 42 | if (flags & UBIFS_APPEND_FL) |
| 43 | inode->i_flags |= S_APPEND; |
| 44 | if (flags & UBIFS_IMMUTABLE_FL) |
| 45 | inode->i_flags |= S_IMMUTABLE; |
| 46 | if (flags & UBIFS_DIRSYNC_FL) |
| 47 | inode->i_flags |= S_DIRSYNC; |
Eric Biggers | 2ee6a57 | 2017-10-09 12:15:35 -0700 | [diff] [blame] | 48 | if (flags & UBIFS_CRYPT_FL) |
| 49 | inode->i_flags |= S_ENCRYPTED; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /* |
| 53 | * ioctl2ubifs - convert ioctl inode flags to UBIFS inode flags. |
| 54 | * @ioctl_flags: flags to convert |
| 55 | * |
Rock Lee | 798868c | 2017-04-13 23:16:06 -0700 | [diff] [blame] | 56 | * This function converts ioctl flags (@FS_COMPR_FL, etc) to UBIFS inode flags |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 57 | * (@UBIFS_COMPR_FL, etc). |
| 58 | */ |
| 59 | static int ioctl2ubifs(int ioctl_flags) |
| 60 | { |
| 61 | int ubifs_flags = 0; |
| 62 | |
| 63 | if (ioctl_flags & FS_COMPR_FL) |
| 64 | ubifs_flags |= UBIFS_COMPR_FL; |
| 65 | if (ioctl_flags & FS_SYNC_FL) |
| 66 | ubifs_flags |= UBIFS_SYNC_FL; |
| 67 | if (ioctl_flags & FS_APPEND_FL) |
| 68 | ubifs_flags |= UBIFS_APPEND_FL; |
| 69 | if (ioctl_flags & FS_IMMUTABLE_FL) |
| 70 | ubifs_flags |= UBIFS_IMMUTABLE_FL; |
| 71 | if (ioctl_flags & FS_DIRSYNC_FL) |
| 72 | ubifs_flags |= UBIFS_DIRSYNC_FL; |
| 73 | |
| 74 | return ubifs_flags; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * ubifs2ioctl - convert UBIFS inode flags to ioctl inode flags. |
| 79 | * @ubifs_flags: flags to convert |
| 80 | * |
Rock Lee | 798868c | 2017-04-13 23:16:06 -0700 | [diff] [blame] | 81 | * This function converts UBIFS inode flags (@UBIFS_COMPR_FL, etc) to ioctl |
| 82 | * flags (@FS_COMPR_FL, etc). |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 83 | */ |
| 84 | static int ubifs2ioctl(int ubifs_flags) |
| 85 | { |
| 86 | int ioctl_flags = 0; |
| 87 | |
| 88 | if (ubifs_flags & UBIFS_COMPR_FL) |
| 89 | ioctl_flags |= FS_COMPR_FL; |
| 90 | if (ubifs_flags & UBIFS_SYNC_FL) |
| 91 | ioctl_flags |= FS_SYNC_FL; |
| 92 | if (ubifs_flags & UBIFS_APPEND_FL) |
| 93 | ioctl_flags |= FS_APPEND_FL; |
| 94 | if (ubifs_flags & UBIFS_IMMUTABLE_FL) |
| 95 | ioctl_flags |= FS_IMMUTABLE_FL; |
| 96 | if (ubifs_flags & UBIFS_DIRSYNC_FL) |
| 97 | ioctl_flags |= FS_DIRSYNC_FL; |
Eric Biggers | 219b0e2 | 2019-12-09 14:23:25 -0800 | [diff] [blame] | 98 | if (ubifs_flags & UBIFS_CRYPT_FL) |
| 99 | ioctl_flags |= FS_ENCRYPT_FL; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 100 | |
| 101 | return ioctl_flags; |
| 102 | } |
| 103 | |
| 104 | static int setflags(struct inode *inode, int flags) |
| 105 | { |
| 106 | int oldflags, err, release; |
| 107 | struct ubifs_inode *ui = ubifs_inode(inode); |
| 108 | struct ubifs_info *c = inode->i_sb->s_fs_info; |
| 109 | struct ubifs_budget_req req = { .dirtied_ino = 1, |
| 110 | .dirtied_ino_d = ui->data_len }; |
| 111 | |
| 112 | err = ubifs_budget_space(c, &req); |
| 113 | if (err) |
| 114 | return err; |
| 115 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 116 | mutex_lock(&ui->ui_mutex); |
| 117 | oldflags = ubifs2ioctl(ui->flags); |
Darrick J. Wong | 5aca284 | 2019-07-01 08:25:34 -0700 | [diff] [blame] | 118 | err = vfs_ioc_setflags_prepare(inode, oldflags, flags); |
| 119 | if (err) |
| 120 | goto out_unlock; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 121 | |
Eric Biggers | 219b0e2 | 2019-12-09 14:23:25 -0800 | [diff] [blame] | 122 | ui->flags &= ~ioctl2ubifs(UBIFS_SETTABLE_IOCTL_FLAGS); |
Eric Biggers | 2b57067 | 2019-12-09 14:23:24 -0800 | [diff] [blame] | 123 | ui->flags |= ioctl2ubifs(flags); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 124 | ubifs_set_inode_flags(inode); |
Deepa Dinamani | 607a11a | 2017-05-08 15:59:25 -0700 | [diff] [blame] | 125 | inode->i_ctime = current_time(inode); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 126 | release = ui->dirty; |
| 127 | mark_inode_dirty_sync(inode); |
| 128 | mutex_unlock(&ui->ui_mutex); |
| 129 | |
| 130 | if (release) |
| 131 | ubifs_release_budget(c, &req); |
| 132 | if (IS_SYNC(inode)) |
| 133 | err = write_inode_now(inode, 1); |
| 134 | return err; |
| 135 | |
| 136 | out_unlock: |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 137 | mutex_unlock(&ui->ui_mutex); |
| 138 | ubifs_release_budget(c, &req); |
| 139 | return err; |
| 140 | } |
| 141 | |
| 142 | long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 143 | { |
| 144 | int flags, err; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 145 | struct inode *inode = file_inode(file); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 146 | |
| 147 | switch (cmd) { |
| 148 | case FS_IOC_GETFLAGS: |
| 149 | flags = ubifs2ioctl(ubifs_inode(inode)->flags); |
| 150 | |
Artem Bityutskiy | a9f2fc0 | 2008-12-23 14:39:14 +0200 | [diff] [blame] | 151 | dbg_gen("get flags: %#x, i_flags %#x", flags, inode->i_flags); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 152 | return put_user(flags, (int __user *) arg); |
| 153 | |
| 154 | case FS_IOC_SETFLAGS: { |
| 155 | if (IS_RDONLY(inode)) |
| 156 | return -EROFS; |
| 157 | |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 158 | if (!inode_owner_or_capable(inode)) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 159 | return -EACCES; |
| 160 | |
| 161 | if (get_user(flags, (int __user *) arg)) |
| 162 | return -EFAULT; |
| 163 | |
Eric Biggers | 219b0e2 | 2019-12-09 14:23:25 -0800 | [diff] [blame] | 164 | if (flags & ~UBIFS_GETTABLE_IOCTL_FLAGS) |
Hou Tao | 2fe8b2d | 2019-02-09 16:54:20 +0800 | [diff] [blame] | 165 | return -EOPNOTSUPP; |
Eric Biggers | 219b0e2 | 2019-12-09 14:23:25 -0800 | [diff] [blame] | 166 | flags &= UBIFS_SETTABLE_IOCTL_FLAGS; |
Hou Tao | 2fe8b2d | 2019-02-09 16:54:20 +0800 | [diff] [blame] | 167 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 168 | if (!S_ISDIR(inode->i_mode)) |
| 169 | flags &= ~FS_DIRSYNC_FL; |
| 170 | |
| 171 | /* |
| 172 | * Make sure the file-system is read-write and make sure it |
| 173 | * will not become read-only while we are changing the flags. |
| 174 | */ |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 175 | err = mnt_want_write_file(file); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 176 | if (err) |
| 177 | return err; |
Artem Bityutskiy | a9f2fc0 | 2008-12-23 14:39:14 +0200 | [diff] [blame] | 178 | dbg_gen("set flags: %#x, i_flags %#x", flags, inode->i_flags); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 179 | err = setflags(inode, flags); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 180 | mnt_drop_write_file(file); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 181 | return err; |
| 182 | } |
Richard Weinberger | d475a50 | 2016-10-20 16:47:56 +0200 | [diff] [blame] | 183 | case FS_IOC_SET_ENCRYPTION_POLICY: { |
Richard Weinberger | e021986 | 2016-10-19 23:24:47 +0200 | [diff] [blame] | 184 | struct ubifs_info *c = inode->i_sb->s_fs_info; |
Richard Weinberger | d475a50 | 2016-10-20 16:47:56 +0200 | [diff] [blame] | 185 | |
Richard Weinberger | e021986 | 2016-10-19 23:24:47 +0200 | [diff] [blame] | 186 | err = ubifs_enable_encryption(c); |
| 187 | if (err) |
| 188 | return err; |
| 189 | |
Richard Weinberger | ec9160d | 2016-12-13 00:27:59 +0100 | [diff] [blame] | 190 | return fscrypt_ioctl_set_policy(file, (const void __user *)arg); |
Richard Weinberger | d475a50 | 2016-10-20 16:47:56 +0200 | [diff] [blame] | 191 | } |
Eric Biggers | cf39496 | 2019-03-15 13:55:03 -0700 | [diff] [blame] | 192 | case FS_IOC_GET_ENCRYPTION_POLICY: |
Richard Weinberger | ec9160d | 2016-12-13 00:27:59 +0100 | [diff] [blame] | 193 | return fscrypt_ioctl_get_policy(file, (void __user *)arg); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 194 | |
Eric Biggers | 62de259 | 2019-08-04 19:35:49 -0700 | [diff] [blame] | 195 | case FS_IOC_GET_ENCRYPTION_POLICY_EX: |
| 196 | return fscrypt_ioctl_get_policy_ex(file, (void __user *)arg); |
| 197 | |
| 198 | case FS_IOC_ADD_ENCRYPTION_KEY: |
| 199 | return fscrypt_ioctl_add_key(file, (void __user *)arg); |
| 200 | |
| 201 | case FS_IOC_REMOVE_ENCRYPTION_KEY: |
| 202 | return fscrypt_ioctl_remove_key(file, (void __user *)arg); |
| 203 | |
| 204 | case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS: |
| 205 | return fscrypt_ioctl_remove_key_all_users(file, |
| 206 | (void __user *)arg); |
| 207 | case FS_IOC_GET_ENCRYPTION_KEY_STATUS: |
| 208 | return fscrypt_ioctl_get_key_status(file, (void __user *)arg); |
| 209 | |
Eric Biggers | 861261f | 2020-03-14 13:50:52 -0700 | [diff] [blame] | 210 | case FS_IOC_GET_ENCRYPTION_NONCE: |
| 211 | return fscrypt_ioctl_get_nonce(file, (void __user *)arg); |
| 212 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 213 | default: |
| 214 | return -ENOTTY; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | #ifdef CONFIG_COMPAT |
| 219 | long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 220 | { |
| 221 | switch (cmd) { |
| 222 | case FS_IOC32_GETFLAGS: |
| 223 | cmd = FS_IOC_GETFLAGS; |
| 224 | break; |
| 225 | case FS_IOC32_SETFLAGS: |
| 226 | cmd = FS_IOC_SETFLAGS; |
| 227 | break; |
Eric Biggers | a75467d | 2016-12-19 11:12:48 -0800 | [diff] [blame] | 228 | case FS_IOC_SET_ENCRYPTION_POLICY: |
| 229 | case FS_IOC_GET_ENCRYPTION_POLICY: |
Eric Biggers | 62de259 | 2019-08-04 19:35:49 -0700 | [diff] [blame] | 230 | case FS_IOC_GET_ENCRYPTION_POLICY_EX: |
| 231 | case FS_IOC_ADD_ENCRYPTION_KEY: |
| 232 | case FS_IOC_REMOVE_ENCRYPTION_KEY: |
| 233 | case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS: |
| 234 | case FS_IOC_GET_ENCRYPTION_KEY_STATUS: |
Eric Biggers | 861261f | 2020-03-14 13:50:52 -0700 | [diff] [blame] | 235 | case FS_IOC_GET_ENCRYPTION_NONCE: |
Eric Biggers | a75467d | 2016-12-19 11:12:48 -0800 | [diff] [blame] | 236 | break; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 237 | default: |
| 238 | return -ENOIOCTLCMD; |
| 239 | } |
| 240 | return ubifs_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); |
| 241 | } |
| 242 | #endif |