[lttng-dev] How to ensure null char to string field?

Mathieu Desnoyers mathieu.desnoyers at efficios.com
Mon May 28 11:27:31 EDT 2012


* Francis Giraldeau (francis.giraldeau at gmail.com) wrote:
> Hi, 
> 
> I want to save a string from user space through a proc file. It seems
> trivial, but I have a problem with null termination. The user space
> string is _not_ null terminated. Here is an hexdump of the string bytes
> for "bidon" after performing copy_from_user on the kernel stack:
> 
> [ 3163.957414] 0x62 0x69 0x64 0x6F 0x6E 0xFFFFFFDF ...
> 
> The tracepoint I use is:
> 
> TRACE_EVENT(lttng_uevent_cfu,
> 	TP_PROTO(const char * str),
> 	TP_ARGS(str),
> 	TP_STRUCT__entry(__string_from_user(str, str)),
> 	TP_fast_assign(tp_copy_string_from_user(str, str)),
> 	TP_printk()
> )
> 
> The user_strlen continues beyond the real string length because it's not
> null terminated. The string length is already known (given as a
> parameter of the write callback), such that I would like to copy only
> this number of bytes and then set last byte to '\0'. Also, this would be
> required to limit the string size.
> 
> I have not found a way to do it with current macros. Shall a new macro
> to handle this use case be added?

I think we would need to add:

for TP_STRUCT__entry:

 __dynamic_array_from_user_text

and implement, for TP_fast_assign:

   __dynamic_array_from_userenc_ext

which will write the string length, followed by an array of bytes,
copied from userspace.

Look at all the "__dynamic_array" and "tp_memcpy_dyn" match in
lttng-events.h as a starting point.

Thanks,

Mathieu

> 
> Cheers,
> 
> Francis Giraldeau
> 
> 
> _______________________________________________
> lttng-dev mailing list
> lttng-dev at lists.lttng.org
> http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

-- 
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com



More information about the lttng-dev mailing list