Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
kernel_modules
View unanswered posts
View posts from last 24 hours

Goto page 1, 2  Next  
This topic is locked: you cannot edit posts or make replies.    Gentoo Forums Forum Index Kernel & Hardware
View previous topic :: View next topic  
Author Message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Thu Feb 29, 2024 6:27 pm    Post subject: kernel_modules Reply with quote

hello forum. there is a simple function from the user space
Code:
 void FOO(){printf("Hi forum");  };



there is a kernel module that I am writing
Code:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/kmod.h>

MODULE_LICENSE("GPL");

int init_module(void)
{

}

tell me, experts "C" , how to call a function FOO() from the user space in the kernel module?


Last edited by Gentoopc on Thu Feb 29, 2024 11:08 pm; edited 1 time in total
Back to top
View user's profile Send private message
gentoo_ram
Guru
Guru


Joined: 25 Oct 2007
Posts: 474
Location: San Diego, California USA

PostPosted: Thu Feb 29, 2024 6:42 pm    Post subject: Reply with quote

The short answer is that you can't do that from kernel context. At least not directly. Kernel context is not user context. The closest analogy will be the kernel helper concept. Sorta like how modules are loaded by the kernel. You should look into that mechanism.
Back to top
View user's profile Send private message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Thu Feb 29, 2024 6:53 pm    Post subject: Reply with quote

gentoo_ram wrote:
The short answer is that you can't do that from kernel context. At least not directly. Kernel context is not user context. The closest analogy will be the kernel helper concept. Sorta like how modules are loaded by the kernel. You should look into that mechanism.



Code:


call_usermodehelper   
call_usermodehelper_pipe
call_usermodehelper_keys



Is that what you're talking about?
Back to top
View user's profile Send private message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Thu Feb 29, 2024 7:22 pm    Post subject: Reply with quote

can someone describe exactly how to do this without riddles?
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21637

PostPosted: Thu Feb 29, 2024 8:30 pm    Post subject: Reply with quote

Those look like reasonable choices, yes. The kernel documentation is generally free of riddles, so you may wish to review it for how to use these functions.
Back to top
View user's profile Send private message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Thu Feb 29, 2024 11:02 pm    Post subject: Reply with quote

Hu wrote:
Those look like reasonable choices, yes. The kernel documentation is generally free of riddles, so you may wish to review it for how to use these functions.


It will take a lot of time. but I can spend this time improving the program. we have a lot of developers on the forum, I think it's not difficult for them to tell a beginner. I need a starting point in understanding the work. the hint will speed up the writing process.
Back to top
View user's profile Send private message
pietinger
Moderator
Moderator


Joined: 17 Oct 2006
Posts: 4160
Location: Bavaria

PostPosted: Thu Feb 29, 2024 11:19 pm    Post subject: Reply with quote

Gentoopc wrote:
can someone describe exactly how to do this without riddles?

I dont write kernel modules, but I googled "writing linux kernel module" and this gave me:
https://medium.com/dvt-engineering/how-to-write-your-first-linux-kernel-module-cf284408beeb
as 2nd result. The first result was:
https://sysprog21.github.io/lkmpg/

Both looking good to me.
_________________
https://wiki.gentoo.org/wiki/User:Pietinger
Back to top
View user's profile Send private message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Thu Feb 29, 2024 11:57 pm    Post subject: Reply with quote

pietinger wrote:


Is there a solution to the problem I described above in what you gave me?
Back to top
View user's profile Send private message
pietinger
Moderator
Moderator


Joined: 17 Oct 2006
Posts: 4160
Location: Bavaria

PostPosted: Fri Mar 01, 2024 12:30 am    Post subject: Reply with quote

Gentoopc wrote:
pietinger wrote:


Is there a solution to the problem I described above in what you gave me?

Yes =>
Code:
/*
 * hello-1.c - The simplest kernel module.
 */
#include <linux/module.h> /* Needed by all modules */
#include <linux/printk.h> /* Needed for pr_info() */
 
int init_module(void)
{
    pr_info("Hello world 1.\n");
 
    /* A non 0 return means init_module failed; module can't be loaded. */
    return 0;
}
 
void cleanup_module(void)
{
    pr_info("Goodbye world 1.\n");
}
 
MODULE_LICENSE("GPL");

_________________
https://wiki.gentoo.org/wiki/User:Pietinger
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21637

PostPosted: Fri Mar 01, 2024 12:32 am    Post subject: Reply with quote

We have many developers, but I'm unsure how many of them are proficient in Linux kernel programming. Regardless, I think there are no CUDA kernel programmers here.

To your original question, you cannot normally call a function in userspace from kernel space. This would be a serious security problem, and the kernel has several measures to prevent it.
Back to top
View user's profile Send private message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Fri Mar 01, 2024 2:37 am    Post subject: Reply with quote

Hu wrote:

To your original question, you cannot normally call a function in userspace from kernel space. This would be a serious security problem, and the kernel has several measures to prevent it.


Why? it is not a user space function that calls a kernel function. it should be possible to run any function from the kernel. you can only think differently when you don't believe the core. if the kernel developers do not provide such an opportunity, then this speaks volumes.
Back to top
View user's profile Send private message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Fri Mar 01, 2024 2:45 am    Post subject: Reply with quote

pietinger wrote:
Gentoopc wrote:
pietinger wrote:


Is there a solution to the problem I described above in what you gave me?

Yes =>
Code:
/*
 * hello-1.c - The simplest kernel module.
 */
#include <linux/module.h> /* Needed by all modules */
#include <linux/printk.h> /* Needed for pr_info() */
 
int init_module(void)
{
    pr_info("Hello world 1.\n");
 
    /* A non 0 return means init_module failed; module can't be loaded. */
    return 0;
}
 
void cleanup_module(void)
{
    pr_info("Goodbye world 1.\n");
}
 
MODULE_LICENSE("GPL");


pr_info are kernel functions. That's how I can do it. The pr_info function is not from user space. Or can you do that?


Code:


void FOO(){printf("  "); };

#include <linux/module.h> /* Needed by all modules */
#include <linux/printk.h> /* Needed for pr_info() */
 
int init_module(void)
{
    FOO();    // hat would be the solution to the problem.
   
    return 0;
}
 
void cleanup_module(void)
{
   
}


that would solve the problem.
Back to top
View user's profile Send private message
GDH-gentoo
Veteran
Veteran


Joined: 20 Jul 2019
Posts: 1537
Location: South America

PostPosted: Fri Mar 01, 2024 3:23 pm    Post subject: Reply with quote

I'm going to say this only once. You can't do that because FOO() calls printf(), printf() is implemented in the operating system's libc, and the kernel runs in what is called a "freestanding execution environment". If you need an additional explanation about the meaning of the last sentence, then excuse my bluntness, but you don't have (yet) the required knowledge to write a kernel module.

And yes, expecting that someone come to threads like this to post a ready to use solution for you so that you can take shortcuts is unrealistic.
_________________
NeddySeagoon wrote:
I'm not a witch, I'm a retired electronics engineer :)
Ionen wrote:
As a packager I just don't want things to get messier with weird build systems and multiple toolchains requirements though :)


Last edited by GDH-gentoo on Fri Mar 01, 2024 8:50 pm; edited 1 time in total
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21637

PostPosted: Fri Mar 01, 2024 3:51 pm    Post subject: Reply with quote

I did not write that the kernel cannot possibly call a userspace function. I wrote that it is normally disallowed, because it would be a security problem if it were done. Separately, GDH-gentoo points out that even if this were allowed, there are technical limitations into what you can expect to work.
Back to top
View user's profile Send private message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Sat Mar 02, 2024 12:28 pm    Post subject: Reply with quote

Hu wrote:
I did not write that the kernel cannot possibly call a userspace function. I wrote that it is normally disallowed, because it would be a security problem if it were done. Separately, GDH-gentoo points out that even if this were allowed, there are technical limitations into what you can expect to work.


that you're writing nonsense. I'm not harming the kernel. I'm trying to do something for free for everyone. if it will be useful, then I will try it and you will find out about it. isn't the idea of Linux that everyone can do as they want? it seems Linux has become Windows.
Back to top
View user's profile Send private message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Sat Mar 02, 2024 12:32 pm    Post subject: Reply with quote

GDH-gentoo wrote:
.



empty words. if you had helped beginners more and said less empty words, then the core would have improved long ago, as well as the Gentoo system as a whole. and so no help, just another chatterbox.
Back to top
View user's profile Send private message
logrusx
Veteran
Veteran


Joined: 22 Feb 2018
Posts: 1539

PostPosted: Sat Mar 02, 2024 1:09 pm    Post subject: Reply with quote

Nobody is obligated to do anything for you. Help yourself by reading the LKMPG.

Best Regards,
Georgi
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21637

PostPosted: Sat Mar 02, 2024 3:49 pm    Post subject: Reply with quote

Gentoopc, once again, you are grossly misunderstanding the responses given to you, and seem to be actively trying to antagonize the people whose help you think you deserve. I interpret your response to my post as an indication you failed to understand my post. I do not see a way to help you if you refuse to read, and see no value in trying to help someone so antagonistic.
Back to top
View user's profile Send private message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Sat Mar 02, 2024 4:55 pm    Post subject: Reply with quote

Hu wrote:
.


I'm not hostile. I'm even very patient and condescending to jerks who attack, shouting about the danger they see in someone trying not to stupidly use what they've been given, but to try to do better. It is very safe to run custom functions from the Linux kernel. this is the highest degree of security. because to do this, you need to take the source code of the kernel, register it in a system call, for example, a call to a user function, then assemble the kernel and install it. any malicious person will not be able to do this if they do not have access to your PC. if you're so worried about safety, then there's so much shit in the kernel that you'll never get out of there. An example is the University of Minnesota. but for some reason you see the danger in me. Then what should we talk about.

the most dangerous thing is that some people mislead me that it is impossible to call user-defined functions from a kernel module. Should I be grateful to them for their lies?








It's not about you. the speech as a whole.


Last edited by Gentoopc on Sat Mar 02, 2024 4:57 pm; edited 1 time in total
Back to top
View user's profile Send private message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 20067

PostPosted: Sat Mar 02, 2024 4:56 pm    Post subject: Reply with quote

Gentoopc wrote:
Hu wrote:
I did not write that the kernel cannot possibly call a userspace function. I wrote that it is normally disallowed, because it would be a security problem if it were done. Separately, GDH-gentoo points out that even if this were allowed, there are technical limitations into what you can expect to work.


that you're writing nonsense. I'm not harming the kernel. I'm trying to do something for free for everyone. if it will be useful, then I will try it and you will find out about it. isn't the idea of Linux that everyone can do as they want? it seems Linux has become Windows.
The security issue isn't about _your_ intentions. It is about those who _do_ have bad intentions. Since intentions cannot be determined by code, the result affects everyone, regardless of their intent.
_________________
Quis separabit? Quo animo?
Back to top
View user's profile Send private message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Sat Mar 02, 2024 5:07 pm    Post subject: Reply with quote

pjp wrote:
.



what is dangerous if I call a function written with my own hands from the kernel module using a system call? if I wanted to do this from the kernel, then I would have to rebuild it. but what's dangerous if I do it from the module? That the world will collapse? a core that is like Swiss cheese with tons of vulnerabilities, no one will ever be able to make it worse. that there are false words, that everyone can do something different in Linux. as soon as you try to do it, they don't let you.
Back to top
View user's profile Send private message
logrusx
Veteran
Veteran


Joined: 22 Feb 2018
Posts: 1539

PostPosted: Sat Mar 02, 2024 5:24 pm    Post subject: Reply with quote

Let me phrase it that way. There are ways to do such things, but you can't, because you're not knowledgeable enough, because you don't want to read and instead insist on somebody else doing what you want being done. This is not how things work.

Best Regards,
Georgi
Back to top
View user's profile Send private message
Zucca
Moderator
Moderator


Joined: 14 Jun 2007
Posts: 3345
Location: Rasi, Finland

PostPosted: Sat Mar 02, 2024 5:28 pm    Post subject: Reply with quote

Gentoopc wrote:
what is dangerous if I call a function written with my own hands from the kernel module using a system call?
Let's repeat:
pjp wrote:
Since intentions cannot be determined by code, the result affects everyone, regardless of their intent.

_________________
..: Zucca :..
Gentoo IRC channels reside on Libera.Chat.
--
Quote:
I am NaN! I am a man!
Back to top
View user's profile Send private message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Sat Mar 02, 2024 5:28 pm    Post subject: Reply with quote

pjp wrote:


if the developers wanted to do good to Linux users, they would have done so.
that would solve all the problems forever. everyone would decide for themselves what they need. and it would simplify the development
Code:


Enable loadable module support
 [*] allow functions to be run from user space in the kernel module
Back to top
View user's profile Send private message
Gentoopc
Apprentice
Apprentice


Joined: 25 Dec 2017
Posts: 296

PostPosted: Sat Mar 02, 2024 5:34 pm    Post subject: Reply with quote

logrusx wrote:


No. The problem is different. It's just based on childish, ill-considered decisions. it's like I'm a Linux god, I'm a developer, only I can do this, and let the rest of them read books and find answers in them that they are shit and will never compare to the king kernel.
Back to top
View user's profile Send private message
Display posts from previous:   
This topic is locked: you cannot edit posts or make replies.    Gentoo Forums Forum Index Kernel & Hardware All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum