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;
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  */
ntfs_lookup(struct inode * dir,struct dentry * dentry,u32 flags)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_dir(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  */
ntfs_create(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,bool excl)107 static int ntfs_create(struct mnt_idmap *idmap, struct inode *dir,
108 		       struct dentry *dentry, umode_t mode, bool excl)
109 {
110 	return ntfs_create_inode(idmap, dir, dentry, NULL, S_IFREG | mode, 0,
111 				 NULL, 0, NULL);
112 }
113 
114 /*
115  * ntfs_mknod - inode_operations::mknod
116  */
ntfs_mknod(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev)117 static int ntfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
118 		      struct dentry *dentry, umode_t mode, dev_t rdev)
119 {
120 	return ntfs_create_inode(idmap, dir, dentry, NULL, mode, rdev, NULL, 0,
121 				 NULL);
122 }
123 
124 /*
125  * ntfs_link - inode_operations::link
126  */
ntfs_link(struct dentry * ode,struct inode * dir,struct dentry * de)127 static int ntfs_link(struct dentry *ode, struct inode *dir, struct dentry *de)
128 {
129 	int err;
130 	struct inode *inode = d_inode(ode);
131 	struct ntfs_inode *ni = ntfs_i(inode);
132 
133 	if (S_ISDIR(inode->i_mode))
134 		return -EPERM;
135 
136 	if (inode->i_nlink >= NTFS_LINK_MAX)
137 		return -EMLINK;
138 
139 	ni_lock_dir(ntfs_i(dir));
140 	if (inode != dir)
141 		ni_lock(ni);
142 
143 	inc_nlink(inode);
144 	ihold(inode);
145 
146 	err = ntfs_link_inode(inode, de);
147 
148 	if (!err) {
149 		inode_set_ctime_current(inode);
150 		inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
151 		mark_inode_dirty(inode);
152 		mark_inode_dirty(dir);
153 		d_instantiate(de, inode);
154 	} else {
155 		drop_nlink(inode);
156 		iput(inode);
157 	}
158 
159 	if (inode != dir)
160 		ni_unlock(ni);
161 	ni_unlock(ntfs_i(dir));
162 
163 	return err;
164 }
165 
166 /*
167  * ntfs_unlink - inode_operations::unlink
168  */
ntfs_unlink(struct inode * dir,struct dentry * dentry)169 static int ntfs_unlink(struct inode *dir, struct dentry *dentry)
170 {
171 	struct ntfs_inode *ni = ntfs_i(dir);
172 	int err;
173 
174 	if (unlikely(ntfs3_forced_shutdown(dir->i_sb)))
175 		return -EIO;
176 
177 	ni_lock_dir(ni);
178 
179 	err = ntfs_unlink_inode(dir, dentry);
180 
181 	ni_unlock(ni);
182 
183 	return err;
184 }
185 
186 /*
187  * ntfs_symlink - inode_operations::symlink
188  */
ntfs_symlink(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,const char * symname)189 static int ntfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
190 			struct dentry *dentry, const char *symname)
191 {
192 	u32 size = strlen(symname);
193 
194 	if (unlikely(ntfs3_forced_shutdown(dir->i_sb)))
195 		return -EIO;
196 
197 	return ntfs_create_inode(idmap, dir, dentry, NULL, S_IFLNK | 0777, 0,
198 				 symname, size, NULL);
199 }
200 
201 /*
202  * ntfs_mkdir- inode_operations::mkdir
203  */
ntfs_mkdir(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)204 static int ntfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
205 		      struct dentry *dentry, umode_t mode)
206 {
207 	return ntfs_create_inode(idmap, dir, dentry, NULL, S_IFDIR | mode, 0,
208 				 NULL, 0, NULL);
209 }
210 
211 /*
212  * ntfs_rmdir - inode_operations::rmdir
213  */
ntfs_rmdir(struct inode * dir,struct dentry * dentry)214 static int ntfs_rmdir(struct inode *dir, struct dentry *dentry)
215 {
216 	struct ntfs_inode *ni = ntfs_i(dir);
217 	int err;
218 
219 	if (unlikely(ntfs3_forced_shutdown(dir->i_sb)))
220 		return -EIO;
221 
222 	ni_lock_dir(ni);
223 
224 	err = ntfs_unlink_inode(dir, dentry);
225 
226 	ni_unlock(ni);
227 
228 	return err;
229 }
230 
231 /*
232  * ntfs_rename - inode_operations::rename
233  */
ntfs_rename(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,struct inode * new_dir,struct dentry * new_dentry,u32 flags)234 static int ntfs_rename(struct mnt_idmap *idmap, struct inode *dir,
235 		       struct dentry *dentry, struct inode *new_dir,
236 		       struct dentry *new_dentry, u32 flags)
237 {
238 	int err;
239 	struct super_block *sb = dir->i_sb;
240 	struct ntfs_sb_info *sbi = sb->s_fs_info;
241 	struct ntfs_inode *dir_ni = ntfs_i(dir);
242 	struct ntfs_inode *new_dir_ni = ntfs_i(new_dir);
243 	struct inode *inode = d_inode(dentry);
244 	struct ntfs_inode *ni = ntfs_i(inode);
245 	struct inode *new_inode = d_inode(new_dentry);
246 	struct NTFS_DE *de, *new_de;
247 	bool is_same;
248 	/*
249 	 * de		- memory of PATH_MAX bytes:
250 	 * [0-1024)	- original name (dentry->d_name)
251 	 * [1024-2048)	- paired to original name, usually DOS variant of dentry->d_name
252 	 * [2048-3072)	- new name (new_dentry->d_name)
253 	 */
254 	static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + SIZEOF_RESIDENT < 1024);
255 	static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + sizeof(struct NTFS_DE) <
256 		      1024);
257 	static_assert(PATH_MAX >= 4 * 1024);
258 
259 	if (unlikely(ntfs3_forced_shutdown(sb)))
260 		return -EIO;
261 
262 	if (flags & ~RENAME_NOREPLACE)
263 		return -EINVAL;
264 
265 	is_same = dentry->d_name.len == new_dentry->d_name.len &&
266 		  !memcmp(dentry->d_name.name, new_dentry->d_name.name,
267 			  dentry->d_name.len);
268 
269 	if (is_same && dir == new_dir) {
270 		/* Nothing to do. */
271 		return 0;
272 	}
273 
274 	if (ntfs_is_meta_file(sbi, inode->i_ino)) {
275 		/* Should we print an error? */
276 		return -EINVAL;
277 	}
278 
279 	if (new_inode) {
280 		/* Target name exists. Unlink it. */
281 		dget(new_dentry);
282 		ni_lock_dir(new_dir_ni);
283 		err = ntfs_unlink_inode(new_dir, new_dentry);
284 		ni_unlock(new_dir_ni);
285 		dput(new_dentry);
286 		if (err)
287 			return err;
288 	}
289 
290 	/* Allocate PATH_MAX bytes. */
291 	de = __getname();
292 	if (!de)
293 		return -ENOMEM;
294 
295 	/* Translate dentry->d_name into unicode form. */
296 	err = fill_name_de(sbi, de, &dentry->d_name, NULL);
297 	if (err < 0)
298 		goto out;
299 
300 	if (is_same) {
301 		/* Reuse 'de'. */
302 		new_de = de;
303 	} else {
304 		/* Translate new_dentry->d_name into unicode form. */
305 		new_de = Add2Ptr(de, 2048);
306 		err = fill_name_de(sbi, new_de, &new_dentry->d_name, NULL);
307 		if (err < 0)
308 			goto out;
309 	}
310 
311 	ni_lock_dir(dir_ni);
312 	ni_lock(ni);
313 	if (dir_ni != new_dir_ni)
314 		ni_lock_dir2(new_dir_ni);
315 
316 	err = ni_rename(dir_ni, new_dir_ni, ni, de, new_de);
317 	if (!err) {
318 		simple_rename_timestamp(dir, dentry, new_dir, new_dentry);
319 		mark_inode_dirty(inode);
320 		mark_inode_dirty(dir);
321 		if (dir != new_dir)
322 			mark_inode_dirty(new_dir);
323 
324 		if (IS_DIRSYNC(dir))
325 			ntfs_sync_inode(dir);
326 
327 		if (IS_DIRSYNC(new_dir))
328 			ntfs_sync_inode(inode);
329 	}
330 
331 	if (dir_ni != new_dir_ni)
332 		ni_unlock(new_dir_ni);
333 	ni_unlock(ni);
334 	ni_unlock(dir_ni);
335 out:
336 	__putname(de);
337 	return err;
338 }
339 
ntfs3_get_parent(struct dentry * child)340 struct dentry *ntfs3_get_parent(struct dentry *child)
341 {
342 	struct inode *inode = d_inode(child);
343 	struct ntfs_inode *ni = ntfs_i(inode);
344 
345 	struct ATTR_LIST_ENTRY *le = NULL;
346 	struct ATTRIB *attr = NULL;
347 	struct ATTR_FILE_NAME *fname;
348 
349 	while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
350 				    NULL))) {
351 		fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
352 		if (!fname)
353 			continue;
354 
355 		return d_obtain_alias(
356 			ntfs_iget5(inode->i_sb, &fname->home, NULL));
357 	}
358 
359 	return ERR_PTR(-ENOENT);
360 }
361 
362 /*
363  * dentry_operations::d_hash
364  */
ntfs_d_hash(const struct dentry * dentry,struct qstr * name)365 static int ntfs_d_hash(const struct dentry *dentry, struct qstr *name)
366 {
367 	struct ntfs_sb_info *sbi;
368 	const char *n = name->name;
369 	unsigned int len = name->len;
370 	unsigned long hash;
371 	struct cpu_str *uni;
372 	unsigned int c;
373 	int err;
374 
375 	/* First try fast implementation. */
376 	hash = init_name_hash(dentry);
377 
378 	for (;;) {
379 		if (!len--) {
380 			name->hash = end_name_hash(hash);
381 			return 0;
382 		}
383 
384 		c = *n++;
385 		if (c >= 0x80)
386 			break;
387 
388 		hash = partial_name_hash(toupper(c), hash);
389 	}
390 
391 	/*
392 	 * Try slow way with current upcase table
393 	 */
394 	uni = kmem_cache_alloc(names_cachep, GFP_NOWAIT);
395 	if (!uni)
396 		return -ENOMEM;
397 
398 	sbi = dentry->d_sb->s_fs_info;
399 
400 	err = ntfs_nls_to_utf16(sbi, name->name, name->len, uni, NTFS_NAME_LEN,
401 				UTF16_HOST_ENDIAN);
402 	if (err < 0)
403 		goto out;
404 
405 	if (!err) {
406 		err = -EINVAL;
407 		goto out;
408 	}
409 
410 	hash = ntfs_names_hash(uni->name, uni->len, sbi->upcase,
411 			       init_name_hash(dentry));
412 	name->hash = end_name_hash(hash);
413 	err = 0;
414 
415 out:
416 	kmem_cache_free(names_cachep, uni);
417 	return err;
418 }
419 
420 /*
421  * dentry_operations::d_compare
422  */
ntfs_d_compare(const struct dentry * dentry,unsigned int len1,const char * str,const struct qstr * name)423 static int ntfs_d_compare(const struct dentry *dentry, unsigned int len1,
424 			  const char *str, const struct qstr *name)
425 {
426 	struct ntfs_sb_info *sbi;
427 	int ret;
428 	const char *n1 = str;
429 	const char *n2 = name->name;
430 	unsigned int len2 = name->len;
431 	unsigned int lm = min(len1, len2);
432 	unsigned char c1, c2;
433 	struct cpu_str *uni1;
434 	struct le_str *uni2;
435 
436 	/* First try fast implementation. */
437 	for (;;) {
438 		if (!lm--)
439 			return len1 != len2;
440 
441 		if ((c1 = *n1++) == (c2 = *n2++))
442 			continue;
443 
444 		if (c1 >= 0x80 || c2 >= 0x80)
445 			break;
446 
447 		if (toupper(c1) != toupper(c2))
448 			return 1;
449 	}
450 
451 	/*
452 	 * Try slow way with current upcase table
453 	 */
454 	sbi = dentry->d_sb->s_fs_info;
455 	uni1 = __getname();
456 	if (!uni1)
457 		return -ENOMEM;
458 
459 	ret = ntfs_nls_to_utf16(sbi, str, len1, uni1, NTFS_NAME_LEN,
460 				UTF16_HOST_ENDIAN);
461 	if (ret < 0)
462 		goto out;
463 
464 	if (!ret) {
465 		ret = -EINVAL;
466 		goto out;
467 	}
468 
469 	uni2 = Add2Ptr(uni1, 2048);
470 
471 	ret = ntfs_nls_to_utf16(sbi, name->name, name->len,
472 				(struct cpu_str *)uni2, NTFS_NAME_LEN,
473 				UTF16_LITTLE_ENDIAN);
474 	if (ret < 0)
475 		goto out;
476 
477 	if (!ret) {
478 		ret = -EINVAL;
479 		goto out;
480 	}
481 
482 	ret = !ntfs_cmp_names_cpu(uni1, uni2, sbi->upcase, false) ? 0 : 1;
483 
484 out:
485 	__putname(uni1);
486 	return ret;
487 }
488 
489 // clang-format off
490 const struct inode_operations ntfs_dir_inode_operations = {
491 	.lookup		= ntfs_lookup,
492 	.create		= ntfs_create,
493 	.link		= ntfs_link,
494 	.unlink		= ntfs_unlink,
495 	.symlink	= ntfs_symlink,
496 	.mkdir		= ntfs_mkdir,
497 	.rmdir		= ntfs_rmdir,
498 	.mknod		= ntfs_mknod,
499 	.rename		= ntfs_rename,
500 	.get_acl	= ntfs_get_acl,
501 	.set_acl	= ntfs_set_acl,
502 	.setattr	= ntfs_setattr,
503 	.getattr	= ntfs_getattr,
504 	.listxattr	= ntfs_listxattr,
505 	.fiemap		= ntfs_fiemap,
506 	.fileattr_get	= ntfs_fileattr_get,
507 	.fileattr_set	= ntfs_fileattr_set,
508 };
509 
510 const struct inode_operations ntfs_special_inode_operations = {
511 	.setattr	= ntfs_setattr,
512 	.getattr	= ntfs_getattr,
513 	.listxattr	= ntfs_listxattr,
514 	.get_acl	= ntfs_get_acl,
515 	.set_acl	= ntfs_set_acl,
516 };
517 
518 const struct dentry_operations ntfs_dentry_ops = {
519 	.d_hash		= ntfs_d_hash,
520 	.d_compare	= ntfs_d_compare,
521 };
522 
523 // clang-format on
524