pft.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774
  1. /*
  2. * Copyright (c) 2014, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. /*
  14. * Per-File-Tagger (PFT).
  15. *
  16. * This driver tags enterprise file for encryption/decryption,
  17. * as part of the Per-File-Encryption (PFE) feature.
  18. *
  19. * Enterprise registered applications are identified by their UID.
  20. *
  21. * The PFT exposes character-device interface to the user-space application,
  22. * to handle the following commands:
  23. * 1. Update registered applications list
  24. * 2. Encryption (in-place) of a file that was created before.
  25. * 3. Set State - update the state.
  26. *
  27. * The PFT exposes kernel API hooks that are intercepting file operations
  28. * like create/open/read/write for tagging files and also for access control.
  29. * It utilizes the existing security framework hooks
  30. * that calls the selinux hooks.
  31. *
  32. * The PFT exposes kernel API to the dm-req-crypt driver to provide the info
  33. * if a file is tagged or not. The dm-req-crypt driver is doing the
  34. * actual encryption/decryptiom.
  35. *
  36. * Tagging the file:
  37. * 1. Non-volatile tagging on storage using file extra-attribute (xattr).
  38. * 2. Volatile tagging on the file's inode, for fast access.
  39. *
  40. */
  41. /* Uncomment the line below to enable debug messages */
  42. /* #define DEBUG 1 */
  43. #define pr_fmt(fmt) "pft [%s]: " fmt, __func__
  44. #include <linux/kernel.h>
  45. #include <linux/slab.h>
  46. #include <linux/module.h>
  47. #include <linux/fs.h>
  48. #include <linux/cdev.h>
  49. #include <linux/sched.h>
  50. #include <linux/uaccess.h>
  51. #include <linux/cred.h>
  52. #include <linux/list.h>
  53. #include <linux/errno.h>
  54. #include <linux/printk.h>
  55. #include <linux/blkdev.h>
  56. #include <linux/elevator.h>
  57. #include <linux/bio.h>
  58. #include <linux/bitops.h>
  59. #include <linux/fdtable.h>
  60. #include <linux/selinux.h>
  61. #include <linux/pft.h>
  62. #include <linux/msm_pft.h>
  63. #include "objsec.h"
  64. /* File tagging as encrypted/non-encrypted is valid */
  65. #define PFT_TAG_MAGIC ((u32)(0xABC00000))
  66. /* File tagged as encrypted */
  67. #define PFT_TAG_ENCRYPTED BIT(16)
  68. #define PFT_TAG_MAGIC_MASK 0xFFF00000
  69. #define PFT_TAG_FLAGS_MASK 0x000F0000
  70. #define PFT_TAG_KEY_MASK 0x0000FFFF
  71. /* The defualt encryption key index */
  72. #define PFT_DEFAULT_KEY_INDEX 1
  73. /* The defualt key index for non-encrypted files */
  74. #define PFT_NO_KEY 0
  75. /* PFT extended attribute name */
  76. #define XATTR_NAME_PFE "security.pfe"
  77. /* PFT driver requested major number */
  78. #define PFT_REQUESTED_MAJOR 213
  79. /* PFT driver name */
  80. #define DEVICE_NAME "pft"
  81. /* Maximum registered applications */
  82. #define PFT_MAX_APPS 1000
  83. /* Maximum command size */
  84. #define PFT_MAX_COMMAND_SIZE (PAGE_SIZE)
  85. /* Current Process ID */
  86. #define current_pid() ((u32)(current->pid))
  87. static const char *pft_state_name[PFT_STATE_MAX_INDEX] = {
  88. "deactivated",
  89. "deactivating",
  90. "key_removed",
  91. "removing_key",
  92. "key_loaded",
  93. };
  94. /**
  95. * struct pft_file_info - pft file node info.
  96. * @file: pointer to file stucture.
  97. * @pid: process ID.
  98. * @list: next list item.
  99. *
  100. * A node in the list of the current open encrypted files.
  101. */
  102. struct pft_file_info {
  103. struct file *file;
  104. pid_t pid;
  105. struct list_head list;
  106. };
  107. /**
  108. * struct pft_device - device state structure.
  109. *
  110. * @open_count: device open count.
  111. * @major: device major number.
  112. * @state: Per-File-Encryption state.
  113. * @response: command response.
  114. * @pfm_pid: PFM process id.
  115. * @inplace_file: file for in-place encryption.
  116. * @uid_table: registered application array (UID).
  117. * @uid_count: number of registered applications.
  118. * @open_file_list: open encrypted file list.
  119. * @lock: lock protect list access.
  120. *
  121. * The open_count purpose is to ensure that only one user space
  122. * application uses this driver.
  123. * The open_file_list is used to close open encrypted files
  124. * after the key is removed from the encryption hardware.
  125. */
  126. struct pft_device {
  127. struct cdev cdev;
  128. dev_t device_no;
  129. struct class *driver_class;
  130. int open_count;
  131. int major;
  132. enum pft_state state;
  133. struct pft_command_response response;
  134. u32 pfm_pid;
  135. struct file *inplace_file;
  136. u32 *uid_table;
  137. u32 uid_count;
  138. struct list_head open_file_list;
  139. struct mutex lock;
  140. };
  141. /* Device Driver State */
  142. static struct pft_device *pft_dev;
  143. static struct inode *pft_bio_get_inode(struct bio *bio);
  144. /**
  145. * pft_is_ready() - driver is initialized and ready.
  146. *
  147. * Return: true if the driver is ready.
  148. */
  149. static bool pft_is_ready(void)
  150. {
  151. return (pft_dev != NULL);
  152. }
  153. /**
  154. * file_to_filename() - get the filename from file pointer.
  155. * @filp: file pointer
  156. *
  157. * it is used for debug prints.
  158. *
  159. * Return: filename string or "unknown".
  160. */
  161. static char *file_to_filename(struct file *filp)
  162. {
  163. struct dentry *dentry = NULL;
  164. char *filename = NULL;
  165. if (!filp || !filp->f_dentry)
  166. return "unknown";
  167. dentry = filp->f_dentry;
  168. filename = dentry->d_iname;
  169. return filename;
  170. }
  171. /**
  172. * inode_to_filename() - get the filename from inode pointer.
  173. * @inode: inode pointer
  174. *
  175. * it is used for debug prints.
  176. *
  177. * Return: filename string or "unknown".
  178. */
  179. static char *inode_to_filename(struct inode *inode)
  180. {
  181. struct dentry *dentry = NULL;
  182. char *filename = NULL;
  183. if (list_empty(&inode->i_dentry))
  184. return "unknown";
  185. dentry = list_first_entry(&inode->i_dentry, struct dentry, d_alias);
  186. filename = dentry->d_iname;
  187. return filename;
  188. }
  189. /**
  190. * pft_set_response() - set response error code.
  191. *
  192. * @error_code: The error code to return on response.
  193. */
  194. static inline void pft_set_response(u32 error_code)
  195. {
  196. pft_dev->response.error_code = error_code;
  197. }
  198. /**
  199. * pft_add_file()- Add the file to the list of opened encrypted
  200. * files.
  201. * @filp: file to add.
  202. *
  203. * Return: 0 of successful operation, negative value otherwise.
  204. */
  205. static int pft_add_file(struct file *filp)
  206. {
  207. struct pft_file_info *node = NULL;
  208. node = kzalloc(sizeof(*node), GFP_KERNEL);
  209. if (!node) {
  210. pr_err("malloc failure\n");
  211. return -ENOMEM;
  212. }
  213. node->file = filp;
  214. INIT_LIST_HEAD(&node->list);
  215. mutex_lock(&pft_dev->lock);
  216. list_add(&node->list, &pft_dev->open_file_list);
  217. pr_debug("adding file %s to open list.\n", file_to_filename(filp));
  218. mutex_unlock(&pft_dev->lock);
  219. return 0;
  220. }
  221. /**
  222. * pft_remove_file()- Remove the given file from the list of
  223. * open encrypted files.
  224. * @filp: file to remove.
  225. *
  226. * Return: 0 on success, negative value on failure.
  227. */
  228. static int pft_remove_file(struct file *filp)
  229. {
  230. int ret = -ENOENT;
  231. struct pft_file_info *tmp = NULL;
  232. struct list_head *pos = NULL;
  233. struct list_head *next = NULL;
  234. bool found = false;
  235. mutex_lock(&pft_dev->lock);
  236. list_for_each_safe(pos, next, &pft_dev->open_file_list) {
  237. tmp = list_entry(pos, struct pft_file_info, list);
  238. if (filp == tmp->file) {
  239. found = true;
  240. break;
  241. }
  242. }
  243. if (found) {
  244. pr_debug("remove file %s. from open list.\n ",
  245. file_to_filename(filp));
  246. list_del(&tmp->list);
  247. kfree(tmp);
  248. ret = 0;
  249. }
  250. mutex_unlock(&pft_dev->lock);
  251. return ret;
  252. }
  253. /**
  254. * pft_is_current_process_registered()- Check if current process
  255. * is registered.
  256. *
  257. * Return: true if current process is registered.
  258. */
  259. static bool pft_is_current_process_registered(void)
  260. {
  261. int is_registered = false;
  262. int i;
  263. u32 uid = current_uid();
  264. mutex_lock(&pft_dev->lock);
  265. for (i = 0; i < pft_dev->uid_count; i++) {
  266. if (pft_dev->uid_table[i] == uid) {
  267. pr_debug("current UID [%u] is registerd.\n", uid);
  268. is_registered = true;
  269. break;
  270. }
  271. }
  272. mutex_unlock(&pft_dev->lock);
  273. return is_registered;
  274. }
  275. /**
  276. * pft_is_xattr_supported() - Check if the filesystem supports
  277. * extended attributes.
  278. * @indoe: pointer to the file inode
  279. *
  280. * Return: true if supported, false if not.
  281. */
  282. static bool pft_is_xattr_supported(struct inode *inode)
  283. {
  284. if (inode == NULL) {
  285. pr_err("invalid argument inode passed as NULL");
  286. return false;
  287. }
  288. if (inode->i_security == NULL) {
  289. pr_debug("i_security is NULL, not ready yet\n");
  290. return false;
  291. }
  292. if (inode->i_op == NULL) {
  293. pr_debug("i_op is NULL\n");
  294. return false;
  295. }
  296. if (inode->i_op->getxattr == NULL) {
  297. pr_debug_once("getxattr() not supported , filename=%s\n",
  298. inode_to_filename(inode));
  299. return false;
  300. }
  301. if (inode->i_op->setxattr == NULL) {
  302. pr_debug("setxattr() not supported\n");
  303. return false;
  304. }
  305. return true;
  306. }
  307. /**
  308. * pft_get_inode_tag() - get the file tag.
  309. * @indoe: pointer to the file inode
  310. *
  311. * Return: tag
  312. */
  313. static u32 pft_get_inode_tag(struct inode *inode)
  314. {
  315. struct inode_security_struct *isec = inode->i_security;
  316. if (isec == NULL)
  317. return 0;
  318. return isec->tag;
  319. }
  320. /**
  321. * pft_get_inode_key_index() - get the file key.
  322. * @indoe: pointer to the file inode
  323. *
  324. * Return: key index
  325. */
  326. static inline u32 pft_get_inode_key_index(struct inode *inode)
  327. {
  328. return pft_get_inode_tag(inode) & PFT_TAG_KEY_MASK;
  329. }
  330. /**
  331. * pft_is_tag_valid() - is the tag valid
  332. * @indoe: pointer to the file inode
  333. *
  334. * The tagging is set to valid when an enterprise file is created
  335. * or when an file is opened first time after power up and the
  336. * xattr was checked to see if the file is encrypted or not.
  337. *
  338. * Return: true if the tag is valid.
  339. */
  340. static inline bool pft_is_tag_valid(struct inode *inode)
  341. {
  342. struct inode_security_struct *isec = inode->i_security;
  343. if (isec == NULL)
  344. return false;
  345. return ((isec->tag & PFT_TAG_MAGIC_MASK) == PFT_TAG_MAGIC) ?
  346. true : false;
  347. }
  348. /**
  349. * pft_is_file_encrypted() - is inode tagged as encrypted.
  350. *
  351. * @tag: holds the key index and tagging flags.
  352. *
  353. * Return: true if the file is encrypted.
  354. */
  355. static inline bool pft_is_file_encrypted(u32 tag)
  356. {
  357. return (tag & PFT_TAG_ENCRYPTED) ? true : false;
  358. }
  359. /**
  360. * pft_tag_inode_non_encrypted() - Tag the inode as
  361. * non-encrypted.
  362. * @indoe: pointer to the file inode
  363. *
  364. * Tag file as non-encrypted, only the valid bit is set,
  365. * the encrypted bit is not set.
  366. */
  367. static inline void pft_tag_inode_non_encrypted(struct inode *inode)
  368. {
  369. struct inode_security_struct *isec = inode->i_security;
  370. isec->tag = (u32)(PFT_TAG_MAGIC);
  371. }
  372. /**
  373. * pft_tag_inode_encrypted() - Tag the inode as encrypted.
  374. * @indoe: pointer to the file inode
  375. *
  376. * Set the valid bit, the encrypted bit, and the key index.
  377. */
  378. static void pft_tag_inode_encrypted(struct inode *inode, u32 key_index)
  379. {
  380. struct inode_security_struct *isec = inode->i_security;
  381. isec->tag = key_index | PFT_TAG_ENCRYPTED | PFT_TAG_MAGIC;
  382. }
  383. /**
  384. * pft_get_file_tag()- get the file tag.
  385. * @dentry: pointer to file dentry.
  386. * @tag_ptr: pointer to tag.
  387. *
  388. * This is the major function for detecting tag files.
  389. * Get the tag from the inode if tag is valid,
  390. * or from the xattr if this is the 1st time after power up.
  391. *
  392. * Return: 0 on successe, negative value on failure.
  393. */
  394. static int pft_get_file_tag(struct dentry *dentry, u32 *tag_ptr)
  395. {
  396. ssize_t size = 0;
  397. struct inode *inode;
  398. const char *xattr_name = XATTR_NAME_PFE;
  399. u32 key;
  400. if (!dentry || !dentry->d_inode || !tag_ptr) {
  401. pr_err("invalid param");
  402. return -EINVAL;
  403. }
  404. inode = dentry->d_inode;
  405. if (pft_is_tag_valid(inode)) {
  406. *tag_ptr = pft_get_inode_tag(inode);
  407. return 0;
  408. }
  409. /*
  410. * For the first time reading the tag, the tag is not valid, hence
  411. * get xattr.
  412. */
  413. size = inode->i_op->getxattr(dentry, xattr_name, &key, sizeof(key));
  414. if (size == -ENODATA || size == -EOPNOTSUPP) {
  415. pft_tag_inode_non_encrypted(inode);
  416. *tag_ptr = pft_get_inode_tag(inode);
  417. } else if (size > 0) {
  418. pr_debug("First time file %s opened, found xattr = %u.\n",
  419. inode_to_filename(inode), key);
  420. pft_tag_inode_encrypted(inode, key);
  421. *tag_ptr = pft_get_inode_tag(inode);
  422. } else {
  423. pr_err("getxattr() failure, ret=%d.\n", size);
  424. return -EINVAL;
  425. }
  426. return 0;
  427. }
  428. /**
  429. * pft_tag_file() - Tag the file saving the key_index.
  430. * @dentry: file dentry.
  431. * @key_index: encryption key index.
  432. *
  433. * This is the major fuction for tagging a file.
  434. * Tag the file on both the xattr and the inode.
  435. *
  436. * Return: 0 on successe, negative value on failure.
  437. */
  438. static int pft_tag_file(struct dentry *dentry, u32 key_index)
  439. {
  440. int size = 0;
  441. const char *xattr_name = XATTR_NAME_PFE;
  442. if (!dentry || !dentry->d_inode) {
  443. pr_err("invalid NULL param");
  444. return -EINVAL;
  445. }
  446. if (!pft_is_xattr_supported(dentry->d_inode)) {
  447. pr_err("set xattr for file %s is not support.\n",
  448. dentry->d_iname);
  449. return -EINVAL;
  450. }
  451. size = dentry->d_inode->i_op->setxattr(dentry, xattr_name, &key_index,
  452. sizeof(key_index), 0);
  453. if (size < 0) {
  454. pr_err("failed to set xattr for file %s, ret =%d.\n",
  455. dentry->d_iname, size);
  456. return -EFAULT;
  457. }
  458. pft_tag_inode_encrypted(dentry->d_inode, key_index);
  459. pr_debug("file %s tagged encrypted\n", dentry->d_iname);
  460. return 0;
  461. }
  462. /**
  463. * pft_get_app_key_index() - get the application key index.
  464. * @uid: registered application UID
  465. *
  466. * Get key index based on the given registered application UID.
  467. * Currently only one key is supported.
  468. *
  469. * Return: encryption key index.
  470. */
  471. static inline u32 pft_get_app_key_index(u32 uid)
  472. {
  473. return PFT_DEFAULT_KEY_INDEX;
  474. }
  475. /**
  476. * pft_is_encrypted_file() - is the file encrypted.
  477. * @dentry: file pointer.
  478. *
  479. * Return: true if the file is encrypted, false otherwise.
  480. */
  481. static bool pft_is_encrypted_file(struct dentry *dentry)
  482. {
  483. int rc;
  484. u32 tag;
  485. if (!pft_is_ready())
  486. return false;
  487. if (!pft_is_xattr_supported(dentry->d_inode))
  488. return false;
  489. rc = pft_get_file_tag(dentry, &tag);
  490. if (rc < 0)
  491. return false;
  492. return pft_is_file_encrypted(tag);
  493. }
  494. /**
  495. * pft_is_inplace_inode() - is this the inode of file for
  496. * in-place encryption.
  497. * @inode: inode of file to check.
  498. *
  499. * Return: true if this file is being encrypted, false
  500. * otherwise.
  501. */
  502. static bool pft_is_inplace_inode(struct inode *inode)
  503. {
  504. if (!pft_dev->inplace_file || !pft_dev->inplace_file->f_path.dentry)
  505. return false;
  506. return (pft_dev->inplace_file->f_path.dentry->d_inode == inode);
  507. }
  508. /**
  509. * pft_is_inplace_file() - is this the file for in-place
  510. * encryption.
  511. * @filp: file to check.
  512. *
  513. * A file struct might be allocated per process, inode should be
  514. * only one.
  515. *
  516. * Return: true if this file is being encrypted, false
  517. * otherwise.
  518. */
  519. static inline bool pft_is_inplace_file(struct file *filp)
  520. {
  521. if (!filp || !filp->f_path.dentry || !filp->f_path.dentry->d_inode)
  522. return false;
  523. return pft_is_inplace_inode(filp->f_path.dentry->d_inode);
  524. }
  525. /**
  526. * pft_get_key_index() - get the key index and other indications
  527. * @inode: Pointer to inode struct
  528. * @key_index: Pointer to the return value of key index
  529. * @is_encrypted: Pointer to the return value.
  530. * @is_inplace: Pointer to the return value.
  531. *
  532. * Provides the given inode's encryption key index, and well as
  533. * indications whether the file is encrypted or is it currently
  534. * being in-placed encrypted.
  535. * This API is called by the dm-req-crypt to decide if to
  536. * encrypt/decrypt the file.
  537. * File tagging depends on the hooks to be called from selinux,
  538. * so if selinux is disabled then tagging is also not
  539. * valid.
  540. *
  541. * Return: 0 on successe, negative value on failure.
  542. */
  543. int pft_get_key_index(struct bio *bio, u32 *key_index,
  544. bool *is_encrypted, bool *is_inplace)
  545. {
  546. u32 tag = 0;
  547. struct inode *inode = NULL;
  548. if (!pft_is_ready())
  549. return -ENODEV;
  550. if (!selinux_is_enabled())
  551. return -ENODEV;
  552. if (!bio)
  553. return -EPERM;
  554. if (!is_encrypted) {
  555. pr_err("is_encrypted is NULL\n");
  556. return -EPERM;
  557. }
  558. if (!is_inplace) {
  559. pr_err("is_inplace is NULL\n");
  560. return -EPERM;
  561. }
  562. if (!key_index) {
  563. pr_err("key_index is NULL\n");
  564. return -EPERM;
  565. }
  566. inode = pft_bio_get_inode(bio);
  567. if (!inode)
  568. return -EINVAL;
  569. if (!pft_is_tag_valid(inode)) {
  570. pr_debug("file %s, Tag not valid\n", inode_to_filename(inode));
  571. return -EINVAL;
  572. }
  573. if (!pft_is_xattr_supported(inode)) {
  574. *is_encrypted = false;
  575. *is_inplace = false;
  576. *key_index = 0;
  577. return 0;
  578. }
  579. tag = pft_get_inode_tag(inode);
  580. *is_encrypted = pft_is_file_encrypted(tag);
  581. *key_index = pft_get_inode_key_index(inode);
  582. *is_inplace = pft_is_inplace_inode(inode);
  583. if (*is_encrypted)
  584. pr_debug("file %s is encrypted\n", inode_to_filename(inode));
  585. return 0;
  586. }
  587. EXPORT_SYMBOL(pft_get_key_index);
  588. /**
  589. * pft_bio_get_inode() - get the inode from a bio.
  590. * @bio: Pointer to BIO structure.
  591. *
  592. * Walk the bio struct links to get the inode.
  593. *
  594. * Return: pointer to the inode struct if successful, or NULL otherwise.
  595. */
  596. static struct inode *pft_bio_get_inode(struct bio *bio)
  597. {
  598. if (!bio)
  599. return NULL;
  600. if (!bio->bi_io_vec)
  601. return NULL;
  602. if (!bio->bi_io_vec->bv_page)
  603. return NULL;
  604. if (PageAnon(bio->bi_io_vec->bv_page)) {
  605. struct inode *inode;
  606. /* Using direct-io (O_DIRECT) without page cache */
  607. inode = dio_bio_get_inode(bio);
  608. pr_debug("inode on direct-io, inode = 0x%x.\n", (int) inode);
  609. return inode;
  610. }
  611. if (!bio->bi_io_vec->bv_page->mapping)
  612. return NULL;
  613. if (!bio->bi_io_vec->bv_page->mapping->host)
  614. return NULL;
  615. return bio->bi_io_vec->bv_page->mapping->host;
  616. }
  617. /**
  618. * pft_allow_merge_bio()- Check if 2 BIOs can be merged.
  619. * @bio1: Pointer to first BIO structure.
  620. * @bio2: Pointer to second BIO structure.
  621. *
  622. * Prevent merging of BIOs from encrypted and non-encrypted
  623. * files, or files encrypted with different key.
  624. * This API is called by the file system block layer.
  625. *
  626. * Return: true if the BIOs allowed to be merged, false
  627. * otherwise.
  628. */
  629. bool pft_allow_merge_bio(struct bio *bio1, struct bio *bio2)
  630. {
  631. u32 key_index1 = 0, key_index2 = 0;
  632. bool is_encrypted1 = false, is_encrypted2 = false;
  633. bool allow = false;
  634. bool is_inplace = false; /* N.A. */
  635. int ret;
  636. if (!pft_is_ready())
  637. return true;
  638. ret = pft_get_key_index(bio1, &key_index1,
  639. &is_encrypted1, &is_inplace);
  640. if (ret)
  641. is_encrypted1 = false;
  642. ret = pft_get_key_index(bio2, &key_index2,
  643. &is_encrypted2, &is_inplace);
  644. if (ret)
  645. is_encrypted2 = false;
  646. allow = ((is_encrypted1 == is_encrypted2) &&
  647. (key_index1 == key_index2));
  648. return allow;
  649. }
  650. EXPORT_SYMBOL(pft_allow_merge_bio);
  651. /**
  652. * pft_inode_create() - file creation callback.
  653. * @dir: directory inode pointer
  654. * @dentry: file dentry pointer
  655. * @mode: flags
  656. *
  657. * This hook is called when file is created by VFS.
  658. * This hook is called from the selinux driver.
  659. * This hooks check file creation permission for enterprise
  660. * applications.
  661. * Call path:
  662. * vfs_create()->security_inode_create()->selinux_inode_create()
  663. *
  664. * Return: 0 on successe, negative value on failure.
  665. */
  666. int pft_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
  667. {
  668. if (!dir || !dentry)
  669. return 0;
  670. if (!pft_is_ready())
  671. return 0;
  672. switch (pft_dev->state) {
  673. case PFT_STATE_DEACTIVATED:
  674. case PFT_STATE_KEY_LOADED:
  675. break;
  676. case PFT_STATE_KEY_REMOVED:
  677. case PFT_STATE_DEACTIVATING:
  678. case PFT_STATE_REMOVING_KEY:
  679. /* At this state no new encrypted files can be created */
  680. if (pft_is_current_process_registered()) {
  681. pr_debug("key removed, registered uid %u is denied from creating new file %s\n",
  682. current_uid(), dentry->d_iname);
  683. return -EACCES;
  684. }
  685. break;
  686. default:
  687. BUG(); /* State is set by "set state" command */
  688. break;
  689. }
  690. return 0;
  691. }
  692. EXPORT_SYMBOL(pft_inode_create);
  693. /**
  694. * pft_inode_post_create() - file creation callback.
  695. * @dir: directory inode pointer
  696. * @dentry: file dentry pointer
  697. * @mode: flags
  698. *
  699. * This hook is called when file is created by VFS.
  700. * This hook is called from the selinux driver.
  701. * This hooks tags new files as encrypted when created by
  702. * enterprise applications.
  703. * Call path:
  704. * vfs_create()->security_inode_post_create()->selinux_inode_post_create()
  705. *
  706. * Return: 0 on successe, negative value on failure.
  707. */
  708. int pft_inode_post_create(struct inode *dir, struct dentry *dentry,
  709. umode_t mode)
  710. {
  711. int ret;
  712. if (!dir || !dentry)
  713. return 0;
  714. if (!pft_is_ready())
  715. return 0;
  716. switch (pft_dev->state) {
  717. case PFT_STATE_DEACTIVATED:
  718. case PFT_STATE_KEY_REMOVED:
  719. case PFT_STATE_DEACTIVATING:
  720. case PFT_STATE_REMOVING_KEY:
  721. break;
  722. case PFT_STATE_KEY_LOADED:
  723. /* Check whether the new file should be encrypted */
  724. if (pft_is_current_process_registered()) {
  725. u32 key_index = pft_get_app_key_index(current_uid());
  726. ret = pft_tag_file(dentry, key_index);
  727. if (ret == 0)
  728. pr_debug("key loaded, pid [%u] uid [%d] is creating file %s\n",
  729. current_pid(), current_uid(),
  730. dentry->d_iname);
  731. else {
  732. pr_err("Failed to tag file %s by pid %d\n",
  733. dentry->d_iname, current_pid());
  734. return -EFAULT;
  735. }
  736. }
  737. break;
  738. default:
  739. BUG(); /* State is set by "set state" command */
  740. break;
  741. }
  742. return 0;
  743. }
  744. EXPORT_SYMBOL(pft_inode_post_create);
  745. /**
  746. * pft_inode_mknod() - mknode file hook (callback)
  747. * @dir: directory inode pointer
  748. * @dentry: file dentry pointer
  749. * @mode: flags
  750. * @dev:
  751. *
  752. * This hook checks encrypted file access permission by
  753. * enterprise application.
  754. * Call path:
  755. * vfs_mknod()->security_inode_mknod()->selinux_inode_mknod()->pft_inode_mknod()
  756. *
  757. * Return: 0 on successful operation, negative value otherwise.
  758. */
  759. int pft_inode_mknod(struct inode *dir, struct dentry *dentry,
  760. umode_t mode, dev_t dev)
  761. {
  762. int rc;
  763. /* Check if allowed to create new encrypted files */
  764. rc = pft_inode_create(dir, dentry, mode);
  765. return rc;
  766. }
  767. EXPORT_SYMBOL(pft_inode_mknod);
  768. /**
  769. * pft_inode_rename() - file rename hook.
  770. * @inode: directory inode
  771. * @dentry: file dentry
  772. * @new_inode
  773. * @new_dentry
  774. *
  775. * Block attempt to rename enterprise file.
  776. *
  777. * Return: 0 on allowed operation, negative value otherwise.
  778. */
  779. int pft_inode_rename(struct inode *inode, struct dentry *dentry,
  780. struct inode *new_inode, struct dentry *new_dentry)
  781. {
  782. if (!inode || !dentry || !new_inode || !new_dentry || !dentry->d_inode)
  783. return 0;
  784. if (!pft_is_ready())
  785. return 0;
  786. /* do nothing for non-encrypted files */
  787. if (!pft_is_encrypted_file(dentry))
  788. return 0;
  789. pr_debug("attempt to rename encrypted file [%s]\n", dentry->d_iname);
  790. if (pft_is_inplace_inode(dentry->d_inode)) {
  791. pr_err("access in-place-encryption file %s by uid [%d] pid [%d] is blocked.\n",
  792. inode_to_filename(inode), current_uid(), current_pid());
  793. return -EACCES;
  794. }
  795. if (!pft_is_current_process_registered()) {
  796. pr_err("unregistered app (uid %u pid %u) is trying to access encrypted file %s\n",
  797. current_uid(), current_pid(), dentry->d_iname);
  798. return -EACCES;
  799. } else
  800. pr_debug("rename file %s\n", dentry->d_iname);
  801. return 0;
  802. }
  803. EXPORT_SYMBOL(pft_inode_rename);
  804. /**
  805. * pft_file_open() - file open hook (callback).
  806. * @filp: file pointer
  807. * @cred: credentials pointer
  808. *
  809. * This hook is called when file is opened by VFS.
  810. * It is called from the selinux driver.
  811. * It checks enterprise file xattr when first opened.
  812. * It adds encrypted file to the list of open files.
  813. * Call path:
  814. * do_filp_open()->security_dentry_open()->selinux_dentry_open()
  815. *
  816. * Return: 0 on successe, negative value on failure.
  817. */
  818. int pft_file_open(struct file *filp, const struct cred *cred)
  819. {
  820. int ret;
  821. if (!filp || !filp->f_path.dentry)
  822. return 0;
  823. if (!pft_is_ready())
  824. return 0;
  825. if (filp->f_flags & O_DIRECT)
  826. pr_debug("file %s using O_DIRECT.\n", file_to_filename(filp));
  827. /* do nothing for non-encrypted files */
  828. if (!pft_is_encrypted_file(filp->f_dentry))
  829. return 0;
  830. /*
  831. * Only PFM allowed to access in-place-encryption-file
  832. * during in-place-encryption process
  833. */
  834. if (pft_is_inplace_file(filp) && current_pid() != pft_dev->pfm_pid) {
  835. pr_err("Access in-place-encryption file %s by uid %d pid %d is blocked.\n",
  836. file_to_filename(filp), current_uid(), current_pid());
  837. return -EACCES;
  838. }
  839. switch (pft_dev->state) {
  840. case PFT_STATE_DEACTIVATED:
  841. case PFT_STATE_KEY_REMOVED:
  842. case PFT_STATE_DEACTIVATING:
  843. case PFT_STATE_REMOVING_KEY:
  844. /* Block any access for encrypted files when key not loaded */
  845. pr_debug("key not loaded. uid (%u) can not access file %s\n",
  846. current_uid(), file_to_filename(filp));
  847. return -EACCES;
  848. case PFT_STATE_KEY_LOADED:
  849. /* Only registered apps may access encrypted files. */
  850. if (!pft_is_current_process_registered()) {
  851. pr_err("unregistered app (uid %u pid %u) is trying to access encrypted file %s\n",
  852. current_uid(), current_pid(),
  853. file_to_filename(filp));
  854. return -EACCES;
  855. }
  856. ret = pft_add_file(filp);
  857. if (ret) {
  858. pr_err("failed to add file %s to the list.\n",
  859. file_to_filename(filp));
  860. return -EFAULT;
  861. }
  862. break;
  863. default:
  864. BUG(); /* State is set by "set state" command */
  865. break;
  866. }
  867. return 0;
  868. }
  869. EXPORT_SYMBOL(pft_file_open);
  870. /**
  871. * pft_file_permission() - check file access permission.
  872. * @filp: file pointer
  873. * @mask: flags
  874. *
  875. * This hook is called when file is read/write by VFS.
  876. * This hook is called from the selinux driver.
  877. * This hook checks encrypted file access permission by
  878. * enterprise application.
  879. * Call path:
  880. * vfs_read()->security_file_permission()->selinux_file_permission()
  881. *
  882. * Return: 0 on successe, negative value on failure.
  883. */
  884. int pft_file_permission(struct file *filp, int mask)
  885. {
  886. if (!filp)
  887. return 0;
  888. if (!pft_is_ready())
  889. return 0;
  890. /* do nothing for non-encrypted files */
  891. if (!pft_is_encrypted_file(filp->f_dentry))
  892. return 0;
  893. /*
  894. * Only PFM allowed to access in-place-encryption-file
  895. * during in-place encryption process
  896. */
  897. if (pft_is_inplace_file(filp)) {
  898. if (current_pid() == pft_dev->pfm_pid) {
  899. /* mask MAY_WRITE=2 / MAY_READ=4 */
  900. pr_debug("r/w [mask 0x%x] in-place-encryption file %s by PFM (UID %d, PID %d).\n",
  901. mask, file_to_filename(filp),
  902. current_uid(), current_pid());
  903. return 0;
  904. } else {
  905. pr_err("Access in-place-encryption file %s by App (UID %d, PID %d) is blocked.\n",
  906. file_to_filename(filp),
  907. current_uid(), current_pid());
  908. return -EACCES;
  909. }
  910. }
  911. switch (pft_dev->state) {
  912. case PFT_STATE_DEACTIVATED:
  913. case PFT_STATE_KEY_REMOVED:
  914. case PFT_STATE_DEACTIVATING:
  915. case PFT_STATE_REMOVING_KEY:
  916. /* Block any access for encrypted files when key not loaded */
  917. pr_debug("key not loaded. uid (%u) can not access file %s\n",
  918. current_uid(), file_to_filename(filp));
  919. return -EACCES;
  920. case PFT_STATE_KEY_LOADED:
  921. /* Only registered apps can access encrypted files. */
  922. if (!pft_is_current_process_registered()) {
  923. pr_err("unregistered app (uid %u pid %u) is trying to access encrypted file %s\n",
  924. current_uid(), current_pid(),
  925. file_to_filename(filp));
  926. return -EACCES;
  927. }
  928. break;
  929. default:
  930. BUG(); /* State is set by "set state" command */
  931. break;
  932. }
  933. return 0;
  934. }
  935. EXPORT_SYMBOL(pft_file_permission);
  936. /**
  937. * pft_sync_file() - sync the file.
  938. * @filp: file pointer
  939. *
  940. * Complete writting any pending write request of encrypted data
  941. * before key is removed, to avoid writting garbage to
  942. * enterprise files.
  943. */
  944. static void pft_sync_file(struct file *filp)
  945. {
  946. int ret;
  947. ret = vfs_fsync(filp, false);
  948. if (ret)
  949. pr_debug("failed to sync file %s, ret = %d.\n",
  950. file_to_filename(filp), ret);
  951. else
  952. pr_debug("Sync file %s ok.\n", file_to_filename(filp));
  953. }
  954. /**
  955. * pft_file_close()- handle file close event
  956. * @filp: file pointer
  957. *
  958. * This hook is called when file is closed by VFS.
  959. * This hook is called from the selinux driver.
  960. *
  961. * Return: 0 on successful operation, negative value otherwise.
  962. */
  963. int pft_file_close(struct file *filp)
  964. {
  965. if (!filp)
  966. return 0;
  967. if (!pft_is_ready())
  968. return 0;
  969. /* do nothing for non-encrypted files */
  970. if (!pft_is_encrypted_file(filp->f_dentry))
  971. return 0;
  972. if (pft_is_inplace_file(filp)) {
  973. pr_debug("pid [%u] uid [%u] is closing in-place-encryption file %s\n",
  974. current_pid(), current_uid(), file_to_filename(filp));
  975. pft_dev->inplace_file = NULL;
  976. }
  977. pft_sync_file(filp);
  978. pft_remove_file(filp);
  979. return 0;
  980. }
  981. EXPORT_SYMBOL(pft_file_close);
  982. /**
  983. * pft_inode_unlink() - Delete file hook.
  984. * @dir: directory inode pointer
  985. * @dentry: file dentry pointer
  986. *
  987. * call path: vfs_unlink()->security_inode_unlink().
  988. *
  989. * Return: 0 on successful operation, negative value otherwise.
  990. */
  991. int pft_inode_unlink(struct inode *dir, struct dentry *dentry)
  992. {
  993. struct inode *inode = NULL;
  994. if (!dir || !dentry || !dentry->d_inode)
  995. return 0;
  996. if (!pft_is_ready())
  997. return 0;
  998. inode = dentry->d_inode;
  999. /* do nothing for non-encrypted files */
  1000. if (!pft_is_encrypted_file(dentry))
  1001. return 0;
  1002. if (pft_is_inplace_inode(inode)) {
  1003. pr_err("block delete in-place-encryption file %s by uid [%d] pid [%d], while encryption in progress.\n",
  1004. inode_to_filename(inode), current_uid(), current_pid());
  1005. return -EBUSY;
  1006. }
  1007. if (!pft_is_current_process_registered()) {
  1008. pr_err("unregistered app (uid %u pid %u) is trying to access encrypted file %s\n",
  1009. current_uid(), current_pid(), inode_to_filename(inode));
  1010. return -EACCES;
  1011. } else
  1012. pr_debug("delete file %s\n", inode_to_filename(inode));
  1013. return 0;
  1014. }
  1015. EXPORT_SYMBOL(pft_inode_unlink);
  1016. /**
  1017. * pft_inode_set_xattr() - set/remove xattr callback.
  1018. * @dentry: file dentry pointer
  1019. * @name: xattr name.
  1020. *
  1021. * This hook checks attempt to set/remove PFE xattr.
  1022. * Only this kernel driver allows to set the PFE xattr, so block
  1023. * any attempt to do it from user space. Allow access for other
  1024. * xattr.
  1025. *
  1026. * Return: 0 on successful operation, negative value otherwise.
  1027. */
  1028. int pft_inode_set_xattr(struct dentry *dentry, const char *name)
  1029. {
  1030. struct inode *inode = NULL;
  1031. if (!dentry || !dentry->d_inode)
  1032. return 0;
  1033. inode = dentry->d_inode;
  1034. if (strcmp(name, XATTR_NAME_PFE) != 0) {
  1035. pr_debug("xattr name=%s file %s\n", name,
  1036. inode_to_filename(inode));
  1037. return 0; /* Not PFE xattr so it is ok */
  1038. }
  1039. pr_err("Attemp to set/remove PFE xattr for file %s\n",
  1040. inode_to_filename(inode));
  1041. /* Only PFT kernel driver allows to set the PFE xattr */
  1042. return -EACCES;
  1043. }
  1044. EXPORT_SYMBOL(pft_inode_set_xattr);
  1045. /**
  1046. * pft_close_opened_enc_files() - Close all the currently open
  1047. * encrypted files
  1048. *
  1049. * Close all open encrypted file when removing key or
  1050. * deactivating.
  1051. */
  1052. static void pft_close_opened_enc_files(void)
  1053. {
  1054. struct pft_file_info *tmp = NULL;
  1055. struct list_head *pos = NULL;
  1056. struct list_head *next = NULL;
  1057. list_for_each_safe(pos, next, &pft_dev->open_file_list) {
  1058. struct file *filp;
  1059. tmp = list_entry(pos, struct pft_file_info, list);
  1060. filp = tmp->file;
  1061. pr_debug("closing file %s.\n", file_to_filename(filp));
  1062. /* filp_close() eventually calls pft_file_close() */
  1063. filp_close(filp, NULL);
  1064. }
  1065. }
  1066. /**
  1067. * pft_set_state() - Handle "Set State" command
  1068. * @command: command buffer.
  1069. * @size: size of command buffer.
  1070. *
  1071. * The command execution status is reported by the response.
  1072. *
  1073. * Return: 0 on successful operation, negative value otherwise.
  1074. */
  1075. static int pft_set_state(struct pft_command *command, int size)
  1076. {
  1077. u32 state = command->set_state.state;
  1078. int expected_size = sizeof(command->opcode) +
  1079. sizeof(command->set_state);
  1080. if (size != expected_size) {
  1081. pr_err("Invalid buffer size\n");
  1082. pft_set_response(PFT_CMD_RESP_INVALID_CMD_PARAMS);
  1083. return -EINVAL;
  1084. }
  1085. if (state >= PFT_STATE_MAX_INDEX) {
  1086. pr_err("Invalid state %d\n", command->set_state.state);
  1087. pft_set_response(PFT_CMD_RESP_INVALID_STATE);
  1088. return 0;
  1089. }
  1090. pr_debug("Set State %d [%s].\n", state, pft_state_name[state]);
  1091. switch (command->set_state.state) {
  1092. case PFT_STATE_DEACTIVATING:
  1093. case PFT_STATE_REMOVING_KEY:
  1094. pft_close_opened_enc_files();
  1095. /* Fall through */
  1096. case PFT_STATE_DEACTIVATED:
  1097. case PFT_STATE_KEY_LOADED:
  1098. case PFT_STATE_KEY_REMOVED:
  1099. pft_dev->state = command->set_state.state;
  1100. pft_set_response(PFT_CMD_RESP_SUCCESS);
  1101. break;
  1102. default:
  1103. pr_err("Invalid state %d\n", command->set_state.state);
  1104. pft_set_response(PFT_CMD_RESP_INVALID_STATE);
  1105. break;
  1106. }
  1107. return 0;
  1108. }
  1109. /**
  1110. * pft_get_process_open_file() - get file pointer using file
  1111. * descriptor index.
  1112. * @index: file descriptor index.
  1113. *
  1114. * Return: file pointer on success, NULL on failure.
  1115. */
  1116. static struct file *pft_get_process_open_file(int index)
  1117. {
  1118. struct fdtable *files_table;
  1119. files_table = files_fdtable(current->files);
  1120. if (files_table == NULL)
  1121. return NULL;
  1122. if (index >= files_table->max_fds)
  1123. return NULL;
  1124. else
  1125. return files_table->fd[index];
  1126. }
  1127. /**
  1128. * pft_set_inplace_file() - handle "inplace file encryption"
  1129. * command.
  1130. * @command: command buffer.
  1131. * @size: size of command buffer.
  1132. *
  1133. * The command execution status is reported by the response.
  1134. *
  1135. * Return: 0 if command is valid, negative value otherwise.
  1136. */
  1137. static int pft_set_inplace_file(struct pft_command *command, int size)
  1138. {
  1139. int expected_size;
  1140. u32 fd;
  1141. int rc;
  1142. struct file *filp = NULL;
  1143. struct inode *inode = NULL;
  1144. int writecount;
  1145. expected_size = sizeof(command->opcode) +
  1146. sizeof(command->preform_in_place_file_enc.file_descriptor);
  1147. if (size != expected_size) {
  1148. pr_err("invalid command size %d expected %d.\n",
  1149. size, expected_size);
  1150. pft_set_response(PFT_CMD_RESP_INVALID_CMD_PARAMS);
  1151. return -EINVAL;
  1152. }
  1153. if (pft_dev->state != (u32) PFT_STATE_KEY_LOADED) {
  1154. pr_err("Key not loaded, state [%d], In-place-encryption is not allowed.\n",
  1155. pft_dev->state);
  1156. pft_set_response(PFT_CMD_RESP_GENERAL_ERROR);
  1157. return 0;
  1158. }
  1159. /* allow only one in-place file encryption at a time */
  1160. if (pft_dev->inplace_file != NULL) {
  1161. pr_err("file %s in-place-encryption in progress.\n",
  1162. file_to_filename(pft_dev->inplace_file));
  1163. /* @todo - use new error code */
  1164. pft_set_response(PFT_CMD_RESP_INPLACE_FILE_IS_OPEN);
  1165. return 0;
  1166. }
  1167. fd = command->preform_in_place_file_enc.file_descriptor;
  1168. filp = pft_get_process_open_file(fd);
  1169. if (filp == NULL) {
  1170. pr_err("failed to find file by fd %d.\n", fd);
  1171. pft_set_response(PFT_CMD_RESP_GENERAL_ERROR);
  1172. return 0;
  1173. }
  1174. /* Verify the file is not already open by other than PFM */
  1175. if (!filp->f_path.dentry || !filp->f_path.dentry->d_inode) {
  1176. pr_err("failed to get inode of inplace-file.\n");
  1177. pft_set_response(PFT_CMD_RESP_GENERAL_ERROR);
  1178. return 0;
  1179. }
  1180. inode = filp->f_path.dentry->d_inode;
  1181. writecount = atomic_read(&inode->i_writecount);
  1182. if (writecount > 1) {
  1183. pr_err("file %s is opened %d times for write.\n",
  1184. file_to_filename(filp), writecount);
  1185. pft_set_response(PFT_CMD_RESP_INPLACE_FILE_IS_OPEN);
  1186. return 0;
  1187. }
  1188. /*
  1189. * Check if the file was already encryprted.
  1190. * In practice, it is unlikely to happen,
  1191. * because PFM is not an enterprise application
  1192. * it won't be able to open encrypted file.
  1193. */
  1194. if (pft_is_encrypted_file(filp->f_dentry)) {
  1195. pr_err("file %s is already encrypted.\n",
  1196. file_to_filename(filp));
  1197. pft_set_response(PFT_CMD_RESP_GENERAL_ERROR);
  1198. return 0;
  1199. }
  1200. /* Update the current in-place-encryption file */
  1201. pft_dev->inplace_file = filp;
  1202. /*
  1203. * Now, any new access to this file is allowed only to PFM.
  1204. * Lets make sure that all pending writes are completed
  1205. * before encrypting the file.
  1206. */
  1207. pft_sync_file(filp);
  1208. rc = pft_tag_file(pft_dev->inplace_file->f_dentry,
  1209. pft_get_app_key_index(current_uid()));
  1210. if (!rc) {
  1211. pr_debug("tagged file %s to be encrypted.\n",
  1212. file_to_filename(pft_dev->inplace_file));
  1213. pft_set_response(PFT_CMD_RESP_SUCCESS);
  1214. } else {
  1215. pr_err("failed to tag file %s for encryption.\n",
  1216. file_to_filename(pft_dev->inplace_file));
  1217. pft_set_response(PFT_CMD_RESP_GENERAL_ERROR);
  1218. }
  1219. return 0;
  1220. }
  1221. /**
  1222. * pft_update_reg_apps() - Update the registered application
  1223. * list.
  1224. * @command: command buffer.
  1225. * @size: size of command buffer.
  1226. *
  1227. * The command execution status is reported by the response.
  1228. *
  1229. * Return: 0 on successful operation, negative value otherwise.
  1230. */
  1231. static int pft_update_reg_apps(struct pft_command *command, int size)
  1232. {
  1233. int i;
  1234. int expected_size;
  1235. void *buf;
  1236. int buf_size;
  1237. u32 items_count = command->update_app_list.items_count;
  1238. if (items_count > PFT_MAX_APPS) {
  1239. pr_err("Number of apps [%d] > max apps [%d]\n",
  1240. items_count , PFT_MAX_APPS);
  1241. pft_set_response(PFT_CMD_RESP_INVALID_CMD_PARAMS);
  1242. return -EINVAL;
  1243. }
  1244. expected_size =
  1245. sizeof(command->opcode) +
  1246. sizeof(command->update_app_list.items_count) +
  1247. (command->update_app_list.items_count * sizeof(u32));
  1248. if (size != expected_size) {
  1249. pr_err("invalid command size %d expected %d.\n",
  1250. size, expected_size);
  1251. pft_set_response(PFT_CMD_RESP_INVALID_CMD_PARAMS);
  1252. return -EINVAL;
  1253. }
  1254. mutex_lock(&pft_dev->lock);
  1255. /* Free old table */
  1256. kfree(pft_dev->uid_table);
  1257. pft_dev->uid_table = NULL;
  1258. pft_dev->uid_count = 0;
  1259. if (items_count == 0) {
  1260. pr_info("empty app list - clear list.\n");
  1261. mutex_unlock(&pft_dev->lock);
  1262. return 0;
  1263. }
  1264. buf_size = command->update_app_list.items_count * sizeof(u32);
  1265. buf = kzalloc(buf_size, GFP_KERNEL);
  1266. if (!buf) {
  1267. pr_err("malloc failure\n");
  1268. pft_set_response(PFT_CMD_RESP_GENERAL_ERROR);
  1269. mutex_unlock(&pft_dev->lock);
  1270. return 0;
  1271. }
  1272. pft_dev->uid_table = buf;
  1273. pft_dev->uid_count = command->update_app_list.items_count;
  1274. pr_debug("uid_count = %d\n", pft_dev->uid_count);
  1275. for (i = 0; i < pft_dev->uid_count; i++)
  1276. pft_dev->uid_table[i] = command->update_app_list.table[i];
  1277. pft_set_response(PFT_CMD_RESP_SUCCESS);
  1278. mutex_unlock(&pft_dev->lock);
  1279. return 0;
  1280. }
  1281. /**
  1282. * pft_handle_command() - Handle user space app commands.
  1283. * @buf: command buffer.
  1284. * @buf_size: command buffer size.
  1285. *
  1286. * Return: 0 on successful operation, negative value otherwise.
  1287. */
  1288. static int pft_handle_command(void *buf, int buf_size)
  1289. {
  1290. int ret = 0;
  1291. struct pft_command *command = NULL;
  1292. /* opcode field is the minimum length of command */
  1293. if (buf_size < sizeof(command->opcode)) {
  1294. pr_err("Invalid argument used buffer size\n");
  1295. return -EINVAL;
  1296. }
  1297. command = (struct pft_command *)buf;
  1298. pft_dev->response.command_id = command->opcode;
  1299. switch (command->opcode) {
  1300. case PFT_CMD_OPCODE_SET_STATE:
  1301. ret = pft_set_state(command, buf_size);
  1302. break;
  1303. case PFT_CMD_OPCODE_UPDATE_REG_APP_UID:
  1304. ret = pft_update_reg_apps(command, buf_size);
  1305. break;
  1306. case PFT_CMD_OPCODE_PERFORM_IN_PLACE_FILE_ENC:
  1307. ret = pft_set_inplace_file(command, buf_size);
  1308. break;
  1309. default:
  1310. pr_err("Invalid command_op_code %u\n", command->opcode);
  1311. pft_set_response(PFT_CMD_RESP_INVALID_COMMAND);
  1312. return 0;
  1313. }
  1314. return ret;
  1315. }
  1316. static int pft_device_open(struct inode *inode, struct file *file)
  1317. {
  1318. int ret;
  1319. mutex_lock(&pft_dev->lock);
  1320. if (pft_dev->open_count > 0) {
  1321. pr_err("PFT device is already opened (%d)\n",
  1322. pft_dev->open_count);
  1323. ret = -EBUSY;
  1324. } else {
  1325. pft_dev->open_count++;
  1326. pft_dev->pfm_pid = current_pid();
  1327. pr_debug("PFT device opened by %d (%d)\n",
  1328. pft_dev->pfm_pid, pft_dev->open_count);
  1329. ret = 0;
  1330. }
  1331. mutex_unlock(&pft_dev->lock);
  1332. pr_debug("device opened, count %d\n", pft_dev->open_count);
  1333. return ret;
  1334. }
  1335. static int pft_device_release(struct inode *inode, struct file *file)
  1336. {
  1337. mutex_lock(&pft_dev->lock);
  1338. if (pft_dev->open_count > 0)
  1339. pft_dev->open_count--;
  1340. pft_dev->pfm_pid = UINT_MAX;
  1341. mutex_unlock(&pft_dev->lock);
  1342. pr_debug("device released, count %d\n", pft_dev->open_count);
  1343. return 0;
  1344. }
  1345. /**
  1346. * pft_device_write() - Get commands from user sapce.
  1347. *
  1348. * Return: number of bytes to write on success to get the
  1349. * command buffer, negative value on failure.
  1350. * The error code for handling the command should be retrive by
  1351. * reading the response.
  1352. * Note: any reurn value of 0..size-1 will cause retry by the
  1353. * OS, so avoid it.
  1354. */
  1355. static ssize_t pft_device_write(struct file *filp, const char __user *user_buff,
  1356. size_t size, loff_t *f_pos)
  1357. {
  1358. int ret;
  1359. char *cmd_buf;
  1360. if (size > PFT_MAX_COMMAND_SIZE || !user_buff || !f_pos) {
  1361. pr_err("inavlid parameters.\n");
  1362. return -EINVAL;
  1363. }
  1364. cmd_buf = kzalloc(size, GFP_KERNEL);
  1365. if (cmd_buf == NULL) {
  1366. pr_err("malloc failure for command buffer\n");
  1367. return -ENOMEM;
  1368. }
  1369. ret = copy_from_user(cmd_buf, user_buff, size);
  1370. if (ret) {
  1371. pr_err("Unable to copy from user (err %d)\n", ret);
  1372. kfree(cmd_buf);
  1373. return -EFAULT;
  1374. }
  1375. ret = pft_handle_command(cmd_buf, size);
  1376. if (ret) {
  1377. kfree(cmd_buf);
  1378. return -EFAULT;
  1379. }
  1380. kfree(cmd_buf);
  1381. return size;
  1382. }
  1383. /**
  1384. * pft_device_read() - return response of last command.
  1385. *
  1386. * Return: number of bytes to read on success, negative value on
  1387. * failure.
  1388. */
  1389. static ssize_t pft_device_read(struct file *filp, char __user *buffer,
  1390. size_t length, loff_t *f_pos)
  1391. {
  1392. int ret = 0;
  1393. if (!buffer || !f_pos || length < sizeof(pft_dev->response)) {
  1394. pr_err("inavlid parameters.\n");
  1395. return -EFAULT;
  1396. }
  1397. ret = copy_to_user(buffer, &(pft_dev->response),
  1398. sizeof(pft_dev->response));
  1399. if (ret) {
  1400. pr_err("Unable to copy to user, err = %d.\n", ret);
  1401. return -EINVAL;
  1402. }
  1403. return sizeof(pft_dev->response);
  1404. }
  1405. static const struct file_operations fops = {
  1406. .owner = THIS_MODULE,
  1407. .read = pft_device_read,
  1408. .write = pft_device_write,
  1409. .open = pft_device_open,
  1410. .release = pft_device_release,
  1411. };
  1412. static int __init pft_register_chardev(void)
  1413. {
  1414. int rc;
  1415. unsigned baseminor = 0;
  1416. unsigned count = 1;
  1417. struct device *class_dev;
  1418. rc = alloc_chrdev_region(&pft_dev->device_no, baseminor, count,
  1419. DEVICE_NAME);
  1420. if (rc < 0) {
  1421. pr_err("alloc_chrdev_region failed %d\n", rc);
  1422. return rc;
  1423. }
  1424. pft_dev->driver_class = class_create(THIS_MODULE, DEVICE_NAME);
  1425. if (IS_ERR(pft_dev->driver_class)) {
  1426. rc = -ENOMEM;
  1427. pr_err("class_create failed %d\n", rc);
  1428. goto exit_unreg_chrdev_region;
  1429. }
  1430. class_dev = device_create(pft_dev->driver_class, NULL,
  1431. pft_dev->device_no, NULL,
  1432. DEVICE_NAME);
  1433. if (!class_dev) {
  1434. pr_err("class_device_create failed %d\n", rc);
  1435. rc = -ENOMEM;
  1436. goto exit_destroy_class;
  1437. }
  1438. cdev_init(&pft_dev->cdev, &fops);
  1439. pft_dev->cdev.owner = THIS_MODULE;
  1440. rc = cdev_add(&pft_dev->cdev, MKDEV(MAJOR(pft_dev->device_no), 0), 1);
  1441. if (rc < 0) {
  1442. pr_err("cdev_add failed %d\n", rc);
  1443. goto exit_destroy_device;
  1444. }
  1445. return 0;
  1446. exit_destroy_device:
  1447. device_destroy(pft_dev->driver_class, pft_dev->device_no);
  1448. exit_destroy_class:
  1449. class_destroy(pft_dev->driver_class);
  1450. exit_unreg_chrdev_region:
  1451. unregister_chrdev_region(pft_dev->device_no, 1);
  1452. return rc;
  1453. }
  1454. static void __exit pft_unregister_chrdev(void)
  1455. {
  1456. cdev_del(&pft_dev->cdev);
  1457. device_destroy(pft_dev->driver_class, pft_dev->device_no);
  1458. class_destroy(pft_dev->driver_class);
  1459. unregister_chrdev_region(pft_dev->device_no, 1);
  1460. }
  1461. static void __exit pft_free_open_files_list(void)
  1462. {
  1463. struct pft_file_info *tmp = NULL;
  1464. struct list_head *pos = NULL;
  1465. struct list_head *next = NULL;
  1466. mutex_lock(&pft_dev->lock);
  1467. list_for_each_safe(pos, next, &pft_dev->open_file_list) {
  1468. tmp = list_entry(pos, struct pft_file_info, list);
  1469. list_del(&tmp->list);
  1470. kfree(tmp);
  1471. }
  1472. mutex_unlock(&pft_dev->lock);
  1473. }
  1474. static void __exit pft_exit(void)
  1475. {
  1476. if (pft_dev == NULL)
  1477. return;
  1478. pft_unregister_chrdev();
  1479. pft_free_open_files_list();
  1480. kfree(pft_dev->uid_table);
  1481. kfree(pft_dev);
  1482. pft_dev = NULL;
  1483. }
  1484. static int __init pft_init(void)
  1485. {
  1486. int ret;
  1487. struct pft_device *dev = NULL;
  1488. dev = kzalloc(sizeof(struct pft_device), GFP_KERNEL);
  1489. if (dev == NULL) {
  1490. pr_err("No memory for device structr\n");
  1491. return -ENOMEM;
  1492. }
  1493. pft_dev = dev;
  1494. dev->state = PFT_STATE_DEACTIVATED;
  1495. dev->pfm_pid = UINT_MAX;
  1496. INIT_LIST_HEAD(&dev->open_file_list);
  1497. mutex_init(&dev->lock);
  1498. ret = pft_register_chardev();
  1499. if (ret) {
  1500. pr_err("create character device failed.\n");
  1501. goto fail;
  1502. }
  1503. pr_info("Drivr initialized successfully %s %s.n", __DATE__, __TIME__);
  1504. return 0;
  1505. fail:
  1506. pr_err("Failed to init driver.\n");
  1507. kfree(dev);
  1508. pft_dev = NULL;
  1509. return -ENODEV;
  1510. }
  1511. module_init(pft_init);
  1512. module_exit(pft_exit);
  1513. MODULE_LICENSE("GPL v2");
  1514. MODULE_DESCRIPTION("Per-File-Tagger driver");