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 | int res; |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 34 | u32 max_size = inode->i_sb->s_blocksize; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 35 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 36 | if (!dentry) |
| 37 | return ERR_PTR(-ECHILD); |
| 38 | |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 39 | res = fscrypt_get_encryption_info(inode); |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 40 | if (res) |
| 41 | return ERR_PTR(res); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 42 | |
| 43 | if (ext4_inode_is_fast_symlink(inode)) { |
Linus Torvalds | 9ec3a64 | 2015-04-26 15:48:49 -0700 | [diff] [blame] | 44 | caddr = (char *) EXT4_I(inode)->i_data; |
| 45 | max_size = sizeof(EXT4_I(inode)->i_data); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 46 | } else { |
| 47 | cpage = read_mapping_page(inode->i_mapping, 0, NULL); |
Theodore Ts'o | b7236e2 | 2015-05-18 13:17:47 -0400 | [diff] [blame] | 48 | if (IS_ERR(cpage)) |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 49 | return ERR_CAST(cpage); |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 50 | caddr = page_address(cpage); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /* Symlink is encrypted */ |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 54 | sd = (struct fscrypt_symlink_data *)caddr; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 55 | cstr.name = sd->encrypted_path; |
Al Viro | 5a1c7f4 | 2015-11-26 15:20:50 -0500 | [diff] [blame] | 56 | cstr.len = le16_to_cpu(sd->len); |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 57 | if ((cstr.len + sizeof(struct fscrypt_symlink_data) - 1) > max_size) { |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 58 | /* Symlink data on the disk is corrupted */ |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 59 | res = -EFSCORRUPTED; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 60 | goto errout; |
| 61 | } |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 62 | |
| 63 | res = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr); |
| 64 | if (res) |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 65 | goto errout; |
Eric Biggers | dcce7a4 | 2016-09-15 13:13:13 -0400 | [diff] [blame] | 66 | paddr = pstr.name; |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 67 | |
| 68 | res = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr); |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 69 | if (res) |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 70 | goto errout; |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 71 | |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 72 | /* Null-terminate the name */ |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 73 | paddr[pstr.len] = '\0'; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 74 | if (cpage) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 75 | put_page(cpage); |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 76 | set_delayed_call(done, kfree_link, paddr); |
| 77 | return paddr; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 78 | errout: |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 79 | if (cpage) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 80 | put_page(cpage); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 81 | kfree(paddr); |
| 82 | return ERR_PTR(res); |
| 83 | } |
| 84 | |
Al Viro | a7a67e8 | 2015-04-27 17:51:30 -0400 | [diff] [blame] | 85 | const struct inode_operations ext4_encrypted_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 86 | .get_link = ext4_encrypted_get_link, |
Al Viro | a7a67e8 | 2015-04-27 17:51:30 -0400 | [diff] [blame] | 87 | .setattr = ext4_setattr, |
David Howells | 99652ea | 2017-03-31 18:31:56 +0100 | [diff] [blame] | 88 | .getattr = ext4_getattr, |
Al Viro | a7a67e8 | 2015-04-27 17:51:30 -0400 | [diff] [blame] | 89 | .listxattr = ext4_listxattr, |
Al Viro | a7a67e8 | 2015-04-27 17:51:30 -0400 | [diff] [blame] | 90 | }; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 91 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 92 | const struct inode_operations ext4_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 93 | .get_link = page_get_link, |
Dmitry Monakhov | 256a453 | 2010-05-16 02:00:00 -0400 | [diff] [blame] | 94 | .setattr = ext4_setattr, |
David Howells | 99652ea | 2017-03-31 18:31:56 +0100 | [diff] [blame] | 95 | .getattr = ext4_getattr, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 96 | .listxattr = ext4_listxattr, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 99 | const struct inode_operations ext4_fast_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 100 | .get_link = simple_get_link, |
Dmitry Monakhov | 256a453 | 2010-05-16 02:00:00 -0400 | [diff] [blame] | 101 | .setattr = ext4_setattr, |
David Howells | 99652ea | 2017-03-31 18:31:56 +0100 | [diff] [blame] | 102 | .getattr = ext4_getattr, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 103 | .listxattr = ext4_listxattr, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 104 | }; |