As is mentioned in https://www.kernel.org/doc/htmldocs/filesystems/API-d-path.html, the function d_path
converts a dentry into an ASCII path name and callers should use the returned pointer.
And I'm trying to resolve the user space filename by the following code (credits to https://stackoverflow.com/a/61504960/10591104):
static inline char* resolve_path(const char __user *filename, int flag) {
struct path path;
int dfd = AT_FDCWD;
char *ret_ptr = NULL;
int error = -EINVAL;
unsigned int lookup_flags = 0;
char *tpath = kmalloc(1024, GFP_KERNEL);
if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT)) != 0) {
goto out;
}
if (!(flag & AT_SYMLINK_NOFOLLOW)) {
lookup_flags |= LOOKUP_FOLLOW;
}
error = user_path_at(dfd, filename, lookup_flags, &path);
if (error) {
goto out;
}
ret_ptr = d_path(&path, tpath, 1024);
if (IS_ERR(ret_ptr)) {
goto out;
}
kfree(tpath);
return ret_ptr;
out:
kfree(tpath);
return NULL;
}
My question is, should I free the returned pointer by d_path
(i.e. the ret_ptr
in the code)? Where is the pointed memory stored? It causes error when I kfree(ret_ptr)
, so I'm assuming there's no need to free it by myself?