xref: /openbmc/linux/fs/ntfs3/namei.c (revision d6e2d652)
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  */
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;
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 	e->size = cpu_to_le16(ALIGN(data_size, 8) + sizeof(struct NTFS_DE));
57 	e->key_size = cpu_to_le16(data_size);
58 	e->flags = 0;
59 	e->res = 0;
60 
61 	return 0;
62 }
63 
64 /*
65  * ntfs_lookup - inode_operations::lookup
66  */
67 static struct dentry *ntfs_lookup(struct inode *dir, struct dentry *dentry,
68 				  u32 flags)
69 {
70 	struct ntfs_inode *ni = ntfs_i(dir);
71 	struct cpu_str *uni = __getname();
72 	struct inode *inode;
73 	int err;
74 
75 	if (!uni)
76 		inode = ERR_PTR(-ENOMEM);
77 	else {
78 		err = ntfs_nls_to_utf16(ni->mi.sbi, dentry->d_name.name,
79 					dentry->d_name.len, uni, NTFS_NAME_LEN,
80 					UTF16_HOST_ENDIAN);
81 		if (err < 0)
82 			inode = ERR_PTR(err);
83 		else {
84 			ni_lock(ni);
85 			inode = dir_search_u(dir, uni, NULL);
86 			ni_unlock(ni);
87 		}
88 		__putname(uni);
89 	}
90 
91 	/*
92 	 * Check for a null pointer
93 	 * If the MFT record of ntfs inode is not a base record, inode->i_op can be NULL.
94 	 * This causes null pointer dereference in d_splice_alias().
95 	 */
96 	if (!IS_ERR_OR_NULL(inode) && !inode->i_op) {
97 		iput(inode);
98 		inode = ERR_PTR(-EINVAL);
99 	}
100 
101 	return d_splice_alias(inode, dentry);
102 }
103 
104 /*
105  * ntfs_create - inode_operations::create
106  */
107 static int ntfs_create(struct mnt_idmap *idmap, struct inode *dir,
108 		       struct dentry *dentry, umode_t mode, bool excl)
109 {
110 	struct inode *inode;
111 
112 	inode = ntfs_create_inode(idmap, dir, dentry, NULL, S_IFREG | mode, 0,
113 				  NULL, 0, NULL);
114 
115 	return IS_ERR(inode) ? PTR_ERR(inode) : 0;
116 }
117 
118 /*
119  * ntfs_mknod
120  *
121  * inode_operations::mknod
122  */
123 static int ntfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
124 		      struct dentry *dentry, umode_t mode, dev_t rdev)
125 {
126 	struct inode *inode;
127 
128 	inode = ntfs_create_inode(idmap, dir, dentry, NULL, mode, rdev, NULL, 0,
129 				  NULL);
130 
131 	return IS_ERR(inode) ? PTR_ERR(inode) : 0;
132 }
133 
134 /*
135  * ntfs_link - inode_operations::link
136  */
137 static int ntfs_link(struct dentry *ode, struct inode *dir, struct dentry *de)
138 {
139 	int err;
140 	struct inode *inode = d_inode(ode);
141 	struct ntfs_inode *ni = ntfs_i(inode);
142 
143 	if (S_ISDIR(inode->i_mode))
144 		return -EPERM;
145 
146 	if (inode->i_nlink >= NTFS_LINK_MAX)
147 		return -EMLINK;
148 
149 	ni_lock_dir(ntfs_i(dir));
150 	if (inode != dir)
151 		ni_lock(ni);
152 
153 	inc_nlink(inode);
154 	ihold(inode);
155 
156 	err = ntfs_link_inode(inode, de);
157 
158 	if (!err) {
159 		dir->i_ctime = dir->i_mtime = inode->i_ctime =
160 			current_time(dir);
161 		mark_inode_dirty(inode);
162 		mark_inode_dirty(dir);
163 		d_instantiate(de, inode);
164 	} else {
165 		drop_nlink(inode);
166 		iput(inode);
167 	}
168 
169 	if (inode != dir)
170 		ni_unlock(ni);
171 	ni_unlock(ntfs_i(dir));
172 
173 	return err;
174 }
175 
176 /*
177  * ntfs_unlink - inode_operations::unlink
178  */
179 static int ntfs_unlink(struct inode *dir, struct dentry *dentry)
180 {
181 	struct ntfs_inode *ni = ntfs_i(dir);
182 	int err;
183 
184 	ni_lock_dir(ni);
185 
186 	err = ntfs_unlink_inode(dir, dentry);
187 
188 	ni_unlock(ni);
189 
190 	return err;
191 }
192 
193 /*
194  * ntfs_symlink - inode_operations::symlink
195  */
196 static int ntfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
197 			struct dentry *dentry, const char *symname)
198 {
199 	u32 size = strlen(symname);
200 	struct inode *inode;
201 
202 	inode = ntfs_create_inode(idmap, dir, dentry, NULL, S_IFLNK | 0777, 0,
203 				  symname, size, NULL);
204 
205 	return IS_ERR(inode) ? PTR_ERR(inode) : 0;
206 }
207 
208 /*
209  * ntfs_mkdir- inode_operations::mkdir
210  */
211 static int ntfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
212 		      struct dentry *dentry, umode_t mode)
213 {
214 	struct inode *inode;
215 
216 	inode = ntfs_create_inode(idmap, dir, dentry, NULL, S_IFDIR | mode, 0,
217 				  NULL, 0, NULL);
218 
219 	return IS_ERR(inode) ? PTR_ERR(inode) : 0;
220 }
221 
222 /*
223  * ntfs_rmdir - inode_operations::rmdir
224  */
225 static int ntfs_rmdir(struct inode *dir, struct dentry *dentry)
226 {
227 	struct ntfs_inode *ni = ntfs_i(dir);
228 	int err;
229 
230 	ni_lock_dir(ni);
231 
232 	err = ntfs_unlink_inode(dir, dentry);
233 
234 	ni_unlock(ni);
235 
236 	return err;
237 }
238 
239 /*
240  * ntfs_rename - inode_operations::rename
241  */
242 static int ntfs_rename(struct mnt_idmap *idmap, struct inode *dir,
243 		       struct dentry *dentry, struct inode *new_dir,
244 		       struct dentry *new_dentry, u32 flags)
245 {
246 	int err;
247 	struct super_block *sb = dir->i_sb;
248 	struct ntfs_sb_info *sbi = sb->s_fs_info;
249 	struct ntfs_inode *dir_ni = ntfs_i(dir);
250 	struct ntfs_inode *new_dir_ni = ntfs_i(new_dir);
251 	struct inode *inode = d_inode(dentry);
252 	struct ntfs_inode *ni = ntfs_i(inode);
253 	struct inode *new_inode = d_inode(new_dentry);
254 	struct NTFS_DE *de, *new_de;
255 	bool is_same, is_bad;
256 	/*
257 	 * de		- memory of PATH_MAX bytes:
258 	 * [0-1024)	- original name (dentry->d_name)
259 	 * [1024-2048)	- paired to original name, usually DOS variant of dentry->d_name
260 	 * [2048-3072)	- new name (new_dentry->d_name)
261 	 */
262 	static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + SIZEOF_RESIDENT < 1024);
263 	static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + sizeof(struct NTFS_DE) <
264 		      1024);
265 	static_assert(PATH_MAX >= 4 * 1024);
266 
267 	if (flags & ~RENAME_NOREPLACE)
268 		return -EINVAL;
269 
270 	is_same = dentry->d_name.len == new_dentry->d_name.len &&
271 		  !memcmp(dentry->d_name.name, new_dentry->d_name.name,
272 			  dentry->d_name.len);
273 
274 	if (is_same && dir == new_dir) {
275 		/* Nothing to do. */
276 		return 0;
277 	}
278 
279 	if (ntfs_is_meta_file(sbi, inode->i_ino)) {
280 		/* Should we print an error? */
281 		return -EINVAL;
282 	}
283 
284 	if (new_inode) {
285 		/* Target name exists. Unlink it. */
286 		dget(new_dentry);
287 		ni_lock_dir(new_dir_ni);
288 		err = ntfs_unlink_inode(new_dir, new_dentry);
289 		ni_unlock(new_dir_ni);
290 		dput(new_dentry);
291 		if (err)
292 			return err;
293 	}
294 
295 	/* Allocate PATH_MAX bytes. */
296 	de = __getname();
297 	if (!de)
298 		return -ENOMEM;
299 
300 	/* Translate dentry->d_name into unicode form. */
301 	err = fill_name_de(sbi, de, &dentry->d_name, NULL);
302 	if (err < 0)
303 		goto out;
304 
305 	if (is_same) {
306 		/* Reuse 'de'. */
307 		new_de = de;
308 	} else {
309 		/* Translate new_dentry->d_name into unicode form. */
310 		new_de = Add2Ptr(de, 2048);
311 		err = fill_name_de(sbi, new_de, &new_dentry->d_name, NULL);
312 		if (err < 0)
313 			goto out;
314 	}
315 
316 	ni_lock_dir(dir_ni);
317 	ni_lock(ni);
318 	if (dir_ni != new_dir_ni)
319 		ni_lock_dir2(new_dir_ni);
320 
321 	is_bad = false;
322 	err = ni_rename(dir_ni, new_dir_ni, ni, de, new_de, &is_bad);
323 	if (is_bad) {
324 		/* Restore after failed rename failed too. */
325 		_ntfs_bad_inode(inode);
326 	} else if (!err) {
327 		inode->i_ctime = dir->i_ctime = dir->i_mtime =
328 			current_time(dir);
329 		mark_inode_dirty(inode);
330 		mark_inode_dirty(dir);
331 		if (dir != new_dir) {
332 			new_dir->i_mtime = new_dir->i_ctime = dir->i_ctime;
333 			mark_inode_dirty(new_dir);
334 		}
335 
336 		if (IS_DIRSYNC(dir))
337 			ntfs_sync_inode(dir);
338 
339 		if (IS_DIRSYNC(new_dir))
340 			ntfs_sync_inode(inode);
341 	}
342 
343 	if (dir_ni != new_dir_ni)
344 		ni_unlock(new_dir_ni);
345 	ni_unlock(ni);
346 	ni_unlock(dir_ni);
347 out:
348 	__putname(de);
349 	return err;
350 }
351 
352 /*
353  * ntfs_atomic_open
354  *
355  * inode_operations::atomic_open
356  */
357 static int ntfs_atomic_open(struct inode *dir, struct dentry *dentry,
358 			    struct file *file, u32 flags, umode_t mode)
359 {
360 	int err;
361 	struct inode *inode;
362 	struct ntfs_fnd *fnd = NULL;
363 	struct ntfs_inode *ni = ntfs_i(dir);
364 	struct dentry *d = NULL;
365 	struct cpu_str *uni = __getname();
366 	bool locked = false;
367 
368 	if (!uni)
369 		return -ENOMEM;
370 
371 	err = ntfs_nls_to_utf16(ni->mi.sbi, dentry->d_name.name,
372 				dentry->d_name.len, uni, NTFS_NAME_LEN,
373 				UTF16_HOST_ENDIAN);
374 	if (err < 0)
375 		goto out;
376 
377 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
378 	if (IS_POSIXACL(dir)) {
379 		/*
380 		 * Load in cache current acl to avoid ni_lock(dir):
381 		 * ntfs_create_inode -> ntfs_init_acl -> posix_acl_create ->
382 		 * ntfs_get_acl -> ntfs_get_acl_ex -> ni_lock
383 		 */
384 		struct posix_acl *p = get_inode_acl(dir, ACL_TYPE_DEFAULT);
385 
386 		if (IS_ERR(p)) {
387 			err = PTR_ERR(p);
388 			goto out;
389 		}
390 		posix_acl_release(p);
391 	}
392 #endif
393 
394 	if (d_in_lookup(dentry)) {
395 		ni_lock_dir(ni);
396 		locked = true;
397 		fnd = fnd_get();
398 		if (!fnd) {
399 			err = -ENOMEM;
400 			goto out1;
401 		}
402 
403 		d = d_splice_alias(dir_search_u(dir, uni, fnd), dentry);
404 		if (IS_ERR(d)) {
405 			err = PTR_ERR(d);
406 			d = NULL;
407 			goto out2;
408 		}
409 
410 		if (d)
411 			dentry = d;
412 	}
413 
414 	if (!(flags & O_CREAT) || d_really_is_positive(dentry)) {
415 		err = finish_no_open(file, d);
416 		goto out2;
417 	}
418 
419 	file->f_mode |= FMODE_CREATED;
420 
421 	/*
422 	 * fnd contains tree's path to insert to.
423 	 * If fnd is not NULL then dir is locked.
424 	 */
425 	inode = ntfs_create_inode(mnt_idmap(file->f_path.mnt), dir, dentry, uni,
426 				  mode, 0, NULL, 0, fnd);
427 	err = IS_ERR(inode) ? PTR_ERR(inode) :
428 			      finish_open(file, dentry, ntfs_file_open);
429 	dput(d);
430 
431 out2:
432 	fnd_put(fnd);
433 out1:
434 	if (locked)
435 		ni_unlock(ni);
436 out:
437 	__putname(uni);
438 	return err;
439 }
440 
441 struct dentry *ntfs3_get_parent(struct dentry *child)
442 {
443 	struct inode *inode = d_inode(child);
444 	struct ntfs_inode *ni = ntfs_i(inode);
445 
446 	struct ATTR_LIST_ENTRY *le = NULL;
447 	struct ATTRIB *attr = NULL;
448 	struct ATTR_FILE_NAME *fname;
449 
450 	while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
451 				    NULL))) {
452 		fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
453 		if (!fname)
454 			continue;
455 
456 		return d_obtain_alias(
457 			ntfs_iget5(inode->i_sb, &fname->home, NULL));
458 	}
459 
460 	return ERR_PTR(-ENOENT);
461 }
462 
463 /*
464  * dentry_operations::d_hash
465  */
466 static int ntfs_d_hash(const struct dentry *dentry, struct qstr *name)
467 {
468 	struct ntfs_sb_info *sbi;
469 	const char *n = name->name;
470 	unsigned int len = name->len;
471 	unsigned long hash;
472 	struct cpu_str *uni;
473 	unsigned int c;
474 	int err;
475 
476 	/* First try fast implementation. */
477 	hash = init_name_hash(dentry);
478 
479 	for (;;) {
480 		if (!len--) {
481 			name->hash = end_name_hash(hash);
482 			return 0;
483 		}
484 
485 		c = *n++;
486 		if (c >= 0x80)
487 			break;
488 
489 		hash = partial_name_hash(toupper(c), hash);
490 	}
491 
492 	/*
493 	 * Try slow way with current upcase table
494 	 */
495 	uni = __getname();
496 	if (!uni)
497 		return -ENOMEM;
498 
499 	sbi = dentry->d_sb->s_fs_info;
500 
501 	err = ntfs_nls_to_utf16(sbi, name->name, name->len, uni, NTFS_NAME_LEN,
502 				UTF16_HOST_ENDIAN);
503 	if (err < 0)
504 		goto out;
505 
506 	if (!err) {
507 		err = -EINVAL;
508 		goto out;
509 	}
510 
511 	hash = ntfs_names_hash(uni->name, uni->len, sbi->upcase,
512 			       init_name_hash(dentry));
513 	name->hash = end_name_hash(hash);
514 	err = 0;
515 
516 out:
517 	__putname(uni);
518 	return err;
519 }
520 
521 /*
522  * dentry_operations::d_compare
523  */
524 static int ntfs_d_compare(const struct dentry *dentry, unsigned int len1,
525 			  const char *str, const struct qstr *name)
526 {
527 	struct ntfs_sb_info *sbi;
528 	int ret;
529 	const char *n1 = str;
530 	const char *n2 = name->name;
531 	unsigned int len2 = name->len;
532 	unsigned int lm = min(len1, len2);
533 	unsigned char c1, c2;
534 	struct cpu_str *uni1;
535 	struct le_str *uni2;
536 
537 	/* First try fast implementation. */
538 	for (;;) {
539 		if (!lm--)
540 			return len1 != len2;
541 
542 		if ((c1 = *n1++) == (c2 = *n2++))
543 			continue;
544 
545 		if (c1 >= 0x80 || c2 >= 0x80)
546 			break;
547 
548 		if (toupper(c1) != toupper(c2))
549 			return 1;
550 	}
551 
552 	/*
553 	 * Try slow way with current upcase table
554 	 */
555 	sbi = dentry->d_sb->s_fs_info;
556 	uni1 = __getname();
557 	if (!uni1)
558 		return -ENOMEM;
559 
560 	ret = ntfs_nls_to_utf16(sbi, str, len1, uni1, NTFS_NAME_LEN,
561 				UTF16_HOST_ENDIAN);
562 	if (ret < 0)
563 		goto out;
564 
565 	if (!ret) {
566 		ret = -EINVAL;
567 		goto out;
568 	}
569 
570 	uni2 = Add2Ptr(uni1, 2048);
571 
572 	ret = ntfs_nls_to_utf16(sbi, name->name, name->len,
573 				(struct cpu_str *)uni2, NTFS_NAME_LEN,
574 				UTF16_LITTLE_ENDIAN);
575 	if (ret < 0)
576 		goto out;
577 
578 	if (!ret) {
579 		ret = -EINVAL;
580 		goto out;
581 	}
582 
583 	ret = !ntfs_cmp_names_cpu(uni1, uni2, sbi->upcase, false) ? 0 : 1;
584 
585 out:
586 	__putname(uni1);
587 	return ret;
588 }
589 
590 // clang-format off
591 const struct inode_operations ntfs_dir_inode_operations = {
592 	.lookup		= ntfs_lookup,
593 	.create		= ntfs_create,
594 	.link		= ntfs_link,
595 	.unlink		= ntfs_unlink,
596 	.symlink	= ntfs_symlink,
597 	.mkdir		= ntfs_mkdir,
598 	.rmdir		= ntfs_rmdir,
599 	.mknod		= ntfs_mknod,
600 	.rename		= ntfs_rename,
601 	.get_acl	= ntfs_get_acl,
602 	.set_acl	= ntfs_set_acl,
603 	.setattr	= ntfs3_setattr,
604 	.getattr	= ntfs_getattr,
605 	.listxattr	= ntfs_listxattr,
606 	.atomic_open	= ntfs_atomic_open,
607 	.fiemap		= ntfs_fiemap,
608 };
609 
610 const struct inode_operations ntfs_special_inode_operations = {
611 	.setattr	= ntfs3_setattr,
612 	.getattr	= ntfs_getattr,
613 	.listxattr	= ntfs_listxattr,
614 	.get_acl	= ntfs_get_acl,
615 	.set_acl	= ntfs_set_acl,
616 };
617 
618 const struct dentry_operations ntfs_dentry_ops = {
619 	.d_hash		= ntfs_d_hash,
620 	.d_compare	= ntfs_d_compare,
621 };
622 
623 // clang-format on
624