LSM-Howto: Kernelmodule with non exported functions - compilation

I'm currently writing a Linux Kernel module which depends on the Linux Security Modules (LSM) at the moment it is nothing really, I just wanted to print out a simple message whenever a file is opened. The problem is: To register to the hook I need the function register_security, which - I found out after googleing - isn't exported anymore and thus can't be used by loadable kernel modules - only by modules which are compiled directly into the kernel.
Of course this makes sense for a security module, but it suckes for me developing.
So now the question to you: Is there a way of patching my module into the kernel? I mean, I don't want to recompile my kernel after every bugfix or for every minor change. I could live with rebooting my pc for every new try, but recompiling would take a little bit to long I guess..
Edit: Hm, noone yet :( I just had an idea, maybe someone can tell me if it's good or not: Can't I just add the EXPORT_SYMBOL in the kernel source for the functions I need, then recompile it and then add my code as a module? Of course this would be just for testing and debugging

Can't you just use fsnotify in kernel, or fanotify from user space?
It's not generally a good idea to export functions that the author didn't think it would be a good idea to export. If you call a function that isn't part of the public interface and that function has a side effect, you will probably break things. Besides, your module won't work on other machines, but maybe you don't care about this.

No, there is not. When a symbol is not exported, the in-kernel linker will not be able to find it. But adding the export to the kernel you use for testing should be OK. You can add your module to the export list by adding it to ./include/linux/Kbuild.
Also if testing in (user-mode-linux)[http://user-mode-linux.sourceforge.net/] or in virtual box, recompiling whole kernel might not be that big problem.

This may be a bit late as I see your question a while back. What I found to be a good solution is to write a module that you compile into the kernel and just exports the couple of functions you what to play with.
For example
//REGISTER FILE_PERMISSION
static void k_register_file_permission(int (*my_file_permission) (struct file *file, int mask)) {
my_file_permission_func = my_file_permission;
}
EXPORT_SYMBOL(k_register_file_permission);
Then you can just call k_register_file_permission from your kernel module, handy durring the development process.
You would also need a function like
int k_file_permission (struct file *file, int mask) {
if(my_file_permission_func == NULL)
{
//do nothing
}
else
{
return my_file_permission_func(file, mask);
}
return 0;
}
That you would register with the LSM at boot time.

Related

Add new system call at FreeBSD

I wanna add custom helloworld syscall to FreeBSD. I used following link as my guide: http://members.tripod.com/s_mathur/bsdhowto.html In step 4 says: Modify the Make File to include sys_hello.c , etc and recompile the kernel! Which Make File? Where is it? and how to compile it and how call syscall hello?
The error that I faced with it, is:
init_sysent.o:(.data + 0x6638): undefined reference to 'sys_hello'
I think that it is because of my Make file, because I don't know I should modify which Make File.
I'm afraid you are not ready to do any kernel development and as such strongly suggest you refrain from it.
I don't know how you ended up on that guide, I have trouble finding it when I look for ways to add system calls to the FreeBSD kernel.
The guide has bits which are outdated and some which were always wrong.
You created a new file (sys_hello.c) but did not add it to the build process. Figuring out how to do that should be trivial.
1. pick a syscall which is always provided, like fork
2. find the file implementing it
3. grep the source tree for mentions of that file
4. profit
Performing the steps and getting the answer is left as an exercise for the reader.
int syshello(p, uap)
struct proc* p; struct syshello_args uap;
K&R C declaration? Just how old is this?
The first argument for several years now is struct thread *.
{
sprintf(uap->buf,"Hello"); /* fill the buffer with Hello */
Fundamentally wrong. Consider what happens if userspace passes a kernel address. Also this assumes shared address spaces to "work". The code should have used copyout. Except the code is additionally wrong by not having an argument allowing the userspace to say what the size is.
p->p_retval[0] = 0; /* set the return value of the system call*/
return 0;
}
As noted earlier, given your difficulty with figuring out what to do with the new file it is clear you are new to programming and as such you really should not touch the kernel until you grow.

How do you fully initialize an embedded ruby VM in a C++ application?

I am embedding ruby version 2.1.2 into a wxWidgets application, compiling on - and targeting - Windows. Linking to msvcrt-ruby210.dll and calling
ruby_sysinit(&argc, &argv);
RUBY_INIT_STACK;
ruby_init();
ruby_init_loadpath();
is enough to get me running with the basic VM and built-in classes. However, I am also packaging the standard library with my application, as I intend use facilities like FileUtils and Resolv from my application. I can require and use some of the libraries just fine, yet when I require 'resolv' I get an error reporting unitialized constant Encoding::UTF_16LE. After some googling and digging around in ruby.c, I've found I can fix this with the following initialization code...
ruby_sysinit(&argc, &argv);
RUBY_INIT_STACK;
ruby_init();
ruby_init_loadpath();
rb_enc_find_index("encdb");
Which clears the previous error, but leaves me with code converter not found (UTF-8 to UTF-16LE). This is fixed by adding an additional line rb_eval_string("require 'enc/trans/transdb'");, but, it is not my desire to replicate, piece by piece, the initialization code performed by ruby's ruby_options function, so I tried to use it directly, as in ruby's own main function...
int my_argc = 2;
char* arg1 = "myapp.exe";
char* arg2 = "scripts/bootstrap.rb";
char** my_argv = new char*[2]{arg1, arg2};
ruby_sysinit(&my_argc, &my_argv);
RUBY_INIT_STACK;
ruby_init();
ruby_run_node(ruby_options(my_argc, my_argv));
This, however, is only effective if I run my application with myapp.exe scripts/bootstrap.rb. It seems that ruby is ignoring my parameters to ruby_options and using the system supplied values of argc & argv (Apparently this has been the case for some time on Windows). This is bothersome, as I would like my application to run with a simple double-click of the executable, and not require users to supply command line arguments indicating the location of the "bootstrap" script.
So, is there a convenient API or some incantation I can use to initialize ruby in this case without requiring the command line parameters?
Note that if at all possible, I would like to avoid having to package my application as a ruby extension.
I noticed this code in pepper_main.c, and suspected this was doing about what I wanted.
static VALUE
init_libraries_internal(VALUE unused)
{
extern void Init_enc();
extern void Init_ext();
init_loadpath();
Init_enc();
Init_ext();
return Qnil;
}
As far as I can figure, I don't need Init_ext() since I'm using the ruby dll, and I'm not statically compiling my extensions. So, I tried just using Init_enc. While this symbol is present in the msvcrt-ruby210.dll, it's not present in the import library (msvcrt-ruby210.dll.a) so I wasn't able to link it with my application. Searching through the symbols in the .so files under the lib\ruby\2.1.0\i386-mingw32\enc directory, I was able to find Init_encdb in encdb.so, and Init_transdb in trans/transdb.so. So, I've required these libs and my bootstrap script as shown below:
ruby_sysinit(&argc, &argv);
RUBY_INIT_STACK;
ruby_init();
ruby_init_loadpath();
rb_require("enc/encdb");
rb_require("enc/trans/transdb");
rb_require("./scripts/bootstrap");
This enables me to use the FileUtils and Resolv libraries without error. While I can't be sure I won't run into more issues like this (I've yet to try requiring an actual gem...) this is a solution I'm more comfortable with. If I can resolve any forthcoming issues with a simple require, as opposed to digging around to find obscure commands like rb_enc_find_index("encdb"); to sprinkle into my initialization code, then that seems reasonable.
I am still interested in any simpler alternatives, and will hold off on accepting this as the answer - for a while, at least - until I receive some confirmation that I'm going about this correctly.

net_device and net_device_ops struct

I would like add a new operation at the struct net_device_ops but I am a really newbye in this type of things and I am a bit worried to follow a wrong way from the begin.
I added a ops like this:
static const struct net_device_ops wl_netdev_ops =
{
/* The other operations..
.ndo_clear_stats = clear_stats
};
What is not clear from my point of view is how I can call from user space that, I usually take statisincs from
/sys/class/net/.../statistics
But now I really don't understand where my new operation is placed, can someone help me telling a good tutorial or link where I can find a simple example or tutorial ?
Thanks in advance,
pedr0
Interesting material
You can't call it directly. You need to export its functionality somehow to userspace, e.g. via an ioctl, netlink, a procfs entry, etc. Which one of these is recommended depends largely on what exactly you're trying to achieve.
Usually it's also advised not to change core kernel structures like this, even if you don't plan to distribute your changes - sometimes order of kernel structure members or the size of it matters, and there are assumptions internally in the kernel regarding this. I'm pretty sure there is some other way to do what you want.

Looking for C source code for snprintf()

I need to port snprintf() to another platform that does not fully support GLibC.
I am looking for the underlying declaration in the Glibc 2.14 source code. I follow many function calls, but get stuck on vfprintf(). It then seems to call _IO_vfprintf(), but I cannot find the definition. Probably a macro is obfuscating things.
I need to see the real C code that scans the format string and calculates the number of bytes it would write if input buffer was large enough.
I also tried looking in newlib 1.19.0, but I got stuck on _svfprintf_r(). I cannot find the definition anywhere.
Can someone point me to either definition or another one for snprintf()?
I've spent quite a while digging the sources to find _svfprintf_r() (and friends) definitions in the Newlib. Since OP asked about it, I'll post my finding for the poor souls who need those as well. The following holds true for Newlib 1.20.0, but I guess it is more or less the same across different versions.
The actual sources are located in the vfprintf.c file. There is a macro _VFPRINTF_R set to one of _svfiprintf_r, _vfiprintf_r, _svfprintf_r, or _vfprintf_r (depending on the build options), and then the actual implementation function is defined accordingly:
int
_DEFUN(_VFPRINTF_R, (data, fp, fmt0, ap),
struct _reent *data _AND
FILE * fp _AND
_CONST char *fmt0 _AND
va_list ap)
{
...
http://www.ijs.si/software/snprintf/ has what they claim is a portable implementation of snprintf, including vsnprintf.c, asnprintf, vasnprintf, asprintf, vasprintf. Perhaps it can help.
The source code of the GNU C library (glibc) is hosted on sourceware.org.
Here is a link to the implementation of vfprintf(), which is called by snprintf():
https://sourceware.org/git/?p=glibc.git;a=blob;f=stdio-common/vfprintf.c

How do I control which symbols a Windows DLL imports from the application?

I'm trying to build a shared library (DLL) on Windows, using MSVC 6 (retro!) and I have a peculiar link issue I need to resolve. My shared library must access some global state, controlled by the loading application.
Broadly, what I have is this:
application.c:
static int g_private_value;
int use_private_value() {
/* do something with g_private_value */
}
int main (...) {
return shared_library_method ();
}
shared_library.c:
__declspec(dllexport) int __stdcall shared_library_method() {
use_private_value();
}
(Updated - I forgot the __declspec(dllexport) int __stdcall portion, but it's there in the real code)
How do I set up shared_library.dll so that it exports shared_library_method and imports use_private_value?
Please remember that A) I'm a unix programmer, generally, and B) that I'm doing this without Visual Studio; our automated build infrastructure drives MSVC with makefiles. If I'm omitting something that will make it easier to answer the question, please comment and I'll update it ASAP.
This is actually going to be pretty difficult to get working. On Unix/Linux you can have shared objects and applications import symbols from each other, but on Windows you can't have a DLL import symbols from the application that loads it: the Windows PE executable format just doesn't support that idiom.
I know that the Cygwin project have some sort of work-around to address this problem, but I don't believe that it's trivial. Unless you want to do lots of PE-related hacking you probably don't want to go there.
An easier solution might be to just have some sort of initializer method exported from the DLL:
typedef int (*func_ptr)();
void init_library(func_ptr func);
The application must call this at start-up, passing in the address of the function you want to share. Not exactly elegant, but it should work okay.
I'll start with half of the answer.
In shared_library.c:
__declspec(dllexport) int __stdcall shared_library_method(void)
{
}
The MSDN article about exporting function from DLL:s.
For the second half you need to export the functions from your application.c.
You can do this in the linker with:
/export:use_private_value#0
This should get you a lib-file that you build with your DLL.
The option to link the lib-file is to use GetProcAddress().
As DavidK noted if you only have a few functions it is probably easier to pass the function pointers in an init function. It is however possible to do what you are asking for.

Resources