]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
policy: purge pointer-casting where not necessary
authorVladimír Čunát <vladimir.cunat@nic.cz>
Wed, 26 Apr 2017 12:53:49 +0000 (14:53 +0200)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Thu, 1 Jun 2017 14:27:16 +0000 (16:27 +0200)
Casting is dangerous, e.g. it's easy to misconfigure policy with
  policy.add(policy.FORWARD('some address'))
which lead to segfault without showing any indication of the cause.
Now this case will show as
  .../policy.lua:98: 'struct kr_query' has no member named 'current'
and only abort the policy module instead of the whole process.

daemon/lua/kres.lua.in
modules/policy/policy.lua

index 540c7f99f866487739606ef8837100456faba18f..f302a08a31fd583e1353ce6f96dd9f4d115c7b98 100644 (file)
@@ -346,7 +346,7 @@ kres = {
        rcode = ffi.new('struct pkt_rcode'),
        query = query_flag,
        CONSUME = 1, PRODUCE = 2, DONE = 4, FAIL = 8, YIELD = 16,
-       -- Metatypes
+       -- Metatypes.  Beware that any pointer will be cast silently...
        pkt_t = function (udata) return ffi.cast('knot_pkt_t *', udata) end,
        request_t = function (udata) return ffi.cast('struct kr_request *', udata) end,
        -- Global API functions
index 8385585fb293fd852ffd4ceb1a2b6aae3aa163d6..2c19ed85415ac12658e468cb7bf28b39c7ef01a0 100644 (file)
@@ -55,7 +55,6 @@ local function mirror(target)
        if not sink then panic('MIRROR target %s is not a valid: %s', target, err) end
        return function(state, req)
                if state == kres.FAIL then return state end
-               req = kres.request_t(req)
                local query = req.qsource.packet
                if query ~= nil then
                        sink:send(ffi.string(query.wire, query.size))
@@ -76,7 +75,6 @@ local function stub(target)
                table.insert(list, addr2sock(target))
        end
        return function(state, req)
-               req = kres.request_t(req)
                local qry = req:current()
                -- Switch mode to stub resolver, do not track origin zone cut since it's not real authority NS
                qry.flags = bit.band(bit.bor(qry.flags, kres.query.STUB), bit.bnot(kres.query.ALWAYS_CUT))
@@ -97,7 +95,6 @@ local function forward(target)
                table.insert(list, addr2sock(target))
        end
        return function(state, req)
-               req = kres.request_t(req)
                local qry = req:current()
                req.options = bit.bor(bit.bor(req.options, kres.query.FORWARD), kres.query.NO_MINIMIZE)
                qry.flags = bit.band(bit.bor(qry.flags, kres.query.FORWARD), bit.bnot(kres.query.ALWAYS_CUT))
@@ -122,7 +119,6 @@ end
 -- Set and clear some query flags
 local function flags(opts_set, opts_clear)
        return function(state, req)
-               req = kres.request_t(req)
                local qry = req:current()
                qry.flags = bit.band(bit.bor(qry.flags, opts_set or 0), bit.bnot(opts_clear or 0))
                return nil -- chain rule