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