LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 01-18-2023, 12:47 PM   #1
deeps8us
LQ Newbie
 
Registered: Jan 2023
Posts: 1

Rep: Reputation: 0
File operation in kernel space leaves inode and dentry open


We have an usecase to do file operation in kernel code as part of security feature.
The functionality flow is filp_open, kernel_read, filp_close.
Even after filp_close, we see that the file inode dentry is left open. Is there a way to solve this ?
Please find the code snippet below:

Quote:
static int sample_test_fun(struct file *filp, void __user *_uarg, bool all_users)
{
struct sample_device_data_buffer buffer = {0};
struct file *fp;
loff_t pos = 0;
int status, i;

fp = filp_open("/opt/secure/test/secure_dir/data.txt", O_RDONLY|O_CLOEXEC, 0400);
if (IS_ERR(fp))
{
pr_err("failed to open %p %ld\n", fp, PTR_ERR(fp));
return PTR_ERR(fp);
}
printk("%s:%d:: file isize is from inode %lld\n",__FUNCTION__, __LINE__, (file_inode(fp))->i_size);
buffer.len = file_inode(fp)->i_size;
printk("%s: file size is %d \n", __FUNCTION__, buffer.len);
buffer.data = kzalloc(buffer.len, GFP_KERNEL);
if (!buffer.data)
{
printk("%s: allocation failed for size %d \n", __FUNCTION__, buffer.len);
filp_close(fp, NULL);
return -ENOMEM;
}
printk("%s:%d",__FUNCTION__, __LINE__);
status = kernel_read(fp, buffer.data, buffer.len, &pos);
printk("%s:%d read bytes %d",__FUNCTION__, __LINE__, status);
filp_close(fp, NULL);
if (status != buffer.len) {
if (status >= 0)
status = -EIO;
goto cleanup;
}
for (i=0; i < buffer.len;i++)
{
printk(" buffer data %c", buffer.data[i]);
}

}
Is this expected ? What is the correct way to solve this issue ?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Does the qstr struct in a kernel dentry hold the filename of a Linux file? dlevy022 Linux - Kernel 1 12-24-2015 07:54 AM
Linux inode/dentry cache exemptions? wraithman Linux - Kernel 1 08-03-2015 08:56 PM
Some questions about struct dentry involved with list ao.yuan.young Linux - Kernel 1 05-31-2009 08:08 PM
Some questions about struct dentry involved with list ao.yuan.young Linux - Kernel 1 05-31-2009 02:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 03:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration