Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2 | * linux/fs/ext4/symlink.c |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3 | * |
| 4 | * Only fast symlinks left here - the rest is done by generic code. AV, 1999 |
| 5 | * |
| 6 | * Copyright (C) 1992, 1993, 1994, 1995 |
| 7 | * Remy Card (card@masi.ibp.fr) |
| 8 | * Laboratoire MASI - Institut Blaise Pascal |
| 9 | * Universite Pierre et Marie Curie (Paris VI) |
| 10 | * |
| 11 | * from |
| 12 | * |
| 13 | * linux/fs/minix/symlink.c |
| 14 | * |
| 15 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 16 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 17 | * ext4 symlink handling code |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include <linux/fs.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 21 | #include <linux/namei.h> |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 22 | #include "ext4.h" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 23 | #include "xattr.h" |
| 24 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 25 | static const char *ext4_encrypted_get_link(struct dentry *dentry, |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 26 | struct inode *inode, |
| 27 | struct delayed_call *done) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 28 | { |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 29 | struct page *cpage = NULL; |
| 30 | char *caddr, *paddr = NULL; |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 31 | struct fscrypt_str cstr, pstr; |
| 32 | struct fscrypt_symlink_data *sd; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 33 | loff_t size = min_t(loff_t, i_size_read(inode), PAGE_SIZE - 1); |
| 34 | int res; |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 35 | u32 max_size = inode->i_sb->s_blocksize; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 36 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 37 | if (!dentry) |
| 38 | return ERR_PTR(-ECHILD); |
| 39 | |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 40 | res = fscrypt_get_encryption_info(inode); |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 41 | if (res) |
| 42 | return ERR_PTR(res); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 43 | |
| 44 | if (ext4_inode_is_fast_symlink(inode)) { |
Linus Torvalds | 9ec3a64 | 2015-04-26 15:48:49 -0700 | [diff] [blame] | 45 | caddr = (char *) EXT4_I(inode)->i_data; |
| 46 | max_size = sizeof(EXT4_I(inode)->i_data); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 47 | } else { |
| 48 | cpage = read_mapping_page(inode->i_mapping, 0, NULL); |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 49 | if (IS_ERR(cpage)) |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 50 | return ERR_CAST(cpage); |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 51 | caddr = page_address(cpage); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 52 | caddr[size] = 0; |
| 53 | } |
| 54 | |
| 55 | /* Symlink is encrypted */ |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 56 | sd = (struct fscrypt_symlink_data *)caddr; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 57 | cstr.name = sd->encrypted_path; |
Al Viro | 5a1c7f4 | 2015-11-26 15:20:50 -0500 | [diff] [blame] | 58 | cstr.len = le16_to_cpu(sd->len); |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 59 | if ((cstr.len + sizeof(struct fscrypt_symlink_data) - 1) > max_size) { |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 60 | /* Symlink data on the disk is corrupted */ |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 61 | res = -EFSCORRUPTED; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 62 | goto errout; |
| 63 | } |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 64 | |
| 65 | res = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr); |
| 66 | if (res) |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 67 | goto errout; |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 68 | |
| 69 | res = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 70 | if (res < 0) |
| 71 | goto errout; |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 72 | |
| 73 | paddr = pstr.name; |
| 74 | |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 75 | /* Null-terminate the name */ |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 76 | if (res <= pstr.len) |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 77 | paddr[res] = '\0'; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 78 | if (cpage) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 79 | put_page(cpage); |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 80 | set_delayed_call(done, kfree_link, paddr); |
| 81 | return paddr; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 82 | errout: |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 83 | if (cpage) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 84 | put_page(cpage); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 85 | kfree(paddr); |
| 86 | return ERR_PTR(res); |
| 87 | } |
| 88 | |
Al Viro | a7a67e8 | 2015-04-27 17:51:30 -0400 | [diff] [blame] | 89 | const struct inode_operations ext4_encrypted_symlink_inode_operations = { |
| 90 | .readlink = generic_readlink, |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 91 | .get_link = ext4_encrypted_get_link, |
Al Viro | a7a67e8 | 2015-04-27 17:51:30 -0400 | [diff] [blame] | 92 | .setattr = ext4_setattr, |
| 93 | .setxattr = generic_setxattr, |
| 94 | .getxattr = generic_getxattr, |
| 95 | .listxattr = ext4_listxattr, |
| 96 | .removexattr = generic_removexattr, |
| 97 | }; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 98 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 99 | const struct inode_operations ext4_symlink_inode_operations = { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 100 | .readlink = generic_readlink, |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 101 | .get_link = page_get_link, |
Dmitry Monakhov | 256a453 | 2010-05-16 02:00:00 -0400 | [diff] [blame] | 102 | .setattr = ext4_setattr, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 103 | .setxattr = generic_setxattr, |
| 104 | .getxattr = generic_getxattr, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 105 | .listxattr = ext4_listxattr, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 106 | .removexattr = generic_removexattr, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 109 | const struct inode_operations ext4_fast_symlink_inode_operations = { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 110 | .readlink = generic_readlink, |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 111 | .get_link = simple_get_link, |
Dmitry Monakhov | 256a453 | 2010-05-16 02:00:00 -0400 | [diff] [blame] | 112 | .setattr = ext4_setattr, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 113 | .setxattr = generic_setxattr, |
| 114 | .getxattr = generic_getxattr, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 115 | .listxattr = ext4_listxattr, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 116 | .removexattr = generic_removexattr, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 117 | }; |