I have a function like
__android_log_print(ANDROID_LOG_INFO, "HelloNDK!");
on my C code
I wouldn't find that output on my LogCat. What kind of filter I need to setup
by Log Tag, by Log Message, by Application Name, by Log Level...etc.
You don't find output because you have misused the function. The function has the prototype:
int __android_log_print(int prio, const char *tag, const char *fmt, ...);
So you must supply a "tag" as well as the format.
For example
__android_log_print(ANDROID_LOG_INFO, "MyTag", "The value is %d", some_variable);
Once you use the function properly, you can use any filtering method (or none at all - such as you'd get from the adb logcat command without further arguments) just as with java code.
Related
I am interested in demoing printf vulnerabilities via an NDK app. To be clear, I am aware that to log in the console we can use __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "Print : %d %s",someVal, someStr);. I have tried it and I know it works. But I explicitly want to demo the vulnerabilities of printf(), specifically to use the %n specifier to write to a pointed location.
Is there a way to make printf() work to this effect or is it possible to achieve this via __android_log_print()? I attempted it with the android/log.h header but it didn't work.
I can get the app to crash by running something along the lines of printf(%s%s%s%s%s%s%s%s%s%s). But again, I can't manipulate pointers.
For general knowledge purposes, why is it that printf() doesn't work in the first place and how does __android_log_print() prevent these exploits?
You do realize that Android is open source.
Starting with looking for __android_log_print()
and finding it: https://android.googlesource.com/platform/system/core/+/refs/heads/master/liblog/logger_write.cpp
int __android_log_print(int prio, const char* tag, const char* fmt, ...) {
va_list ap;
char buf[LOG_BUF_SIZE];
va_start(ap, fmt);
vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
va_end(ap);
return __android_log_write(prio, tag, buf);
}
I eventually ended up looking at: https://android.googlesource.com/platform/bionic/+/refs/heads/master/libc/stdio/vfprintf.cpp
lines 453-454:
case 'n':
__fortify_fatal("%%n not allowed on Android");
Also referenced in the code is additional safety through FORTIFY which is described in the following blog post:
https://android-developers.googleblog.com/2017/04/fortify-in-android.html
Android specifically does not support %n format specifiers because they're vulnerable.
https://android.googlesource.com/platform/bionic/+/400b073ee38ecc2a38234261b221e3a7afc0498e/tests/stdio_test.cpp#328
while looking in android logging frame work -
I checked /dev/log/main |system |event log files. in these log file I dont see time stamp. but at the same time "logcat -v time" display time stamp along with log.
I check Logcat code Its reading androidlog_entry from /dev/log/* buffers and displaying on cmdline.
While tracing android logging code , I could not find at what point we are adding timestamp with our log.
I did trace following flow -
LOGI("pre_alloc_cap_mem_thread_init: inited=%d", obj->mPreAllocCapMemInited);
#define LOGE ALOGE
#define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, VA_ARGS))
#define LOG_PRI(priority, tag, ...) \
android_printLog(priority, tag, VA_ARGS)
#define android_printLog(prio, tag, fmt...) \
__android_log_print(prio, tag, fmt)
int __android_log_print(int prio, const char *tag, const char *fmt, ...)
{
va_list ap;
char buf[LOG_BUF_SIZE];
va_start(ap, fmt);
vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
va_end(ap);
return __android_log_write(prio, tag, buf);
}
and on and on till NR_writev system call .
can somebody please guide me, when Its adding timestamp to android logs.
I appreciate help.. what ever i could get ..
But i have figure out it -
"Android frame work does not add time stamp, threadid etc. to android logs that goes to /dev/log/, but It rather pass it down to kernel to logger driver(check logger.c in kernel code) that further add up the timestamp+PID etc. as prefix to each android log. "
I have spent a ridiculous amount of time trying to figure this out and I am at an absolute loss.
I am working with the JUCE library and have modified one of their sample projects. My goal is to have a very simple Android app that is written in C++ and then ported to Android. I need a function in C++ that I can call that will then call a function on the Android side that will return my heap size and other characteristics to my C++ code so that I can manage memory there.
If anyone has a simple solution that would be amazing. Right now my current snag is this:
char const buf[] = "From JNI";
jstring jstr = env->NewStringUTF(buf);
jclass clazz = env->FindClass("android/os/Debug");
But I keep getting an error saying that 'NewStringUTF' is not a _JNIEnv member... but if I right click on the method and jump to the definition, I see it in my jni.h file... any suggestions? I'm working in Xcode by the way...
Is it plain C, not C++? Perhaps your file has a .c extension.
If it's plain C it should be
JNIEnv* env;
JNI_CreateJavaVM(&jvm, (void **)&env, &args);
(*env)->NewStringUTF(env, buf);
I want to get a struct tm from a const char *. In order to achieve the goal I use following code:
CCLog("ServerTimeStamp:%s",servertimestamp);
struct tm servertime;
servertime.tm_isdst=0;
servertime.tm_gmtoff=0;
CCLog("servertime prev:{%i,%i,%i,%i:%i:%i+%i+%li}",servertime.tm_year,servertime.tm_mon,servertime.tm_mday,servertime.tm_hour,servertime.tm_min,servertime.tm_sec,servertime.tm_isdst,servertime.tm_gmtoff);
strptime(servertimestamp, "%FT%TZ", &servertime);
CCLog("servertime post:{%i,%i,%i,%i:%i:%i+%i+%li}",servertime.tm_year,servertime.tm_mon,servertime.tm_mday,servertime.tm_hour,servertime.tm_min,servertime.tm_sec,servertime.tm_isdst,servertime.tm_gmtoff);
But the output is:
ServerTimeStamp:2014-07-18T11:51:09Z
servertime prev:{24,1803363120,22,1803363016:0:1796668624+0+0}
servertime post:{24,1803363120,22,1803363016:0:1796668624+0+0}
Which means that strptime has probably done nothing. Is this function not implemented on Android under JNI (In the cocos2d-x environment, version 2.1rc0-x-2.1.4)? What would be a suitable alternative? How can I convert the const char * to struct tm?
My App is cross-platform, and the same code works perfectly under iOS (also with the environment cocos2d-x, version 2.1rc0-x-2.1.4).
Thanks in advance!
Don't use the %F specifier for a date.
I am stuck and am getting an error somewhere within my C code, and I do not know where. I would like to use the simple Log.i( tag, msg ) or Log.e( tag, msg ) commands. I have looked around online and have found two other questions on SO but neither of them deal with exactly what I'm saying.
This method is not what I'm looking for...
And this is exactly what I'm looking for, but in C++, not C
If the syntax in C++/C is the same, I'm sorry but I have little experience in both.
Syntax in C is the same
#include <android/log.h>
#define TAG "MYDEBUG"
#ifdef DEBUG
# define D(x...) __android_log_print(ANDROID_LOG_INFO, TAG , x)
#else
# define D(x...) do {} while (0)
#endif
# define W(x...) __android_log_print(ANDROID_LOG_WARN, TAG , x)
# define E(x...) __android_log_print(ANDROID_LOG_ERROR, TAG , x)
#include <cutils/log.h>
#define LOG_TAG "MYDEBUG"
...
ALOGD("Here we are!");
In older releases the macro was:
LOGD("Here we are!");