[PATCH] support for setting permissions on unix domain sockets

bremner at unb.ca bremner at unb.ca
Sun Oct 14 10:36:56 UTC 2007


The following patch adds a new command line option "-a" which takes an
octal permission mask (like chmod) sets the permissions on the unix
domain socket (specified by "-s").  I think this makes unix domain
sockets more useful from a security perspective, since it allows the
creation of a group to control access to a given memcached instance.

#! /bin/sh /usr/share/dpatch/dpatch-run
## umask-unix-socket.dpatch by  <David Bremner <bremner at unb.ca>>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: No description.

@DPATCH@
diff -urNad memcached-1.2.2~/memcached.c memcached-1.2.2/memcached.c
--- memcached-1.2.2~/memcached.c	2007-10-14 08:51:02.000000000 +0200
+++ memcached-1.2.2/memcached.c	2007-10-14 09:10:40.000000000 +0200
@@ -166,6 +166,7 @@
 }
 
 static void settings_init(void) {
+    settings.access=0700;
     settings.port = 11211;
     settings.udpport = 0;
     settings.interf.s_addr = htonl(INADDR_ANY);
@@ -2186,12 +2187,13 @@
     return sfd;
 }
 
-static int server_socket_unix(const char *path) {
+static int server_socket_unix(const char *path, int access_mask) {
     int sfd;
     struct linger ling = {0, 0};
     struct sockaddr_un addr;
     struct stat tstat;
     int flags =1;
+    int old_umask;
 
     if (!path) {
         return -1;
@@ -2221,11 +2223,14 @@
 
     addr.sun_family = AF_UNIX;
     strcpy(addr.sun_path, path);
+    old_umask=umask( ~(access_mask&0777));
     if (bind(sfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
         perror("bind()");
         close(sfd);
+        umask(old_umask);
         return -1;
     }
+    umask(old_umask);
     if (listen(sfd, 1024) == -1) {
         perror("listen()");
         close(sfd);
@@ -2475,8 +2480,13 @@
     setbuf(stderr, NULL);
 
     /* process arguments */
-    while ((c = getopt(argc, argv, "bp:s:U:m:Mc:khirvdl:u:P:f:s:n:t:D:")) != -1) {
+    while ((c = getopt(argc, argv, "a:bp:s:U:m:Mc:khirvdl:u:P:f:s:n:t:D:")) != -1) {
         switch (c) {
+        case 'a':
+            /* access for unix domain socket, as octal mask (like chmod)*/
+            settings.access= strtol(optarg,NULL,8);
+            break;
+
         case 'U':
             settings.udpport = atoi(optarg);
             break;
@@ -2654,7 +2664,7 @@
 
     /* create unix mode sockets after dropping privileges */
     if (settings.socketpath != NULL) {
-        l_socket = server_socket_unix(settings.socketpath);
+        l_socket = server_socket_unix(settings.socketpath,settings.access);
         if (l_socket == -1) {
             fprintf(stderr, "failed to listen\n");
             exit(EXIT_FAILURE);
diff -urNad memcached-1.2.2~/memcached.h memcached-1.2.2/memcached.h
--- memcached-1.2.2~/memcached.h	2007-05-03 00:58:51.000000000 +0200
+++ memcached-1.2.2/memcached.h	2007-10-14 09:09:38.000000000 +0200
@@ -74,6 +74,7 @@
     bool managed;          /* if 1, a tracker manages virtual buckets */
     int evict_to_free;
     char *socketpath;   /* path to unix socket if using local socket */
+    int access;  /* access mask (a la chmod) for unix domain socket */
     double factor;          /* chunk size growth factor */
     int chunk_size;
     int num_threads;        /* number of libevent threads to run */




More information about the memcached mailing list