Skip to content

Commit 56bd06c

Browse files
Eric Sandeenbrauner
authored andcommitted
ecryptfs: Factor out mount option validation
Under the new mount API, mount options are parsed one at a time. Any validation that examines multiple options must be done after parsing is complete, so factor out a ecryptfs_validate_options() which can be called separately. To facilitate this, temporarily move the local variables that tracked whether various options have been set in the parsing function, into the ecryptfs_mount_crypt_stat structure so that they can be examined later. These will be moved to a more ephemeral struct in the mount api conversion patch to follow. Signed-off-by: Eric Sandeen <sandeen@redhat.com> Link: https://lore.kernel.org/r/20241028143359.605061-2-sandeen@redhat.com Acked-by: Tyler Hicks <code@tyhicks.com> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent b4201b5 commit 56bd06c

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

fs/ecryptfs/ecryptfs_kernel.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,13 @@ struct ecryptfs_mount_crypt_stat {
343343
unsigned char global_default_fn_cipher_name[
344344
ECRYPTFS_MAX_CIPHER_NAME_SIZE + 1];
345345
char global_default_fnek_sig[ECRYPTFS_SIG_SIZE_HEX + 1];
346+
/* Mount option status trackers */
347+
bool check_ruid;
348+
bool sig_set;
349+
bool cipher_name_set;
350+
bool cipher_key_bytes_set;
351+
bool fn_cipher_name_set;
352+
bool fn_cipher_key_bytes_set;
346353
};
347354

348355
/* superblock private data. */

fs/ecryptfs/main.c

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -239,18 +239,12 @@ static void ecryptfs_init_mount_crypt_stat(
239239
*
240240
* Returns zero on success; non-zero on error
241241
*/
242-
static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,
243-
uid_t *check_ruid)
242+
static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options)
244243
{
245244
char *p;
246245
int rc = 0;
247-
int sig_set = 0;
248-
int cipher_name_set = 0;
249-
int fn_cipher_name_set = 0;
250246
int cipher_key_bytes;
251-
int cipher_key_bytes_set = 0;
252247
int fn_cipher_key_bytes;
253-
int fn_cipher_key_bytes_set = 0;
254248
struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
255249
&sbi->mount_crypt_stat;
256250
substring_t args[MAX_OPT_ARGS];
@@ -261,9 +255,6 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,
261255
char *fnek_src;
262256
char *cipher_key_bytes_src;
263257
char *fn_cipher_key_bytes_src;
264-
u8 cipher_code;
265-
266-
*check_ruid = 0;
267258

268259
if (!options) {
269260
rc = -EINVAL;
@@ -285,14 +276,14 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,
285276
"global sig; rc = [%d]\n", rc);
286277
goto out;
287278
}
288-
sig_set = 1;
279+
mount_crypt_stat->sig_set = 1;
289280
break;
290281
case ecryptfs_opt_cipher:
291282
case ecryptfs_opt_ecryptfs_cipher:
292283
cipher_name_src = args[0].from;
293284
strscpy(mount_crypt_stat->global_default_cipher_name,
294285
cipher_name_src);
295-
cipher_name_set = 1;
286+
mount_crypt_stat->cipher_name_set = 1;
296287
break;
297288
case ecryptfs_opt_ecryptfs_key_bytes:
298289
cipher_key_bytes_src = args[0].from;
@@ -301,7 +292,7 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,
301292
&cipher_key_bytes_src, 0);
302293
mount_crypt_stat->global_default_cipher_key_size =
303294
cipher_key_bytes;
304-
cipher_key_bytes_set = 1;
295+
mount_crypt_stat->cipher_key_bytes_set = 1;
305296
break;
306297
case ecryptfs_opt_passthrough:
307298
mount_crypt_stat->flags |=
@@ -340,7 +331,7 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,
340331
fn_cipher_name_src = args[0].from;
341332
strscpy(mount_crypt_stat->global_default_fn_cipher_name,
342333
fn_cipher_name_src);
343-
fn_cipher_name_set = 1;
334+
mount_crypt_stat->fn_cipher_name_set = 1;
344335
break;
345336
case ecryptfs_opt_fn_cipher_key_bytes:
346337
fn_cipher_key_bytes_src = args[0].from;
@@ -349,7 +340,7 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,
349340
&fn_cipher_key_bytes_src, 0);
350341
mount_crypt_stat->global_default_fn_cipher_key_bytes =
351342
fn_cipher_key_bytes;
352-
fn_cipher_key_bytes_set = 1;
343+
mount_crypt_stat->fn_cipher_key_bytes_set = 1;
353344
break;
354345
case ecryptfs_opt_unlink_sigs:
355346
mount_crypt_stat->flags |= ECRYPTFS_UNLINK_SIGS;
@@ -359,7 +350,7 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,
359350
ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY;
360351
break;
361352
case ecryptfs_opt_check_dev_ruid:
362-
*check_ruid = 1;
353+
mount_crypt_stat->check_ruid = 1;
363354
break;
364355
case ecryptfs_opt_err:
365356
default:
@@ -368,28 +359,39 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,
368359
__func__, p);
369360
}
370361
}
371-
if (!sig_set) {
362+
363+
out:
364+
return rc;
365+
}
366+
367+
static int ecryptfs_validate_options(
368+
struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
369+
{
370+
int rc = 0;
371+
u8 cipher_code;
372+
373+
if (!mount_crypt_stat->sig_set) {
372374
rc = -EINVAL;
373375
ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
374376
"auth tok signature as a mount "
375377
"parameter; see the eCryptfs README\n");
376378
goto out;
377379
}
378-
if (!cipher_name_set) {
380+
if (!mount_crypt_stat->cipher_name_set) {
379381
int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
380382

381383
BUG_ON(cipher_name_len > ECRYPTFS_MAX_CIPHER_NAME_SIZE);
382384
strcpy(mount_crypt_stat->global_default_cipher_name,
383385
ECRYPTFS_DEFAULT_CIPHER);
384386
}
385387
if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
386-
&& !fn_cipher_name_set)
388+
&& !mount_crypt_stat->fn_cipher_name_set)
387389
strcpy(mount_crypt_stat->global_default_fn_cipher_name,
388390
mount_crypt_stat->global_default_cipher_name);
389-
if (!cipher_key_bytes_set)
391+
if (!mount_crypt_stat->cipher_key_bytes_set)
390392
mount_crypt_stat->global_default_cipher_key_size = 0;
391393
if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
392-
&& !fn_cipher_key_bytes_set)
394+
&& !mount_crypt_stat->fn_cipher_key_bytes_set)
393395
mount_crypt_stat->global_default_fn_cipher_key_bytes =
394396
mount_crypt_stat->global_default_cipher_key_size;
395397

@@ -469,7 +471,6 @@ static struct dentry *ecryptfs_mount(struct file_system_type *fs_type, int flags
469471
const char *err = "Getting sb failed";
470472
struct inode *inode;
471473
struct path path;
472-
uid_t check_ruid;
473474
int rc;
474475

475476
sbi = kmem_cache_zalloc(ecryptfs_sb_info_cache, GFP_KERNEL);
@@ -484,12 +485,17 @@ static struct dentry *ecryptfs_mount(struct file_system_type *fs_type, int flags
484485
goto out;
485486
}
486487

487-
rc = ecryptfs_parse_options(sbi, raw_data, &check_ruid);
488+
rc = ecryptfs_parse_options(sbi, raw_data);
488489
if (rc) {
489490
err = "Error parsing options";
490491
goto out;
491492
}
492493
mount_crypt_stat = &sbi->mount_crypt_stat;
494+
rc = ecryptfs_validate_options(mount_crypt_stat);
495+
if (rc) {
496+
err = "Error validationg options";
497+
goto out;
498+
}
493499

494500
s = sget(fs_type, NULL, set_anon_super, flags, NULL);
495501
if (IS_ERR(s)) {
@@ -529,7 +535,8 @@ static struct dentry *ecryptfs_mount(struct file_system_type *fs_type, int flags
529535
goto out_free;
530536
}
531537

532-
if (check_ruid && !uid_eq(d_inode(path.dentry)->i_uid, current_uid())) {
538+
if (mount_crypt_stat->check_ruid &&
539+
!uid_eq(d_inode(path.dentry)->i_uid, current_uid())) {
533540
rc = -EPERM;
534541
printk(KERN_ERR "Mount of device (uid: %d) not owned by "
535542
"requested user (uid: %d)\n",

0 commit comments

Comments
 (0)