main.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. /**
  2. * eCryptfs: Linux filesystem encryption layer
  3. *
  4. * Copyright (C) 1997-2003 Erez Zadok
  5. * Copyright (C) 2001-2003 Stony Brook University
  6. * Copyright (C) 2004-2007 International Business Machines Corp.
  7. * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
  8. * Michael C. Thompson <mcthomps@us.ibm.com>
  9. * Tyler Hicks <tyhicks@ou.edu>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  24. * 02111-1307, USA.
  25. */
  26. #include <linux/dcache.h>
  27. #include <linux/file.h>
  28. #include <linux/module.h>
  29. #include <linux/namei.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/crypto.h>
  32. #include <linux/mount.h>
  33. #include <linux/pagemap.h>
  34. #include <linux/key.h>
  35. #include <linux/parser.h>
  36. #include <linux/fs_stack.h>
  37. #include <linux/slab.h>
  38. #include <linux/magic.h>
  39. #include "ecryptfs_kernel.h"
  40. #ifdef CONFIG_WTL_ENCRYPTION_FILTER
  41. #include <linux/ctype.h>
  42. #endif
  43. /**
  44. * Module parameter that defines the ecryptfs_verbosity level.
  45. */
  46. int ecryptfs_verbosity = 0;
  47. module_param(ecryptfs_verbosity, int, 0);
  48. MODULE_PARM_DESC(ecryptfs_verbosity,
  49. "Initial verbosity level (0 or 1; defaults to "
  50. "0, which is Quiet)");
  51. /**
  52. * Module parameter that defines the number of message buffer elements
  53. */
  54. unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS;
  55. module_param(ecryptfs_message_buf_len, uint, 0);
  56. MODULE_PARM_DESC(ecryptfs_message_buf_len,
  57. "Number of message buffer elements");
  58. /**
  59. * Module parameter that defines the maximum guaranteed amount of time to wait
  60. * for a response from ecryptfsd. The actual sleep time will be, more than
  61. * likely, a small amount greater than this specified value, but only less if
  62. * the message successfully arrives.
  63. */
  64. signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ;
  65. module_param(ecryptfs_message_wait_timeout, long, 0);
  66. MODULE_PARM_DESC(ecryptfs_message_wait_timeout,
  67. "Maximum number of seconds that an operation will "
  68. "sleep while waiting for a message response from "
  69. "userspace");
  70. /**
  71. * Module parameter that is an estimate of the maximum number of users
  72. * that will be concurrently using eCryptfs. Set this to the right
  73. * value to balance performance and memory use.
  74. */
  75. unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS;
  76. module_param(ecryptfs_number_of_users, uint, 0);
  77. MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of "
  78. "concurrent users of eCryptfs");
  79. void __ecryptfs_printk(const char *fmt, ...)
  80. {
  81. va_list args;
  82. va_start(args, fmt);
  83. if (fmt[1] == '7') { /* KERN_DEBUG */
  84. if (ecryptfs_verbosity >= 1)
  85. vprintk(fmt, args);
  86. } else
  87. vprintk(fmt, args);
  88. va_end(args);
  89. }
  90. /**
  91. * ecryptfs_init_lower_file
  92. * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with
  93. * the lower dentry and the lower mount set
  94. *
  95. * eCryptfs only ever keeps a single open file for every lower
  96. * inode. All I/O operations to the lower inode occur through that
  97. * file. When the first eCryptfs dentry that interposes with the first
  98. * lower dentry for that inode is created, this function creates the
  99. * lower file struct and associates it with the eCryptfs
  100. * inode. When all eCryptfs files associated with the inode are released, the
  101. * file is closed.
  102. *
  103. * The lower file will be opened with read/write permissions, if
  104. * possible. Otherwise, it is opened read-only.
  105. *
  106. * This function does nothing if a lower file is already
  107. * associated with the eCryptfs inode.
  108. *
  109. * Returns zero on success; non-zero otherwise
  110. */
  111. static int ecryptfs_init_lower_file(struct dentry *dentry,
  112. struct file **lower_file)
  113. {
  114. const struct cred *cred = current_cred();
  115. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  116. struct vfsmount *lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
  117. int rc;
  118. rc = ecryptfs_privileged_open(lower_file, lower_dentry, lower_mnt,
  119. cred);
  120. if (rc) {
  121. printk(KERN_ERR "Error opening lower file "
  122. "for lower_dentry [0x%p] and lower_mnt [0x%p]; "
  123. "rc = [%d]\n", lower_dentry, lower_mnt, rc);
  124. (*lower_file) = NULL;
  125. }
  126. return rc;
  127. }
  128. int ecryptfs_get_lower_file(struct dentry *dentry, struct inode *inode)
  129. {
  130. struct ecryptfs_inode_info *inode_info;
  131. int count, rc = 0;
  132. inode_info = ecryptfs_inode_to_private(inode);
  133. mutex_lock(&inode_info->lower_file_mutex);
  134. count = atomic_inc_return(&inode_info->lower_file_count);
  135. if (WARN_ON_ONCE(count < 1))
  136. rc = -EINVAL;
  137. else if (count == 1) {
  138. rc = ecryptfs_init_lower_file(dentry,
  139. &inode_info->lower_file);
  140. if (rc)
  141. atomic_set(&inode_info->lower_file_count, 0);
  142. }
  143. mutex_unlock(&inode_info->lower_file_mutex);
  144. return rc;
  145. }
  146. void ecryptfs_put_lower_file(struct inode *inode)
  147. {
  148. struct ecryptfs_inode_info *inode_info;
  149. inode_info = ecryptfs_inode_to_private(inode);
  150. if (atomic_dec_and_mutex_lock(&inode_info->lower_file_count,
  151. &inode_info->lower_file_mutex)) {
  152. filemap_write_and_wait(inode->i_mapping);
  153. fput(inode_info->lower_file);
  154. inode_info->lower_file = NULL;
  155. mutex_unlock(&inode_info->lower_file_mutex);
  156. }
  157. }
  158. enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig,
  159. ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher,
  160. ecryptfs_opt_ecryptfs_key_bytes,
  161. ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata,
  162. ecryptfs_opt_encrypted_view, ecryptfs_opt_fnek_sig,
  163. ecryptfs_opt_fn_cipher, ecryptfs_opt_fn_cipher_key_bytes,
  164. ecryptfs_opt_unlink_sigs, ecryptfs_opt_mount_auth_tok_only,
  165. ecryptfs_opt_check_dev_ruid,
  166. #ifdef CONFIG_WTL_ENCRYPTION_FILTER
  167. ecryptfs_opt_enable_filtering,
  168. #endif
  169. #if defined(CONFIG_CRYPTO_FIPS) && !defined(CONFIG_FORCE_DISABLE_FIPS)
  170. ecryptfs_opt_enable_cc,
  171. #endif
  172. ecryptfs_opt_err };
  173. static const match_table_t tokens = {
  174. {ecryptfs_opt_sig, "sig=%s"},
  175. {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"},
  176. {ecryptfs_opt_cipher, "cipher=%s"},
  177. {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"},
  178. {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"},
  179. {ecryptfs_opt_passthrough, "ecryptfs_passthrough"},
  180. {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"},
  181. {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"},
  182. {ecryptfs_opt_fnek_sig, "ecryptfs_fnek_sig=%s"},
  183. {ecryptfs_opt_fn_cipher, "ecryptfs_fn_cipher=%s"},
  184. {ecryptfs_opt_fn_cipher_key_bytes, "ecryptfs_fn_key_bytes=%u"},
  185. {ecryptfs_opt_unlink_sigs, "ecryptfs_unlink_sigs"},
  186. {ecryptfs_opt_mount_auth_tok_only, "ecryptfs_mount_auth_tok_only"},
  187. {ecryptfs_opt_check_dev_ruid, "ecryptfs_check_dev_ruid"},
  188. #ifdef CONFIG_WTL_ENCRYPTION_FILTER
  189. {ecryptfs_opt_enable_filtering, "ecryptfs_enable_filtering=%s"},
  190. #endif
  191. #if defined(CONFIG_CRYPTO_FIPS) && !defined(CONFIG_FORCE_DISABLE_FIPS)
  192. {ecryptfs_opt_enable_cc, "ecryptfs_enable_cc"},
  193. #endif
  194. {ecryptfs_opt_err, NULL}
  195. };
  196. static int ecryptfs_init_global_auth_toks(
  197. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  198. {
  199. struct ecryptfs_global_auth_tok *global_auth_tok;
  200. struct ecryptfs_auth_tok *auth_tok;
  201. int rc = 0;
  202. list_for_each_entry(global_auth_tok,
  203. &mount_crypt_stat->global_auth_tok_list,
  204. mount_crypt_stat_list) {
  205. rc = ecryptfs_keyring_auth_tok_for_sig(
  206. &global_auth_tok->global_auth_tok_key, &auth_tok,
  207. global_auth_tok->sig);
  208. if (rc) {
  209. printk(KERN_ERR "Could not find valid key in user "
  210. "session keyring for sig specified in mount "
  211. "option: [%s]\n", global_auth_tok->sig);
  212. global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
  213. goto out;
  214. } else {
  215. global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
  216. up_write(&(global_auth_tok->global_auth_tok_key)->sem);
  217. }
  218. }
  219. out:
  220. return rc;
  221. }
  222. static void ecryptfs_init_mount_crypt_stat(
  223. struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
  224. {
  225. memset((void *)mount_crypt_stat, 0,
  226. sizeof(struct ecryptfs_mount_crypt_stat));
  227. INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list);
  228. mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex);
  229. mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED;
  230. }
  231. #ifdef CONFIG_WTL_ENCRYPTION_FILTER
  232. static int parse_enc_file_filter_parms(
  233. struct ecryptfs_mount_crypt_stat *mcs, char *str)
  234. {
  235. char *token = NULL;
  236. int count = 0;
  237. mcs->max_name_filter_len = 0;
  238. while ((token = strsep(&str, "|")) != NULL) {
  239. if (count >= ENC_NAME_FILTER_MAX_INSTANCE)
  240. return -1;
  241. strncpy(mcs->enc_filter_name[count++],
  242. token, ENC_NAME_FILTER_MAX_LEN);
  243. if (mcs->max_name_filter_len < strlen(token))
  244. mcs->max_name_filter_len = strlen(token);
  245. }
  246. return 0;
  247. }
  248. static int parse_enc_ext_filter_parms(
  249. struct ecryptfs_mount_crypt_stat *mcs, char *str)
  250. {
  251. char *token = NULL;
  252. int count = 0;
  253. while ((token = strsep(&str, "|")) != NULL) {
  254. if (count >= ENC_EXT_FILTER_MAX_INSTANCE)
  255. return -1;
  256. strncpy(mcs->enc_filter_ext[count++],
  257. token, ENC_EXT_FILTER_MAX_LEN);
  258. }
  259. return 0;
  260. }
  261. static int parse_enc_filter_parms(
  262. struct ecryptfs_mount_crypt_stat *mcs, char *str)
  263. {
  264. char *token = NULL;
  265. if (!strcmp("*", str)) {
  266. mcs->flags |= ECRYPTFS_ENABLE_NEW_PASSTHROUGH;
  267. return 0;
  268. }
  269. token = strsep(&str, ":");
  270. if (token != NULL)
  271. parse_enc_file_filter_parms(mcs, token);
  272. token = strsep(&str, ":");
  273. if (token != NULL)
  274. parse_enc_ext_filter_parms(mcs, token);
  275. return 0;
  276. }
  277. #endif
  278. /**
  279. * ecryptfs_parse_options
  280. * @sb: The ecryptfs super block
  281. * @options: The options passed to the kernel
  282. * @check_ruid: set to 1 if device uid should be checked against the ruid
  283. *
  284. * Parse mount options:
  285. * debug=N - ecryptfs_verbosity level for debug output
  286. * sig=XXX - description(signature) of the key to use
  287. *
  288. * Returns the dentry object of the lower-level (lower/interposed)
  289. * directory; We want to mount our stackable file system on top of
  290. * that lower directory.
  291. *
  292. * The signature of the key to use must be the description of a key
  293. * already in the keyring. Mounting will fail if the key can not be
  294. * found.
  295. *
  296. * Returns zero on success; non-zero on error
  297. */
  298. static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,
  299. uid_t *check_ruid)
  300. {
  301. char *p;
  302. int rc = 0;
  303. int sig_set = 0;
  304. int cipher_name_set = 0;
  305. int fn_cipher_name_set = 0;
  306. int cipher_key_bytes;
  307. int cipher_key_bytes_set = 0;
  308. int fn_cipher_key_bytes;
  309. int fn_cipher_key_bytes_set = 0;
  310. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  311. &sbi->mount_crypt_stat;
  312. substring_t args[MAX_OPT_ARGS];
  313. int token;
  314. char *sig_src;
  315. char *cipher_name_dst;
  316. char *cipher_name_src;
  317. char *fn_cipher_name_dst;
  318. char *fn_cipher_name_src;
  319. char *fnek_dst;
  320. char *fnek_src;
  321. char *cipher_key_bytes_src;
  322. char *fn_cipher_key_bytes_src;
  323. u8 cipher_code;
  324. #if defined(CONFIG_CRYPTO_FIPS) && !defined(CONFIG_FORCE_DISABLE_FIPS)
  325. char cipher_mode[ECRYPTFS_MAX_CIPHER_MODE_SIZE] = ECRYPTFS_AES_ECB_MODE;
  326. #endif
  327. *check_ruid = 0;
  328. if (!options) {
  329. rc = -EINVAL;
  330. goto out;
  331. }
  332. ecryptfs_init_mount_crypt_stat(mount_crypt_stat);
  333. while ((p = strsep(&options, ",")) != NULL) {
  334. if (!*p)
  335. continue;
  336. token = match_token(p, tokens, args);
  337. switch (token) {
  338. case ecryptfs_opt_sig:
  339. case ecryptfs_opt_ecryptfs_sig:
  340. sig_src = args[0].from;
  341. rc = ecryptfs_add_global_auth_tok(mount_crypt_stat,
  342. sig_src, 0);
  343. if (rc) {
  344. printk(KERN_ERR "Error attempting to register "
  345. "global sig; rc = [%d]\n", rc);
  346. goto out;
  347. }
  348. sig_set = 1;
  349. break;
  350. case ecryptfs_opt_cipher:
  351. case ecryptfs_opt_ecryptfs_cipher:
  352. cipher_name_src = args[0].from;
  353. cipher_name_dst =
  354. mount_crypt_stat->
  355. global_default_cipher_name;
  356. strncpy(cipher_name_dst, cipher_name_src,
  357. ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  358. cipher_name_dst[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
  359. cipher_name_set = 1;
  360. break;
  361. case ecryptfs_opt_ecryptfs_key_bytes:
  362. cipher_key_bytes_src = args[0].from;
  363. cipher_key_bytes =
  364. (int)simple_strtol(cipher_key_bytes_src,
  365. &cipher_key_bytes_src, 0);
  366. mount_crypt_stat->global_default_cipher_key_size =
  367. cipher_key_bytes;
  368. cipher_key_bytes_set = 1;
  369. break;
  370. case ecryptfs_opt_passthrough:
  371. mount_crypt_stat->flags |=
  372. ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED;
  373. break;
  374. case ecryptfs_opt_xattr_metadata:
  375. mount_crypt_stat->flags |=
  376. ECRYPTFS_XATTR_METADATA_ENABLED;
  377. break;
  378. case ecryptfs_opt_encrypted_view:
  379. mount_crypt_stat->flags |=
  380. ECRYPTFS_XATTR_METADATA_ENABLED;
  381. mount_crypt_stat->flags |=
  382. ECRYPTFS_ENCRYPTED_VIEW_ENABLED;
  383. break;
  384. case ecryptfs_opt_fnek_sig:
  385. fnek_src = args[0].from;
  386. fnek_dst =
  387. mount_crypt_stat->global_default_fnek_sig;
  388. strncpy(fnek_dst, fnek_src, ECRYPTFS_SIG_SIZE_HEX);
  389. mount_crypt_stat->global_default_fnek_sig[
  390. ECRYPTFS_SIG_SIZE_HEX] = '\0';
  391. rc = ecryptfs_add_global_auth_tok(
  392. mount_crypt_stat,
  393. mount_crypt_stat->global_default_fnek_sig,
  394. ECRYPTFS_AUTH_TOK_FNEK);
  395. if (rc) {
  396. printk(KERN_ERR "Error attempting to register "
  397. "global fnek sig [%s]; rc = [%d]\n",
  398. mount_crypt_stat->global_default_fnek_sig,
  399. rc);
  400. goto out;
  401. }
  402. mount_crypt_stat->flags |=
  403. (ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES
  404. | ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK);
  405. break;
  406. case ecryptfs_opt_fn_cipher:
  407. fn_cipher_name_src = args[0].from;
  408. fn_cipher_name_dst =
  409. mount_crypt_stat->global_default_fn_cipher_name;
  410. strncpy(fn_cipher_name_dst, fn_cipher_name_src,
  411. ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  412. mount_crypt_stat->global_default_fn_cipher_name[
  413. ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
  414. fn_cipher_name_set = 1;
  415. break;
  416. case ecryptfs_opt_fn_cipher_key_bytes:
  417. fn_cipher_key_bytes_src = args[0].from;
  418. fn_cipher_key_bytes =
  419. (int)simple_strtol(fn_cipher_key_bytes_src,
  420. &fn_cipher_key_bytes_src, 0);
  421. mount_crypt_stat->global_default_fn_cipher_key_bytes =
  422. fn_cipher_key_bytes;
  423. fn_cipher_key_bytes_set = 1;
  424. break;
  425. case ecryptfs_opt_unlink_sigs:
  426. mount_crypt_stat->flags |= ECRYPTFS_UNLINK_SIGS;
  427. break;
  428. case ecryptfs_opt_mount_auth_tok_only:
  429. mount_crypt_stat->flags |=
  430. ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY;
  431. break;
  432. case ecryptfs_opt_check_dev_ruid:
  433. *check_ruid = 1;
  434. break;
  435. #ifdef CONFIG_WTL_ENCRYPTION_FILTER
  436. case ecryptfs_opt_enable_filtering:
  437. rc = parse_enc_filter_parms(mount_crypt_stat,
  438. args[0].from);
  439. if (rc) {
  440. printk(KERN_ERR "Error attempting to parse encryption "
  441. "filtering parameters.\n");
  442. rc = -EINVAL;
  443. goto out;
  444. }
  445. mount_crypt_stat->flags |= ECRYPTFS_ENABLE_FILTERING;
  446. break;
  447. #endif
  448. #if defined(CONFIG_CRYPTO_FIPS) && !defined(CONFIG_FORCE_DISABLE_FIPS)
  449. case ecryptfs_opt_enable_cc:
  450. mount_crypt_stat->flags |= ECRYPTFS_ENABLE_CC;
  451. strncpy(cipher_mode, ECRYPTFS_AES_CBC_MODE, ECRYPTFS_MAX_CIPHER_MODE_SIZE);
  452. break;
  453. #endif
  454. case ecryptfs_opt_err:
  455. default:
  456. printk(KERN_WARNING
  457. "%s: eCryptfs: unrecognized option [%s]\n",
  458. __func__, p);
  459. }
  460. }
  461. if (!sig_set) {
  462. rc = -EINVAL;
  463. ecryptfs_printk(KERN_ERR, "You must supply at least one valid "
  464. "auth tok signature as a mount "
  465. "parameter; see the eCryptfs README\n");
  466. goto out;
  467. }
  468. if (!cipher_name_set) {
  469. int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER);
  470. BUG_ON(cipher_name_len >= ECRYPTFS_MAX_CIPHER_NAME_SIZE);
  471. strcpy(mount_crypt_stat->global_default_cipher_name,
  472. ECRYPTFS_DEFAULT_CIPHER);
  473. }
  474. if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
  475. && !fn_cipher_name_set)
  476. strcpy(mount_crypt_stat->global_default_fn_cipher_name,
  477. mount_crypt_stat->global_default_cipher_name);
  478. if (!cipher_key_bytes_set)
  479. mount_crypt_stat->global_default_cipher_key_size = 0;
  480. if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
  481. && !fn_cipher_key_bytes_set)
  482. mount_crypt_stat->global_default_fn_cipher_key_bytes =
  483. mount_crypt_stat->global_default_cipher_key_size;
  484. cipher_code = ecryptfs_code_for_cipher_string(
  485. mount_crypt_stat->global_default_cipher_name,
  486. mount_crypt_stat->global_default_cipher_key_size);
  487. if (!cipher_code) {
  488. ecryptfs_printk(KERN_ERR,
  489. "eCryptfs doesn't support cipher: %s",
  490. mount_crypt_stat->global_default_cipher_name);
  491. rc = -EINVAL;
  492. goto out;
  493. }
  494. mutex_lock(&key_tfm_list_mutex);
  495. #if defined(CONFIG_CRYPTO_FIPS) && !defined(CONFIG_FORCE_DISABLE_FIPS)
  496. if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name, cipher_mode,
  497. NULL)) {
  498. rc = ecryptfs_add_new_key_tfm(
  499. NULL, mount_crypt_stat->global_default_cipher_name,
  500. mount_crypt_stat->global_default_cipher_key_size,
  501. mount_crypt_stat->flags);
  502. if (rc) {
  503. printk(KERN_ERR "Error attempting to initialize "
  504. "cipher with name = [%s] and key size = [%td]; "
  505. "rc = [%d]\n",
  506. mount_crypt_stat->global_default_cipher_name,
  507. mount_crypt_stat->global_default_cipher_key_size,
  508. rc);
  509. rc = -EINVAL;
  510. mutex_unlock(&key_tfm_list_mutex);
  511. goto out;
  512. }
  513. }
  514. if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
  515. && !ecryptfs_tfm_exists(
  516. mount_crypt_stat->global_default_fn_cipher_name, cipher_mode, NULL)) {
  517. rc = ecryptfs_add_new_key_tfm(
  518. NULL, mount_crypt_stat->global_default_fn_cipher_name,
  519. mount_crypt_stat->global_default_fn_cipher_key_bytes,
  520. mount_crypt_stat->flags);
  521. if (rc) {
  522. printk(KERN_ERR "Error attempting to initialize "
  523. "cipher with name = [%s] and key size = [%td]; "
  524. "rc = [%d]\n",
  525. mount_crypt_stat->global_default_fn_cipher_name,
  526. mount_crypt_stat->global_default_fn_cipher_key_bytes,
  527. rc);
  528. rc = -EINVAL;
  529. mutex_unlock(&key_tfm_list_mutex);
  530. goto out;
  531. }
  532. }
  533. #else
  534. if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name,
  535. NULL)) {
  536. rc = ecryptfs_add_new_key_tfm(
  537. NULL, mount_crypt_stat->global_default_cipher_name,
  538. mount_crypt_stat->global_default_cipher_key_size);
  539. if (rc) {
  540. printk(KERN_ERR "Error attempting to initialize "
  541. "cipher with name = [%s] and key size = [%td]; "
  542. "rc = [%d]\n",
  543. mount_crypt_stat->global_default_cipher_name,
  544. mount_crypt_stat->global_default_cipher_key_size,
  545. rc);
  546. rc = -EINVAL;
  547. mutex_unlock(&key_tfm_list_mutex);
  548. goto out;
  549. }
  550. }
  551. if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
  552. && !ecryptfs_tfm_exists(
  553. mount_crypt_stat->global_default_fn_cipher_name, NULL)) {
  554. rc = ecryptfs_add_new_key_tfm(
  555. NULL, mount_crypt_stat->global_default_fn_cipher_name,
  556. mount_crypt_stat->global_default_fn_cipher_key_bytes);
  557. if (rc) {
  558. printk(KERN_ERR "Error attempting to initialize "
  559. "cipher with name = [%s] and key size = [%td]; "
  560. "rc = [%d]\n",
  561. mount_crypt_stat->global_default_fn_cipher_name,
  562. mount_crypt_stat->global_default_fn_cipher_key_bytes,
  563. rc);
  564. rc = -EINVAL;
  565. mutex_unlock(&key_tfm_list_mutex);
  566. goto out;
  567. }
  568. }
  569. #endif
  570. mutex_unlock(&key_tfm_list_mutex);
  571. rc = ecryptfs_init_global_auth_toks(mount_crypt_stat);
  572. if (rc)
  573. printk(KERN_WARNING "One or more global auth toks could not "
  574. "properly register; rc = [%d]\n", rc);
  575. out:
  576. return rc;
  577. }
  578. struct kmem_cache *ecryptfs_sb_info_cache;
  579. static struct file_system_type ecryptfs_fs_type;
  580. /**
  581. * ecryptfs_get_sb
  582. * @fs_type
  583. * @flags
  584. * @dev_name: The path to mount over
  585. * @raw_data: The options passed into the kernel
  586. */
  587. static struct dentry *ecryptfs_mount(struct file_system_type *fs_type, int flags,
  588. const char *dev_name, void *raw_data)
  589. {
  590. struct super_block *s, *lower_sb;
  591. struct ecryptfs_sb_info *sbi;
  592. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  593. struct ecryptfs_dentry_info *root_info;
  594. const char *err = "Getting sb failed";
  595. struct inode *inode;
  596. struct path path;
  597. uid_t check_ruid;
  598. int rc;
  599. sbi = kmem_cache_zalloc(ecryptfs_sb_info_cache, GFP_KERNEL);
  600. if (!sbi) {
  601. rc = -ENOMEM;
  602. goto out;
  603. }
  604. rc = ecryptfs_parse_options(sbi, raw_data, &check_ruid);
  605. if (rc) {
  606. err = "Error parsing options";
  607. goto out;
  608. }
  609. mount_crypt_stat = &sbi->mount_crypt_stat;
  610. s = sget(fs_type, NULL, set_anon_super, NULL);
  611. if (IS_ERR(s)) {
  612. rc = PTR_ERR(s);
  613. goto out;
  614. }
  615. rc = bdi_setup_and_register(&sbi->bdi, "ecryptfs", BDI_CAP_MAP_COPY);
  616. if (rc)
  617. goto out1;
  618. ecryptfs_set_superblock_private(s, sbi);
  619. s->s_bdi = &sbi->bdi;
  620. /* ->kill_sb() will take care of sbi after that point */
  621. sbi = NULL;
  622. s->s_op = &ecryptfs_sops;
  623. s->s_d_op = &ecryptfs_dops;
  624. err = "Reading sb failed";
  625. rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
  626. if (rc) {
  627. ecryptfs_printk(KERN_WARNING, "kern_path() failed\n");
  628. goto out1;
  629. }
  630. if (path.dentry->d_sb->s_type == &ecryptfs_fs_type) {
  631. rc = -EINVAL;
  632. printk(KERN_ERR "Mount on filesystem of type "
  633. "eCryptfs explicitly disallowed due to "
  634. "known incompatibilities\n");
  635. goto out_free;
  636. }
  637. if (check_ruid && path.dentry->d_inode->i_uid != current_uid()) {
  638. rc = -EPERM;
  639. printk(KERN_ERR "Mount of device (uid: %d) not owned by "
  640. "requested user (uid: %d)\n",
  641. path.dentry->d_inode->i_uid, current_uid());
  642. goto out_free;
  643. }
  644. lower_sb = path.dentry->d_sb;
  645. atomic_inc(&lower_sb->s_active);
  646. ecryptfs_set_superblock_lower(s, path.dentry->d_sb);
  647. /**
  648. * Set the POSIX ACL flag based on whether they're enabled in the lower
  649. * mount.
  650. */
  651. s->s_flags = flags & ~MS_POSIXACL;
  652. s->s_flags |= path.dentry->d_sb->s_flags & MS_POSIXACL;
  653. /**
  654. * Force a read-only eCryptfs mount when:
  655. * 1) The lower mount is ro
  656. * 2) The ecryptfs_encrypted_view mount option is specified
  657. */
  658. if (path.dentry->d_sb->s_flags & MS_RDONLY ||
  659. mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
  660. s->s_flags |= MS_RDONLY;
  661. s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
  662. s->s_blocksize = path.dentry->d_sb->s_blocksize;
  663. s->s_magic = ECRYPTFS_SUPER_MAGIC;
  664. s->s_stack_depth = path.dentry->d_sb->s_stack_depth + 1;
  665. rc = -EINVAL;
  666. if (s->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
  667. pr_err("eCryptfs: maximum fs stacking depth exceeded\n");
  668. goto out_free;
  669. }
  670. inode = ecryptfs_get_inode(path.dentry->d_inode, s);
  671. rc = PTR_ERR(inode);
  672. if (IS_ERR(inode))
  673. goto out_sput;
  674. s->s_root = d_make_root(inode);
  675. if (!s->s_root) {
  676. rc = -ENOMEM;
  677. goto out_sput;
  678. }
  679. rc = -ENOMEM;
  680. root_info = kmem_cache_zalloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
  681. if (!root_info)
  682. goto out_sput;
  683. /* ->kill_sb() will take care of root_info */
  684. ecryptfs_set_dentry_private(s->s_root, root_info);
  685. ecryptfs_set_dentry_lower(s->s_root, path.dentry);
  686. ecryptfs_set_dentry_lower_mnt(s->s_root, path.mnt);
  687. s->s_flags |= MS_ACTIVE;
  688. return dget(s->s_root);
  689. out_sput:
  690. atomic_dec(&lower_sb->s_active);
  691. out_free:
  692. path_put(&path);
  693. out1:
  694. deactivate_locked_super(s);
  695. out:
  696. if (sbi) {
  697. ecryptfs_destroy_mount_crypt_stat(&sbi->mount_crypt_stat);
  698. kmem_cache_free(ecryptfs_sb_info_cache, sbi);
  699. }
  700. printk(KERN_ERR "%s; rc = [%d]\n", err, rc);
  701. return ERR_PTR(rc);
  702. }
  703. /**
  704. * ecryptfs_kill_block_super
  705. * @sb: The ecryptfs super block
  706. *
  707. * Used to bring the superblock down and free the private data.
  708. */
  709. static void ecryptfs_kill_block_super(struct super_block *sb)
  710. {
  711. struct ecryptfs_sb_info *sb_info = ecryptfs_superblock_to_private(sb);
  712. kill_anon_super(sb);
  713. if (!sb_info)
  714. return;
  715. if (sb_info->wsi_sb)
  716. atomic_dec(&sb_info->wsi_sb->s_active);
  717. ecryptfs_destroy_mount_crypt_stat(&sb_info->mount_crypt_stat);
  718. bdi_destroy(&sb_info->bdi);
  719. kmem_cache_free(ecryptfs_sb_info_cache, sb_info);
  720. }
  721. static struct file_system_type ecryptfs_fs_type = {
  722. .owner = THIS_MODULE,
  723. .name = "ecryptfs",
  724. .mount = ecryptfs_mount,
  725. .kill_sb = ecryptfs_kill_block_super,
  726. .fs_flags = 0
  727. };
  728. MODULE_ALIAS_FS("ecryptfs");
  729. /**
  730. * inode_info_init_once
  731. *
  732. * Initializes the ecryptfs_inode_info_cache when it is created
  733. */
  734. static void
  735. inode_info_init_once(void *vptr)
  736. {
  737. struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr;
  738. inode_init_once(&ei->vfs_inode);
  739. }
  740. static struct ecryptfs_cache_info {
  741. struct kmem_cache **cache;
  742. const char *name;
  743. size_t size;
  744. void (*ctor)(void *obj);
  745. } ecryptfs_cache_infos[] = {
  746. {
  747. .cache = &ecryptfs_auth_tok_list_item_cache,
  748. .name = "ecryptfs_auth_tok_list_item",
  749. .size = sizeof(struct ecryptfs_auth_tok_list_item),
  750. },
  751. {
  752. .cache = &ecryptfs_file_info_cache,
  753. .name = "ecryptfs_file_cache",
  754. .size = sizeof(struct ecryptfs_file_info),
  755. },
  756. {
  757. .cache = &ecryptfs_dentry_info_cache,
  758. .name = "ecryptfs_dentry_info_cache",
  759. .size = sizeof(struct ecryptfs_dentry_info),
  760. },
  761. {
  762. .cache = &ecryptfs_inode_info_cache,
  763. .name = "ecryptfs_inode_cache",
  764. .size = sizeof(struct ecryptfs_inode_info),
  765. .ctor = inode_info_init_once,
  766. },
  767. {
  768. .cache = &ecryptfs_sb_info_cache,
  769. .name = "ecryptfs_sb_cache",
  770. .size = sizeof(struct ecryptfs_sb_info),
  771. },
  772. {
  773. .cache = &ecryptfs_header_cache,
  774. .name = "ecryptfs_headers",
  775. .size = PAGE_CACHE_SIZE,
  776. },
  777. {
  778. .cache = &ecryptfs_xattr_cache,
  779. .name = "ecryptfs_xattr_cache",
  780. .size = PAGE_CACHE_SIZE,
  781. },
  782. {
  783. .cache = &ecryptfs_key_record_cache,
  784. .name = "ecryptfs_key_record_cache",
  785. .size = sizeof(struct ecryptfs_key_record),
  786. },
  787. {
  788. .cache = &ecryptfs_key_sig_cache,
  789. .name = "ecryptfs_key_sig_cache",
  790. .size = sizeof(struct ecryptfs_key_sig),
  791. },
  792. {
  793. .cache = &ecryptfs_global_auth_tok_cache,
  794. .name = "ecryptfs_global_auth_tok_cache",
  795. .size = sizeof(struct ecryptfs_global_auth_tok),
  796. },
  797. {
  798. .cache = &ecryptfs_key_tfm_cache,
  799. .name = "ecryptfs_key_tfm_cache",
  800. .size = sizeof(struct ecryptfs_key_tfm),
  801. },
  802. {
  803. .cache = &ecryptfs_open_req_cache,
  804. .name = "ecryptfs_open_req_cache",
  805. .size = sizeof(struct ecryptfs_open_req),
  806. },
  807. };
  808. static void ecryptfs_free_kmem_caches(void)
  809. {
  810. int i;
  811. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  812. struct ecryptfs_cache_info *info;
  813. info = &ecryptfs_cache_infos[i];
  814. if (*(info->cache))
  815. kmem_cache_destroy(*(info->cache));
  816. }
  817. }
  818. /**
  819. * ecryptfs_init_kmem_caches
  820. *
  821. * Returns zero on success; non-zero otherwise
  822. */
  823. static int ecryptfs_init_kmem_caches(void)
  824. {
  825. int i;
  826. for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) {
  827. struct ecryptfs_cache_info *info;
  828. info = &ecryptfs_cache_infos[i];
  829. *(info->cache) = kmem_cache_create(info->name, info->size,
  830. 0, SLAB_HWCACHE_ALIGN, info->ctor);
  831. if (!*(info->cache)) {
  832. ecryptfs_free_kmem_caches();
  833. ecryptfs_printk(KERN_WARNING, "%s: "
  834. "kmem_cache_create failed\n",
  835. info->name);
  836. return -ENOMEM;
  837. }
  838. }
  839. return 0;
  840. }
  841. static struct kobject *ecryptfs_kobj;
  842. static ssize_t version_show(struct kobject *kobj,
  843. struct kobj_attribute *attr, char *buff)
  844. {
  845. return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);
  846. }
  847. static struct kobj_attribute version_attr = __ATTR_RO(version);
  848. static struct attribute *attributes[] = {
  849. &version_attr.attr,
  850. NULL,
  851. };
  852. static struct attribute_group attr_group = {
  853. .attrs = attributes,
  854. };
  855. static int do_sysfs_registration(void)
  856. {
  857. int rc;
  858. ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj);
  859. if (!ecryptfs_kobj) {
  860. printk(KERN_ERR "Unable to create ecryptfs kset\n");
  861. rc = -ENOMEM;
  862. goto out;
  863. }
  864. rc = sysfs_create_group(ecryptfs_kobj, &attr_group);
  865. if (rc) {
  866. printk(KERN_ERR
  867. "Unable to create ecryptfs version attributes\n");
  868. kobject_put(ecryptfs_kobj);
  869. }
  870. out:
  871. return rc;
  872. }
  873. static void do_sysfs_unregistration(void)
  874. {
  875. sysfs_remove_group(ecryptfs_kobj, &attr_group);
  876. kobject_put(ecryptfs_kobj);
  877. }
  878. static int __init ecryptfs_init(void)
  879. {
  880. int rc;
  881. if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) {
  882. rc = -EINVAL;
  883. ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is "
  884. "larger than the host's page size, and so "
  885. "eCryptfs cannot run on this system. The "
  886. "default eCryptfs extent size is [%u] bytes; "
  887. "the page size is [%lu] bytes.\n",
  888. ECRYPTFS_DEFAULT_EXTENT_SIZE,
  889. (unsigned long)PAGE_CACHE_SIZE);
  890. goto out;
  891. }
  892. rc = ecryptfs_init_kmem_caches();
  893. if (rc) {
  894. printk(KERN_ERR
  895. "Failed to allocate one or more kmem_cache objects\n");
  896. goto out;
  897. }
  898. rc = do_sysfs_registration();
  899. if (rc) {
  900. printk(KERN_ERR "sysfs registration failed\n");
  901. goto out_free_kmem_caches;
  902. }
  903. rc = ecryptfs_init_kthread();
  904. if (rc) {
  905. printk(KERN_ERR "%s: kthread initialization failed; "
  906. "rc = [%d]\n", __func__, rc);
  907. goto out_do_sysfs_unregistration;
  908. }
  909. rc = ecryptfs_init_messaging();
  910. if (rc) {
  911. printk(KERN_ERR "Failure occurred while attempting to "
  912. "initialize the communications channel to "
  913. "ecryptfsd\n");
  914. goto out_destroy_kthread;
  915. }
  916. rc = ecryptfs_init_crypto();
  917. if (rc) {
  918. printk(KERN_ERR "Failure whilst attempting to init crypto; "
  919. "rc = [%d]\n", rc);
  920. goto out_release_messaging;
  921. }
  922. rc = register_filesystem(&ecryptfs_fs_type);
  923. if (rc) {
  924. printk(KERN_ERR "Failed to register filesystem\n");
  925. goto out_destroy_crypto;
  926. }
  927. if (ecryptfs_verbosity > 0)
  928. printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values "
  929. "will be written to the syslog!\n", ecryptfs_verbosity);
  930. goto out;
  931. out_destroy_crypto:
  932. ecryptfs_destroy_crypto();
  933. out_release_messaging:
  934. ecryptfs_release_messaging();
  935. out_destroy_kthread:
  936. ecryptfs_destroy_kthread();
  937. out_do_sysfs_unregistration:
  938. do_sysfs_unregistration();
  939. out_free_kmem_caches:
  940. ecryptfs_free_kmem_caches();
  941. out:
  942. return rc;
  943. }
  944. static void __exit ecryptfs_exit(void)
  945. {
  946. int rc;
  947. rc = ecryptfs_destroy_crypto();
  948. if (rc)
  949. printk(KERN_ERR "Failure whilst attempting to destroy crypto; "
  950. "rc = [%d]\n", rc);
  951. ecryptfs_release_messaging();
  952. ecryptfs_destroy_kthread();
  953. do_sysfs_unregistration();
  954. unregister_filesystem(&ecryptfs_fs_type);
  955. ecryptfs_free_kmem_caches();
  956. }
  957. MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");
  958. MODULE_DESCRIPTION("eCryptfs");
  959. MODULE_LICENSE("GPL");
  960. module_init(ecryptfs_init)
  961. module_exit(ecryptfs_exit)