Is there function like sprintf()
in Linux Kernel (like printf()
->printk()
)?
yes there is check out here for example
you can use grep to see if it is in the kernel's source code
yes. https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/lib/vsprintf.c#n1828
int snprintf(char *buf, size_t size, const char *fmt, ...)
{
va_list args;
int i;
va_start(args, fmt);
i = vsnprintf(buf, size, fmt, args);
va_end(args);
return i;
}
EXPORT_SYMBOL(snprintf);
sprintf()
by itself is prone to buffer overflows. CERT buffer overflows, Apple, etc
Running crash on a live 2.6 kernel confirms sprintf() is defined, and where it is defined.
crash> sym sprintf
ffffffff81267ba0 (T) sprintf ../debug/kernel-2.6.39/linux-2.6.39-400.210.2.el6uek/lib/vsprintf.c: 1442
Yes, just include linux/kernel.h
sprintf()
is unsafe because of buffer overflow.
If you need to pass data from user space to kernel space, use instead copy_from_user()
; it acts like copy_to_user()
but in reverse direction.