View previous topic :: View next topic |
Author |
Message |
Gentoopc Apprentice
Joined: 25 Dec 2017 Posts: 296
|
Posted: Thu Feb 29, 2024 6:27 pm Post subject: kernel_modules |
|
|
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 |
|
|
gentoo_ram Guru
Joined: 25 Oct 2007 Posts: 474 Location: San Diego, California USA
|
Posted: Thu Feb 29, 2024 6:42 pm Post subject: |
|
|
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 |
|
|
Gentoopc Apprentice
Joined: 25 Dec 2017 Posts: 296
|
Posted: Thu Feb 29, 2024 6:53 pm Post subject: |
|
|
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 |
|
|
Gentoopc Apprentice
Joined: 25 Dec 2017 Posts: 296
|
Posted: Thu Feb 29, 2024 7:22 pm Post subject: |
|
|
can someone describe exactly how to do this without riddles? |
|
Back to top |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21637
|
Posted: Thu Feb 29, 2024 8:30 pm Post subject: |
|
|
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 |
|
|
Gentoopc Apprentice
Joined: 25 Dec 2017 Posts: 296
|
Posted: Thu Feb 29, 2024 11:02 pm Post subject: |
|
|
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 |
|
|
pietinger Moderator
Joined: 17 Oct 2006 Posts: 4160 Location: Bavaria
|
|
Back to top |
|
|
Gentoopc Apprentice
Joined: 25 Dec 2017 Posts: 296
|
Posted: Thu Feb 29, 2024 11:57 pm Post subject: |
|
|
Is there a solution to the problem I described above in what you gave me? |
|
Back to top |
|
|
pietinger Moderator
Joined: 17 Oct 2006 Posts: 4160 Location: Bavaria
|
Posted: Fri Mar 01, 2024 12:30 am Post subject: |
|
|
Gentoopc 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 |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21637
|
Posted: Fri Mar 01, 2024 12:32 am Post subject: |
|
|
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 |
|
|
Gentoopc Apprentice
Joined: 25 Dec 2017 Posts: 296
|
Posted: Fri Mar 01, 2024 2:37 am Post subject: |
|
|
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 |
|
|
Gentoopc Apprentice
Joined: 25 Dec 2017 Posts: 296
|
Posted: Fri Mar 01, 2024 2:45 am Post subject: |
|
|
pietinger wrote: | Gentoopc 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 |
|
|
GDH-gentoo Veteran
Joined: 20 Jul 2019 Posts: 1537 Location: South America
|
Posted: Fri Mar 01, 2024 3:23 pm Post subject: |
|
|
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 |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21637
|
Posted: Fri Mar 01, 2024 3:51 pm Post subject: |
|
|
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 |
|
|
Gentoopc Apprentice
Joined: 25 Dec 2017 Posts: 296
|
Posted: Sat Mar 02, 2024 12:28 pm Post subject: |
|
|
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 |
|
|
Gentoopc Apprentice
Joined: 25 Dec 2017 Posts: 296
|
Posted: Sat Mar 02, 2024 12:32 pm Post subject: |
|
|
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 |
|
|
logrusx Veteran
Joined: 22 Feb 2018 Posts: 1539
|
Posted: Sat Mar 02, 2024 1:09 pm Post subject: |
|
|
Nobody is obligated to do anything for you. Help yourself by reading the LKMPG.
Best Regards,
Georgi |
|
Back to top |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21637
|
Posted: Sat Mar 02, 2024 3:49 pm Post subject: |
|
|
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 |
|
|
Gentoopc Apprentice
Joined: 25 Dec 2017 Posts: 296
|
Posted: Sat Mar 02, 2024 4:55 pm Post subject: |
|
|
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 |
|
|
pjp Administrator
Joined: 16 Apr 2002 Posts: 20067
|
Posted: Sat Mar 02, 2024 4:56 pm Post subject: |
|
|
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 |
|
|
Gentoopc Apprentice
Joined: 25 Dec 2017 Posts: 296
|
Posted: Sat Mar 02, 2024 5:07 pm Post subject: |
|
|
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 |
|
|
logrusx Veteran
Joined: 22 Feb 2018 Posts: 1539
|
Posted: Sat Mar 02, 2024 5:24 pm Post subject: |
|
|
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 |
|
|
Zucca Moderator
Joined: 14 Jun 2007 Posts: 3345 Location: Rasi, Finland
|
Posted: Sat Mar 02, 2024 5:28 pm Post subject: |
|
|
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 |
|
|
Gentoopc Apprentice
Joined: 25 Dec 2017 Posts: 296
|
Posted: Sat Mar 02, 2024 5:28 pm Post subject: |
|
|
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 |
|
|
Gentoopc Apprentice
Joined: 25 Dec 2017 Posts: 296
|
Posted: Sat Mar 02, 2024 5:34 pm Post subject: |
|
|
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 |
|
|
|