Skip to content

Instantly share code, notes, and snippets.

@bow-fujita
Created March 19, 2015 08:09
Show Gist options
  • Save bow-fujita/a338e35b95605ad63147 to your computer and use it in GitHub Desktop.
Save bow-fujita/a338e35b95605ad63147 to your computer and use it in GitHub Desktop.
Patch for TokyoTyrant to fix UNIX socket issue.
diff -urN tokyotyrant-1.1.41.orig/ttutil.c tokyotyrant-1.1.41/ttutil.c
--- tokyotyrant-1.1.41.orig/ttutil.c 2010-08-04 23:24:33.000000000 -0700
+++ tokyotyrant-1.1.41/ttutil.c 2015-03-19 01:04:27.000000000 -0700
@@ -24,7 +24,6 @@
*************************************************************************************************/
-#define SOCKPATHBUFSIZ 108 // size of a socket path buffer
#define SOCKRCVTIMEO 0.25 // timeout of the recv call of socket
#define SOCKSNDTIMEO 0.25 // timeout of the send call of socket
#define SOCKCNCTTIMEO 5.0 // timeout of the connect call of socket
@@ -127,7 +126,7 @@
struct sockaddr_un saun;
memset(&saun, 0, sizeof(saun));
saun.sun_family = AF_UNIX;
- snprintf(saun.sun_path, SOCKPATHBUFSIZ, "%s", path);
+ strncpy(saun.sun_path, path, sizeof(saun.sun_path)-1);
int fd = socket(PF_UNIX, SOCK_STREAM, 0);
if(fd == -1) return -1;
int optint = 1;
@@ -187,7 +186,7 @@
struct sockaddr_un saun;
memset(&saun, 0, sizeof(saun));
saun.sun_family = AF_UNIX;
- snprintf(saun.sun_path, SOCKPATHBUFSIZ, "%s", path);
+ strncpy(saun.sun_path, path, sizeof(saun.sun_path)-1);
int fd = socket(PF_UNIX, SOCK_STREAM, 0);
if(fd == -1) return -1;
if(bind(fd, (struct sockaddr *)&saun, sizeof(saun)) != 0 ||
@bow-fujita
Copy link
Author

Starting TokyoTyrant with the following command doesn't work on Mac OS X:

$ ttserver -host /var/run/ttserver.sock -port 0 '*'
2015-03-18T23:51:09-08:00   SYSTEM  --------- logging started [11710] --------
2015-03-18T23:51:09-08:00   SYSTEM  server configuration: host=/var/run/ttserver.sock port=0
2015-03-18T23:51:09-08:00   SYSTEM  maximum connection: 8191
2015-03-18T23:51:09-08:00   SYSTEM  opening the database: *
Abort trap: 6

It's because of difference between SOCKPATHBUFSIZ and sizeof(sockaddr_un.sun_path).
struct sockaddr_un is defined in /usr/include/sys/un.h on Mac OS X as follow:

struct  sockaddr_un {
    unsigned char   sun_len;    /* sockaddr len including null */
    sa_family_t sun_family; /* [XSI] AF_UNIX */
    char        sun_path[104];  /* [XSI] path name (gag) */
};

So SOCKPATHBUFSIZ (=108) is greater than actual size of sockaddr_un.sun_path and it may cause buffer overflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment