Linux Kernel: Should I Free the Pointer Returned by D_Path?

As is mentioned in , 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 ):

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?

3
Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article