Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segfault when using redis through a unix socket #146

Closed
timo-schluessler opened this issue May 8, 2023 · 0 comments
Closed

Segfault when using redis through a unix socket #146

timo-schluessler opened this issue May 8, 2023 · 0 comments

Comments

@timo-schluessler
Copy link

Null pointer dereference in db_redis_write because redisCommand returns NULL because the redis handle is in error state because redisEnableKeepalive fails when used with unix socket connections (see this note).

Possible fix:

diff --git a/src/database.c b/src/database.c
index 0451796..3773f90 100644
--- a/src/database.c
+++ b/src/database.c
@@ -184,6 +184,10 @@ static char* db_redis_read(database_t* db, const char* key)
     redisContext* handle = (redisContext*)db->handle;
     redisReply* reply = redisCommand(handle, "GET %s", buffer);
     char* value = NULL;
+    if (!reply) {
+        log_err("redis read error: %s", handle->errstring);
+        return value;
+    }
     if (reply->type == REDIS_REPLY_ERROR)
     {
         log_warn("redis read error: %s", reply->str);
@@ -205,6 +209,11 @@ static bool db_redis_write(database_t* db, const char* key, const char* value,
     bool success = true;
     redisReply* reply =
         redisCommand(handle, "SETEX %s %u %s", buffer, lifetime, value);
+    if (!reply)
+    {
+        log_err("redis write error: %s", handle->errstring);
+        return false;
+    }
     if (reply->type == REDIS_REPLY_ERROR)
     {
         log_warn("redis write error: %s", reply->str);
@@ -223,6 +232,7 @@ static void db_redis_disconnect(database_t* db)
 static bool db_redis_connect(database_t* db, const char* hostname, int port)
 {
     redisContext* handle;
+    bool is_unix = false;
     if (port > 0)
     {
         handle = redisConnect(hostname, port);
@@ -230,6 +240,7 @@ static bool db_redis_connect(database_t* db, const char* hostname, int port)
     else
     {
         handle = redisConnectUnix(hostname);
+        is_unix = true;
     }
     if (!handle)
     {
@@ -242,7 +253,14 @@ static bool db_redis_connect(database_t* db, const char* hostname, int port)
         redisFree(handle);
         return false;
     }
-    redisEnableKeepAlive(handle);
+    if (!is_unix) {
+        redisEnableKeepAlive(handle);
+        if (handle->err) {
+            log_error("failed to set keep alive on redis connection: %s", handle->errstr);
+            redisFree(handle);
+            return false;
+        }
+    }
     db->handle = handle;
     db->read = db_redis_read;
     db->write = db_redis_write;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant