xref: /linux/fs/ntfs3/namei.c (revision dc83d18cdd90482c70fa4320160bba70ec5c9ef8)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *
4  * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
5  *
6  */
7 
8 #include <linux/fs.h>
9 #include <linux/nls.h>
10 #include <linux/ctype.h>
11 #include <linux/posix_acl.h>
12 
13 #include "debug.h"
14 #include "ntfs.h"
15 #include "ntfs_fs.h"
16 
17 /*
18  * fill_name_de - Format NTFS_DE in @buf.
19  */
fill_name_de(struct ntfs_sb_info * sbi,void * buf,const struct qstr * name,const struct cpu_str * uni)20 int fill_name_de(struct ntfs_sb_info *sbi, void *buf, const struct qstr *name,
21 		 const struct cpu_str *uni)
22 {
23 	int err;
24 	struct NTFS_DE *e = buf;
25 	u16 data_size, real_size, aligned_size;
26 	struct ATTR_FILE_NAME *fname = (struct ATTR_FILE_NAME *)(e + 1);
27 
28 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
29 	e->ref.high = fname->home.high = 0;
30 #endif
31 	if (uni) {
32 #ifdef __BIG_ENDIAN
33 		int ulen = uni->len;
34 		__le16 *uname = fname->name;
35 		const u16 *name_cpu = uni->name;
36 
37 		while (ulen--)
38 			*uname++ = cpu_to_le16(*name_cpu++);
39 #else
40 		memcpy(fname->name, uni->name, uni->len * sizeof(u16));
41 #endif
42 		fname->name_len = uni->len;
43 
44 	} else {
45 		/* Convert input string to unicode. */
46 		err = ntfs_nls_to_utf16(sbi, name->name, name->len,
47 					(struct cpu_str *)&fname->name_len,
48 					NTFS_NAME_LEN, UTF16_LITTLE_ENDIAN);
49 		if (err < 0)
50 			return err;
51 	}
52 
53 	fname->type = FILE_NAME_POSIX;
54 	data_size = fname_full_size(fname);
55 
56 	real_size = data_size + sizeof(struct NTFS_DE);
57 	aligned_size = ALIGN(data_size, 8) + sizeof(struct NTFS_DE);
58 	if (aligned_size > real_size)
59 		memset((char *)buf + real_size, 0, aligned_size - real_size);
60 
61 	e->size = cpu_to_le16(aligned_size);
62 	e->key_size = cpu_to_le16(data_size);
63 	e->flags = 0;
64 	e->res = 0;
65 
66 	return 0;
67 }
68 
69 /*
70  * ntfs_lookup - inode_operations::lookup
71  */
ntfs_lookup(struct inode * dir,struct dentry * dentry,u32 flags)72 static struct dentry *ntfs_lookup(struct inode *dir, struct dentry *dentry,
73 				  u32 flags)
74 {
75 	struct ntfs_inode *ni = ntfs_i(dir);
76 	struct cpu_str *uni = kmalloc(PATH_MAX, GFP_KERNEL);
77 	struct inode *inode;
78 	int err;
79 
80 	if (!uni)
81 		return ERR_PTR(-ENOMEM);
82 
83 	err = ntfs_nls_to_utf16(ni->mi.sbi, dentry->d_name.name,
84 				dentry->d_name.len, uni, NTFS_NAME_LEN,
85 				UTF16_HOST_ENDIAN);
86 
87 	if (err < 0) {
88 		kfree(uni);
89 		return ERR_PTR(err);
90 	}
91 
92 	ni_lock_dir(ni);
93 	inode = dir_search_flags(dir, uni, NULL, flags);
94 	ni_unlock(ni);
95 
96 	kfree(uni);
97 
98 	/*
99 	 * Check for a null pointer
100 	 * If the MFT record of ntfs inode is not a base record, inode->i_op can be NULL.
101 	 * This causes null pointer dereference in d_splice_alias().
102 	 */
103 	if (!IS_ERR_OR_NULL(inode) && !inode->i_op) {
104 		iput(inode);
105 		return ERR_PTR(-EINVAL);
106 	}
107 
108 	return d_splice_alias(inode, dentry);
109 }
110 
111 /*
112  * ntfs_create - inode_operations::create
113  */
ntfs_create(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)114 static int ntfs_create(struct mnt_idmap *idmap, struct inode *dir,
115 		       struct dentry *dentry, umode_t mode)
116 {
117 	return ntfs_create_inode(idmap, dir, dentry, NULL, S_IFREG | mode, 0,
118 				 NULL, 0, NULL);
119 }
120 
121 /*
122  * ntfs_mknod - inode_operations::mknod
123  */
ntfs_mknod(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev)124 static int ntfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
125 		      struct dentry *dentry, umode_t mode, dev_t rdev)
126 {
127 	return ntfs_create_inode(idmap, dir, dentry, NULL, mode, rdev, NULL, 0,
128 				 NULL);
129 }
130 
131 /*
132  * ntfs_link - inode_operations::link
133  */
ntfs_link(struct dentry * ode,struct inode * dir,struct dentry * de)134 static int ntfs_link(struct dentry *ode, struct inode *dir, struct dentry *de)
135 {
136 	int err;
137 	struct inode *inode = d_inode(ode);
138 	struct ntfs_inode *ni = ntfs_i(inode);
139 
140 	if (S_ISDIR(inode->i_mode))
141 		return -EPERM;
142 
143 	if (inode->i_nlink >= NTFS_LINK_MAX)
144 		return -EMLINK;
145 
146 	ni_lock_dir(ntfs_i(dir));
147 	if (inode != dir)
148 		ni_lock(ni);
149 
150 	inc_nlink(inode);
151 	ihold(inode);
152 
153 	err = ntfs_link_inode(inode, de);
154 
155 	if (!err) {
156 		inode_set_ctime_current(inode);
157 		inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
158 		mark_inode_dirty(inode);
159 		mark_inode_dirty(dir);
160 		d_instantiate(de, inode);
161 	} else {
162 		drop_nlink(inode);
163 		iput(inode);
164 	}
165 
166 	if (inode != dir)
167 		ni_unlock(ni);
168 	ni_unlock(ntfs_i(dir));
169 
170 	return err;
171 }
172 
173 /*
174  * ntfs_unlink - inode_operations::unlink
175  */
ntfs_unlink(struct inode * dir,struct dentry * dentry)176 static int ntfs_unlink(struct inode *dir, struct dentry *dentry)
177 {
178 	struct ntfs_inode *dir_ni = ntfs_i(dir);
179 	struct inode *inode = d_inode(dentry);
180 	struct ntfs_inode *ni = ntfs_i(inode);
181 	int err;
182 
183 	/* Avoid any operation if inode is bad. */
184 	if (unlikely(is_bad_ni(ni)))
185 		return -EINVAL;
186 
187 	if (unlikely(ntfs3_forced_shutdown(dir->i_sb)))
188 		return -EIO;
189 
190 	if (likely(is_ni_base(ni))) {
191 		ni_lock_dir(dir_ni);
192 		/* Remove general file/dir. */
193 		err = ntfs_unlink_inode(dir, dentry);
194 		ni_unlock(dir_ni);
195 	} else {
196 		ni_lock(ni);
197 		/* Remove ADS. */
198 		err = ni_remove_attr(ni, ATTR_DATA, ni->file.ads.name,
199 				     ni->file.ads.len, false, NULL);
200 		ni_unlock(ni);
201 
202 		if (!err)
203 			drop_nlink(inode);
204 	}
205 
206 	return err;
207 }
208 
209 /*
210  * ntfs_symlink - inode_operations::symlink
211  */
ntfs_symlink(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,const char * symname)212 static int ntfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
213 			struct dentry *dentry, const char *symname)
214 {
215 	u32 size = strlen(symname);
216 
217 	/* Avoid any operation if inode is bad. */
218 	if (unlikely(is_bad_ni(ntfs_i(dir))))
219 		return -EINVAL;
220 
221 	if (unlikely(ntfs3_forced_shutdown(dir->i_sb)))
222 		return -EIO;
223 
224 	return ntfs_create_inode(idmap, dir, dentry, NULL, S_IFLNK | 0777, 0,
225 				 symname, size, NULL);
226 }
227 
228 /*
229  * ntfs_mkdir - inode_operations::mkdir
230  */
ntfs_mkdir(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)231 static struct dentry *ntfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
232 				 struct dentry *dentry, umode_t mode)
233 {
234 	return ERR_PTR(ntfs_create_inode(idmap, dir, dentry, NULL,
235 					 mode, 0, NULL, 0, NULL));
236 }
237 
238 /*
239  * ntfs_rmdir - inode_operations::rmdir
240  */
ntfs_rmdir(struct inode * dir,struct dentry * dentry)241 static int ntfs_rmdir(struct inode *dir, struct dentry *dentry)
242 {
243 	struct ntfs_inode *ni = ntfs_i(dir);
244 	int err;
245 
246 	/* Avoid any operation if inode is bad. */
247 	if (unlikely(is_bad_ni(ni)))
248 		return -EINVAL;
249 
250 	if (unlikely(ntfs3_forced_shutdown(dir->i_sb)))
251 		return -EIO;
252 
253 	ni_lock_dir(ni);
254 
255 	err = ntfs_unlink_inode(dir, dentry);
256 
257 	ni_unlock(ni);
258 
259 	return err;
260 }
261 
262 /*
263  * ntfs_rename - inode_operations::rename
264  */
ntfs_rename(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,struct inode * new_dir,struct dentry * new_dentry,u32 flags)265 static int ntfs_rename(struct mnt_idmap *idmap, struct inode *dir,
266 		       struct dentry *dentry, struct inode *new_dir,
267 		       struct dentry *new_dentry, u32 flags)
268 {
269 	int err;
270 	struct super_block *sb = dir->i_sb;
271 	struct ntfs_sb_info *sbi = sb->s_fs_info;
272 	struct ntfs_inode *dir_ni = ntfs_i(dir);
273 	struct ntfs_inode *new_dir_ni = ntfs_i(new_dir);
274 	struct inode *inode = d_inode(dentry);
275 	struct ntfs_inode *ni = ntfs_i(inode);
276 	struct inode *new_inode = d_inode(new_dentry);
277 	struct NTFS_DE *de, *new_de;
278 	bool is_same;
279 	/*
280 	 * de		- memory of PATH_MAX bytes:
281 	 * [0-1024)	- original name (dentry->d_name)
282 	 * [1024-2048)	- paired to original name, usually DOS variant of dentry->d_name
283 	 * [2048-3072)	- new name (new_dentry->d_name)
284 	 */
285 	static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + SIZEOF_RESIDENT < 1024);
286 	static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + sizeof(struct NTFS_DE) <
287 		      1024);
288 	static_assert(PATH_MAX >= 4 * 1024);
289 
290 	if (!is_ni_base(ni)) {
291 		/* No rename for ADS. */
292 		return -EOPNOTSUPP;
293 	}
294 
295 	/* Avoid any operation if inode is bad. */
296 	if (unlikely(is_bad_ni(ni)))
297 		return -EINVAL;
298 
299 	if (unlikely(ntfs3_forced_shutdown(sb)))
300 		return -EIO;
301 
302 	if (flags & ~RENAME_NOREPLACE)
303 		return -EINVAL;
304 
305 	is_same = dentry->d_name.len == new_dentry->d_name.len &&
306 		  !memcmp(dentry->d_name.name, new_dentry->d_name.name,
307 			  dentry->d_name.len);
308 
309 	if (is_same && dir == new_dir) {
310 		/* Nothing to do. */
311 		return 0;
312 	}
313 
314 	if (ntfs_is_meta_file(sbi, inode->i_ino)) {
315 		/* Should we print an error? */
316 		return -EINVAL;
317 	}
318 
319 	if (new_inode) {
320 		/* Target name exists. Unlink it. */
321 		dget(new_dentry);
322 		ni_lock_dir(new_dir_ni);
323 		err = ntfs_unlink_inode(new_dir, new_dentry);
324 		ni_unlock(new_dir_ni);
325 		dput(new_dentry);
326 		if (err)
327 			return err;
328 	}
329 
330 	de = kmalloc(PATH_MAX, GFP_KERNEL);
331 	if (!de)
332 		return -ENOMEM;
333 
334 	/* Translate dentry->d_name into unicode form. */
335 	err = fill_name_de(sbi, de, &dentry->d_name, NULL);
336 	if (err < 0)
337 		goto out;
338 
339 	if (is_same) {
340 		/* Reuse 'de'. */
341 		new_de = de;
342 	} else {
343 		/* Translate new_dentry->d_name into unicode form. */
344 		new_de = Add2Ptr(de, 2048);
345 		err = fill_name_de(sbi, new_de, &new_dentry->d_name, NULL);
346 		if (err < 0)
347 			goto out;
348 	}
349 
350 	ni_lock_dir(dir_ni);
351 	ni_lock(ni);
352 	if (dir_ni != new_dir_ni)
353 		ni_lock_dir2(new_dir_ni);
354 
355 	err = ni_rename(dir_ni, new_dir_ni, ni, de, new_de);
356 	if (!err) {
357 		simple_rename_timestamp(dir, dentry, new_dir, new_dentry);
358 		mark_inode_dirty(inode);
359 		mark_inode_dirty(dir);
360 		if (dir != new_dir)
361 			mark_inode_dirty(new_dir);
362 
363 		if (IS_DIRSYNC(dir))
364 			ntfs_sync_inode(dir);
365 
366 		if (IS_DIRSYNC(new_dir))
367 			ntfs_sync_inode(new_dir);
368 	}
369 
370 	if (dir_ni != new_dir_ni)
371 		ni_unlock(new_dir_ni);
372 	ni_unlock(ni);
373 	ni_unlock(dir_ni);
374 out:
375 	kfree(de);
376 	return err;
377 }
378 
ntfs3_get_parent(struct dentry * child)379 struct dentry *ntfs3_get_parent(struct dentry *child)
380 {
381 	struct inode *inode = d_inode(child);
382 	struct ntfs_inode *ni = ntfs_i(inode);
383 
384 	struct ATTR_LIST_ENTRY *le = NULL;
385 	struct ATTRIB *attr = NULL;
386 	struct ATTR_FILE_NAME *fname;
387 
388 	while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
389 				    NULL))) {
390 		fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
391 		if (!fname)
392 			continue;
393 
394 		return d_obtain_alias(
395 			ntfs_iget5(inode->i_sb, &fname->home, NULL));
396 	}
397 
398 	return ERR_PTR(-ENOENT);
399 }
400 
401 /*
402  * dentry_operations::d_hash
403  */
ntfs_d_hash(const struct dentry * dentry,struct qstr * name)404 static int ntfs_d_hash(const struct dentry *dentry, struct qstr *name)
405 {
406 	struct ntfs_sb_info *sbi;
407 	const char *n = name->name;
408 	unsigned int len = name->len;
409 	unsigned long hash;
410 	struct cpu_str *uni;
411 	unsigned int c;
412 	int err;
413 
414 	/* First try fast implementation. */
415 	hash = init_name_hash(dentry);
416 
417 	for (;;) {
418 		if (!len--) {
419 			name->hash = end_name_hash(hash);
420 			return 0;
421 		}
422 
423 		c = *n++;
424 		if (c >= 0x80)
425 			break;
426 
427 		hash = partial_name_hash(toupper(c), hash);
428 	}
429 
430 	/*
431 	 * Try slow way with current upcase table
432 	 */
433 	uni = kmalloc(PATH_MAX, GFP_NOWAIT);
434 	if (!uni)
435 		return -ENOMEM;
436 
437 	sbi = dentry->d_sb->s_fs_info;
438 
439 	err = ntfs_nls_to_utf16(sbi, name->name, name->len, uni, NTFS_NAME_LEN,
440 				UTF16_HOST_ENDIAN);
441 	if (err < 0)
442 		goto out;
443 
444 	if (!err) {
445 		err = -EINVAL;
446 		goto out;
447 	}
448 
449 	hash = ntfs_names_hash(uni->name, uni->len, sbi->upcase,
450 			       init_name_hash(dentry));
451 	name->hash = end_name_hash(hash);
452 	err = 0;
453 
454 out:
455 	kfree(uni);
456 	return err;
457 }
458 
459 /*
460  * dentry_operations::d_compare
461  */
ntfs_d_compare(const struct dentry * dentry,unsigned int len1,const char * str,const struct qstr * name)462 static int ntfs_d_compare(const struct dentry *dentry, unsigned int len1,
463 			  const char *str, const struct qstr *name)
464 {
465 	struct ntfs_sb_info *sbi;
466 	int ret;
467 	const char *n1 = str;
468 	const char *n2 = name->name;
469 	unsigned int len2 = name->len;
470 	unsigned int lm = min(len1, len2);
471 	unsigned char c1, c2;
472 	struct cpu_str *uni1;
473 	struct le_str *uni2;
474 
475 	/* First try fast implementation. */
476 	for (;;) {
477 		if (!lm--)
478 			return len1 != len2;
479 
480 		if ((c1 = *n1++) == (c2 = *n2++))
481 			continue;
482 
483 		if (c1 >= 0x80 || c2 >= 0x80)
484 			break;
485 
486 		if (toupper(c1) != toupper(c2))
487 			return 1;
488 	}
489 
490 	/*
491 	 * Try slow way with current upcase table
492 	 */
493 	sbi = dentry->d_sb->s_fs_info;
494 	uni1 = kmalloc(PATH_MAX, GFP_NOWAIT);
495 	if (!uni1)
496 		return -ENOMEM;
497 
498 	ret = ntfs_nls_to_utf16(sbi, str, len1, uni1, NTFS_NAME_LEN,
499 				UTF16_HOST_ENDIAN);
500 	if (ret < 0)
501 		goto out;
502 
503 	if (!ret) {
504 		ret = -EINVAL;
505 		goto out;
506 	}
507 
508 	uni2 = Add2Ptr(uni1, 2048);
509 
510 	ret = ntfs_nls_to_utf16(sbi, name->name, name->len,
511 				(struct cpu_str *)uni2, NTFS_NAME_LEN,
512 				UTF16_LITTLE_ENDIAN);
513 	if (ret < 0)
514 		goto out;
515 
516 	if (!ret) {
517 		ret = -EINVAL;
518 		goto out;
519 	}
520 
521 	ret = !ntfs_cmp_names_cpu(uni1, uni2, sbi->upcase, false) ? 0 : 1;
522 
523 out:
524 	kfree(uni1);
525 	return ret;
526 }
527 
528 // clang-format off
529 const struct inode_operations ntfs_dir_inode_operations = {
530 	.lookup		= ntfs_lookup,
531 	.create		= ntfs_create,
532 	.link		= ntfs_link,
533 	.unlink		= ntfs_unlink,
534 	.symlink	= ntfs_symlink,
535 	.mkdir		= ntfs_mkdir,
536 	.rmdir		= ntfs_rmdir,
537 	.mknod		= ntfs_mknod,
538 	.rename		= ntfs_rename,
539 	.get_acl	= ntfs_get_acl,
540 	.set_acl	= ntfs_set_acl,
541 	.setattr	= ntfs_setattr,
542 	.getattr	= ntfs_getattr,
543 	.listxattr	= ntfs_listxattr,
544 	.fiemap		= ntfs_fiemap,
545 	.fileattr_get	= ntfs_fileattr_get,
546 	.fileattr_set	= ntfs_fileattr_set,
547 };
548 
549 const struct inode_operations ntfs_special_inode_operations = {
550 	.setattr	= ntfs_setattr,
551 	.getattr	= ntfs_getattr,
552 	.listxattr	= ntfs_listxattr,
553 	.get_acl	= ntfs_get_acl,
554 	.set_acl	= ntfs_set_acl,
555 	.fileattr_get	= ntfs_fileattr_get,
556 	.fileattr_set	= ntfs_fileattr_set,
557 };
558 
559 const struct dentry_operations ntfs_dentry_ops = {
560 	.d_hash		= ntfs_d_hash,
561 	.d_compare	= ntfs_d_compare,
562 };
563 
564 // clang-format on
565