From: Vladimír Čunát Date: Wed, 26 Apr 2017 12:53:49 +0000 (+0200) Subject: policy: purge pointer-casting where not necessary X-Git-Tag: 1.3.0-rc1~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3f60ad2dfb15381818f7c58e8709d7ea9479aee0;p=thirdparty%2Fknot-resolver.git policy: purge pointer-casting where not necessary 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. --- diff --git a/daemon/lua/kres.lua.in b/daemon/lua/kres.lua.in index 540c7f99f..f302a08a3 100644 --- a/daemon/lua/kres.lua.in +++ b/daemon/lua/kres.lua.in @@ -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 diff --git a/modules/policy/policy.lua b/modules/policy/policy.lua index 8385585fb..2c19ed854 100644 --- a/modules/policy/policy.lua +++ b/modules/policy/policy.lua @@ -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