xref: /linux/fs/udf/symlink.c (revision 4e9903b0861c9df3464b82db4a7025863bac1897)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * symlink.c
4  *
5  * PURPOSE
6  *	Symlink handling routines for the OSTA-UDF(tm) filesystem.
7  *
8  * COPYRIGHT
9  *  (C) 1998-2001 Ben Fennema
10  *  (C) 1999 Stelias Computing Inc
11  *
12  * HISTORY
13  *
14  *  04/16/99 blf  Created.
15  *
16  */
17 
18 #include "udfdecl.h"
19 #include <linux/uaccess.h>
20 #include <linux/errno.h>
21 #include <linux/fs.h>
22 #include <linux/time.h>
23 #include <linux/mm.h>
24 #include <linux/stat.h>
25 #include <linux/pagemap.h>
26 #include "udf_i.h"
27 
28 static int udf_pc_to_char(struct super_block *sb, unsigned char *from,
29 			  int fromlen, unsigned char *to, int tolen)
30 {
31 	struct pathComponent *pc;
32 	int elen = 0;
33 	int comp_len;
34 	unsigned char *p = to;
35 
36 	/* Reserve one byte for terminating \0 */
37 	tolen--;
38 	while (elen < fromlen) {
39 		pc = (struct pathComponent *)(from + elen);
40 		elen += sizeof(struct pathComponent);
41 		switch (pc->componentType) {
42 		case 1:
43 			/*
44 			 * Symlink points to some place which should be agreed
45  			 * upon between originator and receiver of the media. Ignore.
46 			 */
47 			if (pc->lengthComponentIdent > 0) {
48 				elen += pc->lengthComponentIdent;
49 				break;
50 			}
51 			fallthrough;
52 		case 2:
53 			if (tolen == 0)
54 				return -ENAMETOOLONG;
55 			p = to;
56 			*p++ = '/';
57 			tolen--;
58 			break;
59 		case 3:
60 			if (tolen < 3)
61 				return -ENAMETOOLONG;
62 			memcpy(p, "../", 3);
63 			p += 3;
64 			tolen -= 3;
65 			break;
66 		case 4:
67 			if (tolen < 2)
68 				return -ENAMETOOLONG;
69 			memcpy(p, "./", 2);
70 			p += 2;
71 			tolen -= 2;
72 			/* that would be . - just ignore */
73 			break;
74 		case 5:
75 			elen += pc->lengthComponentIdent;
76 			if (elen > fromlen)
77 				return -EIO;
78 			comp_len = udf_get_filename(sb, pc->componentIdent,
79 						    pc->lengthComponentIdent,
80 						    p, tolen);
81 			if (comp_len < 0)
82 				return comp_len;
83 
84 			p += comp_len;
85 			tolen -= comp_len;
86 			if (tolen == 0)
87 				return -ENAMETOOLONG;
88 			*p++ = '/';
89 			tolen--;
90 			break;
91 		}
92 	}
93 	if (p > to + 1)
94 		p[-1] = '\0';
95 	else
96 		p[0] = '\0';
97 	return 0;
98 }
99 
100 static int udf_symlink_filler(struct file *file, struct folio *folio)
101 {
102 	struct inode *inode = folio->mapping->host;
103 	struct buffer_head *bh = NULL;
104 	unsigned char *symlink;
105 	int err = 0;
106 	unsigned char *p = folio_address(folio);
107 	struct udf_inode_info *iinfo = UDF_I(inode);
108 
109 	/* We don't support symlinks longer than one block */
110 	if (inode->i_size > inode->i_sb->s_blocksize) {
111 		err = -ENAMETOOLONG;
112 		goto out;
113 	}
114 
115 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
116 		symlink = iinfo->i_data + iinfo->i_lenEAttr;
117 	} else {
118 		bh = udf_bread(inode, 0, 0, &err);
119 		if (!bh) {
120 			if (!err)
121 				err = -EFSCORRUPTED;
122 			goto out;
123 		}
124 		symlink = bh->b_data;
125 	}
126 
127 	err = udf_pc_to_char(inode->i_sb, symlink, inode->i_size, p, PAGE_SIZE);
128 	brelse(bh);
129 out:
130 	folio_end_read(folio, err == 0);
131 	return err;
132 }
133 
134 static int udf_symlink_getattr(struct mnt_idmap *idmap,
135 			       const struct path *path, struct kstat *stat,
136 			       u32 request_mask, unsigned int flags)
137 {
138 	struct dentry *dentry = path->dentry;
139 	struct inode *inode = d_backing_inode(dentry);
140 	struct folio *folio;
141 
142 	generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
143 	folio = read_mapping_folio(inode->i_mapping, 0, NULL);
144 	if (IS_ERR(folio))
145 		return PTR_ERR(folio);
146 	/*
147 	 * UDF uses non-trivial encoding of symlinks so i_size does not match
148 	 * number of characters reported by readlink(2) which apparently some
149 	 * applications expect. Also POSIX says that "The value returned in the
150 	 * st_size field shall be the length of the contents of the symbolic
151 	 * link, and shall not count a trailing null if one is present." So
152 	 * let's report the length of string returned by readlink(2) for
153 	 * st_size.
154 	 */
155 	stat->size = strlen(folio_address(folio));
156 	folio_put(folio);
157 
158 	return 0;
159 }
160 
161 /*
162  * symlinks can't do much...
163  */
164 const struct address_space_operations udf_symlink_aops = {
165 	.read_folio		= udf_symlink_filler,
166 };
167 
168 const struct inode_operations udf_symlink_inode_operations = {
169 	.get_link	= page_get_link,
170 	.getattr	= udf_symlink_getattr,
171 };
172