Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3 | * linux/fs/ext4/symlink.c |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4 | * |
| 5 | * Only fast symlinks left here - the rest is done by generic code. AV, 1999 |
| 6 | * |
| 7 | * Copyright (C) 1992, 1993, 1994, 1995 |
| 8 | * Remy Card (card@masi.ibp.fr) |
| 9 | * Laboratoire MASI - Institut Blaise Pascal |
| 10 | * Universite Pierre et Marie Curie (Paris VI) |
| 11 | * |
| 12 | * from |
| 13 | * |
| 14 | * linux/fs/minix/symlink.c |
| 15 | * |
| 16 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 17 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 18 | * ext4 symlink handling code |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include <linux/fs.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 22 | #include <linux/namei.h> |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 23 | #include "ext4.h" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 24 | #include "xattr.h" |
| 25 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 26 | static const char *ext4_encrypted_get_link(struct dentry *dentry, |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 27 | struct inode *inode, |
| 28 | struct delayed_call *done) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 29 | { |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 30 | struct page *cpage = NULL; |
| 31 | char *caddr, *paddr = NULL; |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 32 | struct fscrypt_str cstr, pstr; |
| 33 | struct fscrypt_symlink_data *sd; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 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 | } |
| 53 | |
| 54 | /* Symlink is encrypted */ |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 55 | sd = (struct fscrypt_symlink_data *)caddr; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 56 | cstr.name = sd->encrypted_path; |
Al Viro | 5a1c7f4 | 2015-11-26 15:20:50 -0500 | [diff] [blame] | 57 | cstr.len = le16_to_cpu(sd->len); |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 58 | if ((cstr.len + sizeof(struct fscrypt_symlink_data) - 1) > max_size) { |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 59 | /* Symlink data on the disk is corrupted */ |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 60 | res = -EFSCORRUPTED; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 61 | goto errout; |
| 62 | } |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 63 | |
| 64 | res = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr); |
| 65 | if (res) |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 66 | goto errout; |
Eric Biggers | dcce7a4 | 2016-09-15 13:13:13 -0400 | [diff] [blame] | 67 | paddr = pstr.name; |
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); |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 70 | if (res) |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 71 | goto errout; |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 72 | |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 73 | /* Null-terminate the name */ |
Eric Biggers | ef1eb3a | 2016-09-15 17:25:55 -0400 | [diff] [blame] | 74 | paddr[pstr.len] = '\0'; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 75 | if (cpage) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 76 | put_page(cpage); |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 77 | set_delayed_call(done, kfree_link, paddr); |
| 78 | return paddr; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 79 | errout: |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 80 | if (cpage) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 81 | put_page(cpage); |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 82 | kfree(paddr); |
| 83 | return ERR_PTR(res); |
| 84 | } |
| 85 | |
Al Viro | a7a67e8 | 2015-04-27 17:51:30 -0400 | [diff] [blame] | 86 | const struct inode_operations ext4_encrypted_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 87 | .get_link = ext4_encrypted_get_link, |
Al Viro | a7a67e8 | 2015-04-27 17:51:30 -0400 | [diff] [blame] | 88 | .setattr = ext4_setattr, |
David Howells | 99652ea | 2017-03-31 18:31:56 +0100 | [diff] [blame] | 89 | .getattr = ext4_getattr, |
Al Viro | a7a67e8 | 2015-04-27 17:51:30 -0400 | [diff] [blame] | 90 | .listxattr = ext4_listxattr, |
Al Viro | a7a67e8 | 2015-04-27 17:51:30 -0400 | [diff] [blame] | 91 | }; |
Theodore Ts'o | f348c25 | 2015-04-16 01:55:00 -0400 | [diff] [blame] | 92 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 93 | const struct inode_operations ext4_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 94 | .get_link = page_get_link, |
Dmitry Monakhov | 256a453 | 2010-05-16 02:00:00 -0400 | [diff] [blame] | 95 | .setattr = ext4_setattr, |
David Howells | 99652ea | 2017-03-31 18:31:56 +0100 | [diff] [blame] | 96 | .getattr = ext4_getattr, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 97 | .listxattr = ext4_listxattr, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 100 | const struct inode_operations ext4_fast_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 101 | .get_link = simple_get_link, |
Dmitry Monakhov | 256a453 | 2010-05-16 02:00:00 -0400 | [diff] [blame] | 102 | .setattr = ext4_setattr, |
David Howells | 99652ea | 2017-03-31 18:31:56 +0100 | [diff] [blame] | 103 | .getattr = ext4_getattr, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 104 | .listxattr = ext4_listxattr, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 105 | }; |