I just came across this neat gdb trick. Say you have a badly written program that's leaking file descriptors all over.. and it's in prod (the horror!) and it's imperative that you let it limp along till the patched binary comes around... GDB can close the file descriptors for you!
Here's how:
Leaky Program
Lsof tells it all. Take note of the two open descriptors to /etc/passwd
Playing around with variables in GDB. Can you guess why we chose the magic value 2147483646? For a hint, look at the last gist in this post
What does lsof think?
Bulk close of fds
Shell output from leaky
Showing posts with label C. Show all posts
Showing posts with label C. Show all posts
Wednesday, February 27, 2013
Friday, April 15, 2011
recursion && tail recursion
A look at the difference between plain old recursion and tail recursion.
- Compile recursion.c
- Fire up GDB
$ gdb recursion
GNU gdb (GDB) 7.2-debian
.....
(gdb)
- Set up breakpoints at recursion termination points
(gdb) break recursion.c:50
Breakpoint 1 at 0x4005b9: file /home/lmwangi/learning/algo/recursions/recursion.c, line 50.
(gdb) break recursion.c:68
Breakpoint 2 at 0x400601: file /home/lmwangi/learning/algo/recursions/recursion.c, line 68.
- Run till the first breakpoint - Good old recursion. Notice that we have to step 4 times.
(gdb) run
Starting program: /home/lmwangi/learning/algo/recursions/recursion
Breakpoint 1, factorial (n=1) at /home/lmwangi/learning/algo/recursions/recursion.c:50
50 return 1;
(gdb) bt
#0 factorial (n=1) at /home/lmwangi/learning/algo/recursions/recursion.c:50
#1 0x00000000004005cd in factorial (n=2) at /home/lmwangi/learning/algo/recursions/recursion.c:53
#2 0x00000000004005cd in factorial (n=3) at /home/lmwangi/learning/algo/recursions/recursion.c:53
#3 0x00000000004005cd in factorial (n=4) at /home/lmwangi/learning/algo/recursions/recursion.c:53
#4 0x00000000004005cd in factorial (n=5) at /home/lmwangi/learning/algo/recursions/recursion.c:53
#5 0x0000000000400544 in main () at /home/lmwangi/learning/algo/recursions/recursion.c:30
(gdb) step
56 }
(gdb) step
56 }
(gdb) step
56 }
(gdb) step
56 }
(gdb) step
56 }
(gdb) step
main () at /home/lmwangi/learning/algo/recursions/recursion.c:32
32 printf("Factorial %d\n", result);
- Tail recursion. Notice that we step only once since the function has all the results from prior operations
(gdb) cont
Continuing.
Factorial 120
Breakpoint 2, tail_factorial (n=1, a=120) at /home/lmwangi/learning/algo/recursions/recursion.c:68
68 return a;
(gdb) bt
#0 tail_factorial (n=1, a=120) at /home/lmwangi/learning/algo/recursions/recursion.c:68
#1 0x000000000040061c in tail_factorial (n=2, a=60) at /home/lmwangi/learning/algo/recursions/recursion.c:71
#2 0x000000000040061c in tail_factorial (n=3, a=20) at /home/lmwangi/learning/algo/recursions/recursion.c:71
#3 0x000000000040061c in tail_factorial (n=4, a=5) at /home/lmwangi/learning/algo/recursions/recursion.c:71
#4 0x000000000040061c in tail_factorial (n=5, a=1) at /home/lmwangi/learning/algo/recursions/recursion.c:71
#5 0x000000000040056d in main () at /home/lmwangi/learning/algo/recursions/recursion.c:34
(gdb) step
73 }
(gdb) step
main () at /home/lmwangi/learning/algo/recursions/recursion.c:36
36 printf("Tail Factorial %d\n", result);
(gdb)
Tuesday, May 4, 2010
Mod_auth_mysql Django Authentication
I needed a way to get mod_auth mysql to work with django password scheme so that I can have a single sign-on between apache and django. Unfortunately, Django uses a salted password scheme (hashtype$salt$Password) while mod_auth_mysql expects unsalted hashes. To take advantage of the salting advantages, it's better to update mod_auth_mysql to understand django's password scheme.
Well since blogger doesn't allow me to upload a patch file, here it goes
Patch for mod-auth-mysql-4.3.9
----
----
A dpatch is also available for debian users. Read more about dpatch here. Remember to update your 00list :)
Well since blogger doesn't allow me to upload a patch file, here it goes
Patch for mod-auth-mysql-4.3.9
----
--- mod_auth_mysql.c-pristine 2010-05-04 17:07:43.000000000 +0400
+++ mod_auth_mysql.c 2010-05-04 17:08:01.000000000 +0400
@@ -103,6 +103,8 @@
#endif
#define SHA1SUM_ENCRYPTION_FLAG 1<<6
+#define DJANGO_ENCRYPTION_FLAG 1<<8
+
static int check_no_encryption(const char *passwd, char *enc_passwd)
{
return (!strcmp(passwd, enc_passwd));
@@ -226,6 +228,86 @@
return (!strcmp(sha1_hex_hash(passwd), enc_passwd));
}
+static int check_django_encryption(const char *passwd, char *enc_passwd)
+{
+ char *delim = "$";
+ char *hash_type, *cp, *hash, *salt, *salted_passwd, *free_cp;
+ int ret;
+
+ // Since strtok alters the string it is parsing,
+ // we should always copy the string to a temporary buffer
+ cp = strdup (enc_passwd);
+
+ // strdup() function returns a pointer to the duplicated string, or
+ // NULL if insufficient memory was available
+ if (cp == NULL){
+ //TODO: Use the proper log for these errors. Not syslog
+ syslog(LOG_DEBUG, "Module_Auth_MySQL: Couldn't allocate memory for %s.\nExiting\n", cp);
+ return 0;
+ }
+ free_cp = cp;
+
+
+ //split the hash_type out
+ hash_type = strtok(cp, delim);
+ if (hash_type == NULL){
+ syslog(LOG_DEBUG, "Module_Auth_MySQL: Couldn't find %s in %s.\nExiting\n",delim, cp);
+ free(free_cp);
+ return 0;
+ }
+
+ //split the salt out
+ salt = strtok(NULL, delim);
+ if (salt == NULL){
+ syslog(LOG_DEBUG, "Module_Auth_MySQL: Couldn't find %s in %s.\nExiting\n",delim, cp);
+ free(free_cp);
+ return 0;
+ }
+
+ //split the salt out
+ hash = strtok(NULL, delim);
+ if (hash == NULL){
+ syslog(LOG_DEBUG, "Module_Auth_MySQL: Couldn't find %s in %s.\nExiting\n",delim, cp);
+ free(free_cp);
+ return 0;
+ }
+
+#ifdef DEBUG
+ syslog(LOG_DEBUG, "MAMDEBUG: DJANGO called: passwd:%s enc_password:%s", passwd, enc_passwd);
+ syslog(LOG_DEBUG, "MAMDEBUG: Hash method: %s\n", hash_type);
+ syslog(LOG_DEBUG, "MAMDEBUG: Salt: %s\n", salt);
+ syslog(LOG_DEBUG, "MAMDEBUG: Hash: %s\n", hash);
+#endif
+
+ // Concat salt and password.
+ salted_passwd = malloc(strlen(salt)+strlen(passwd)+1);
+ if (salted_passwd == NULL){
+ syslog(LOG_DEBUG, "Module_Auth_MySQL: Couldn't allocate memory for %s.\nExiting\n", salted_passwd);
+ free(free_cp);
+ return 0;
+ }
+ strcpy(salted_passwd, salt);
+ strcat(salted_passwd, passwd);
+
+ if( !strcmp(hash_type, "sha1") ) {
+ ret = (!strcmp(sha1_hex_hash(salted_passwd), hash));
+ }
+ else if( !strcmp(hash_type, "md5") ) {
+ ret = (!strcmp(md5_hex_hash(salted_passwd), hash));
+ }
+ else {
+ syslog(LOG_DEBUG, "Module_Auth_MySQL: Uknown salt type %s.", hash_type);
+ ret = 0;
+ }
+
+ //Free the strdup resource and the malloc resource
+ free(free_cp);
+ free(salted_passwd);
+
+ return ret;
+}
+
+
static int check_mysql_encryption(const char *passwd, char *enc_passwd)
{
@@ -254,6 +336,7 @@
{ "PHP_MD5", check_PHP_MD5_encryption, PHP_MD5_ENCRYPTION_FLAG },
{ "SHA1Sum", check_SHA1Sum_encryption, SHA1SUM_ENCRYPTION_FLAG},
/* add additional encryption types below */
+ { "Django", check_django_encryption, DJANGO_ENCRYPTION_FLAG},
{ NULL, NULL, 0 }
};
----
A dpatch is also available for debian users. Read more about dpatch here. Remember to update your 00list :)
Subscribe to:
Posts (Atom)