]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Only try to execute a faulty Lua FFI per-thread code once
authorRemi Gacogne <remi.gacogne@powerdns.com>
Wed, 30 Jun 2021 16:16:36 +0000 (18:16 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Wed, 30 Jun 2021 16:16:36 +0000 (18:16 +0200)
pdns/dnsdist-lua-actions.cc
pdns/dnsdistdist/dnsdist-rules.hh

index f83d9a9ab71d664fe8e208e131bce95b0dbde24c..aadaa3c008af15e8efabb593f12f862b750f279c 100644 (file)
@@ -504,8 +504,15 @@ public:
       auto& state = t_perThreadStates[d_functionID];
       if (!state.d_initialized) {
         setupLuaFFIPerThreadContext(state.d_luaContext);
-        state.d_func = state.d_luaContext.executeCode<func_t>(d_functionCode);
+        /* mark the state as initialized first so if there is a syntax error
+           we only try to execute the code once */
         state.d_initialized = true;
+        state.d_func = state.d_luaContext.executeCode<func_t>(d_functionCode);
+      }
+
+      if (!state.d_func) {
+        /* the function was not properly initialized */
+        return DNSAction::Action::None;
       }
 
       dnsdist_ffi_dnsquestion_t dqffi(dq);
@@ -520,9 +527,11 @@ public:
         }
       }
       return static_cast<DNSAction::Action>(ret);
-    } catch (const std::exception &e) {
+    }
+    catch (const std::exception &e) {
       warnlog("LuaFFIPerThreadAction failed inside Lua, returning ServFail: %s", e.what());
-    } catch (...) {
+    }
+    catch (...) {
       warnlog("LuaFFIPerthreadAction failed inside Lua, returning ServFail: [unknown exception]");
     }
     return DNSAction::Action::ServFail;
@@ -616,8 +625,15 @@ public:
       auto& state = t_perThreadStates[d_functionID];
       if (!state.d_initialized) {
         setupLuaFFIPerThreadContext(state.d_luaContext);
-        state.d_func = state.d_luaContext.executeCode<func_t>(d_functionCode);
+        /* mark the state as initialized first so if there is a syntax error
+           we only try to execute the code once */
         state.d_initialized = true;
+        state.d_func = state.d_luaContext.executeCode<func_t>(d_functionCode);
+      }
+
+      if (!state.d_func) {
+        /* the function was not properly initialized */
+        return DNSResponseAction::Action::None;
       }
 
       dnsdist_ffi_dnsquestion_t dqffi(dq);
@@ -632,9 +648,11 @@ public:
         }
       }
       return static_cast<DNSResponseAction::Action>(ret);
-    } catch (const std::exception &e) {
+    }
+    catch (const std::exception &e) {
       warnlog("LuaFFIPerThreadResponseAction failed inside Lua, returning ServFail: %s", e.what());
-    } catch (...) {
+    }
+    catch (...) {
       warnlog("LuaFFIPerthreadResponseAction failed inside Lua, returning ServFail: [unknown exception]");
     }
     return DNSResponseAction::Action::ServFail;
index bbbcabe0a9c68a27c8e1a5be27cd74941600ac3d..2c3960df1b2f2718622cd8952d2280bd1090e2af 100644 (file)
@@ -1193,15 +1193,24 @@ public:
       auto& state = t_perThreadStates[d_functionID];
       if (!state.d_initialized) {
         setupLuaFFIPerThreadContext(state.d_luaContext);
-        state.d_func = state.d_luaContext.executeCode<func_t>(d_functionCode);
+        /* mark the state as initialized first so if there is a syntax error
+           we only try to execute the code once */
         state.d_initialized = true;
+        state.d_func = state.d_luaContext.executeCode<func_t>(d_functionCode);
+      }
+
+      if (!state.d_func) {
+        /* the function was not properly initialized */
+        return false;
       }
 
       dnsdist_ffi_dnsquestion_t dqffi(const_cast<DNSQuestion*>(dq));
       return state.d_func(&dqffi);
-    } catch (const std::exception &e) {
+    }
+    catch (const std::exception &e) {
       warnlog("LuaFFIPerthreadRule failed inside Lua: %s", e.what());
-    } catch (...) {
+    }
+    catch (...) {
       warnlog("LuaFFIPerThreadRule failed inside Lua: [unknown exception]");
     }
     return false;