Rust solves this. (Fix use-after-free bug in Lua string handling)
Browse files
+20 −13 src/config/lua.zig
| @@ -743,7 +743,7 @@ fn lua_rule_add(state: ?*c.lua_State) callconv(.c) c_int { | |||
| 743 | fn lua_bar_set_font(state: ?*c.lua_State) callconv(.c) c_int { | 743 | fn lua_bar_set_font(state: ?*c.lua_State) callconv(.c) c_int { |
| 744 | const cfg = config orelse return 0; | 744 | const cfg = config orelse return 0; |
| 745 | const s = state orelse return 0; | 745 | const s = state orelse return 0; |
| 746 | - if (get_string_arg(s, 1)) |font| { | 746 | + if (dupe_lua_string(s, 1)) |font| { |
| 747 | cfg.font = font; | 747 | cfg.font = font; |
| 748 | } | 748 | } |
| 749 | return 0; | 749 | return 0; |
| @@ -784,7 +784,7 @@ fn parse_block_config(state: *c.lua_State, idx: c_int) ?Block { | |||
| 784 | c.lua_settop(state, -2); | 784 | c.lua_settop(state, -2); |
| 785 | 785 | ||
| 786 | _ = c.lua_getfield(state, idx, "format"); | 786 | _ = c.lua_getfield(state, idx, "format"); |
| 787 | - const format = get_lua_string(state, -1) orelse ""; | 787 | + const format = dupe_lua_string(state, -1) orelse ""; |
| 788 | c.lua_settop(state, -2); | 788 | c.lua_settop(state, -2); |
| 789 | 789 | ||
| 790 | _ = c.lua_getfield(state, idx, "interval"); | 790 | _ = c.lua_getfield(state, idx, "interval"); |
| @@ -812,17 +812,17 @@ fn parse_block_config(state: *c.lua_State, idx: c_int) ?Block { | |||
| 812 | } else if (std.mem.eql(u8, block_type_str, "DateTime")) { | 812 | } else if (std.mem.eql(u8, block_type_str, "DateTime")) { |
| 813 | block.block_type = .datetime; | 813 | block.block_type = .datetime; |
| 814 | _ = c.lua_getfield(state, idx, "__arg"); | 814 | _ = c.lua_getfield(state, idx, "__arg"); |
| 815 | - block.datetime_format = get_lua_string(state, -1); | 815 | + block.datetime_format = dupe_lua_string(state, -1); |
| 816 | c.lua_settop(state, -2); | 816 | c.lua_settop(state, -2); |
| 817 | } else if (std.mem.eql(u8, block_type_str, "Shell")) { | 817 | } else if (std.mem.eql(u8, block_type_str, "Shell")) { |
| 818 | block.block_type = .shell; | 818 | block.block_type = .shell; |
| 819 | _ = c.lua_getfield(state, idx, "__arg"); | 819 | _ = c.lua_getfield(state, idx, "__arg"); |
| 820 | - block.command = get_lua_string(state, -1); | 820 | + block.command = dupe_lua_string(state, -1); |
| 821 | c.lua_settop(state, -2); | 821 | c.lua_settop(state, -2); |
| 822 | } else if (std.mem.eql(u8, block_type_str, "Static")) { | 822 | } else if (std.mem.eql(u8, block_type_str, "Static")) { |
| 823 | block.block_type = .static; | 823 | block.block_type = .static; |
| 824 | _ = c.lua_getfield(state, idx, "__arg"); | 824 | _ = c.lua_getfield(state, idx, "__arg"); |
| 825 | - if (get_lua_string(state, -1)) |text| { | 825 | + if (dupe_lua_string(state, -1)) |text| { |
| 826 | block.format = text; | 826 | block.format = text; |
| 827 | } | 827 | } |
| 828 | c.lua_settop(state, -2); | 828 | c.lua_settop(state, -2); |
| @@ -831,19 +831,19 @@ fn parse_block_config(state: *c.lua_State, idx: c_int) ?Block { | |||
| 831 | _ = c.lua_getfield(state, idx, "__arg"); | 831 | _ = c.lua_getfield(state, idx, "__arg"); |
| 832 | if (c.lua_type(state, -1) == c.LUA_TTABLE) { | 832 | if (c.lua_type(state, -1) == c.LUA_TTABLE) { |
| 833 | _ = c.lua_getfield(state, -1, "charging"); | 833 | _ = c.lua_getfield(state, -1, "charging"); |
| 834 | - block.format_charging = get_lua_string(state, -1); | 834 | + block.format_charging = dupe_lua_string(state, -1); |
| 835 | c.lua_settop(state, -2); | 835 | c.lua_settop(state, -2); |
| 836 | 836 | ||
| 837 | _ = c.lua_getfield(state, -1, "discharging"); | 837 | _ = c.lua_getfield(state, -1, "discharging"); |
| 838 | - block.format_discharging = get_lua_string(state, -1); | 838 | + block.format_discharging = dupe_lua_string(state, -1); |
| 839 | c.lua_settop(state, -2); | 839 | c.lua_settop(state, -2); |
| 840 | 840 | ||
| 841 | _ = c.lua_getfield(state, -1, "full"); | 841 | _ = c.lua_getfield(state, -1, "full"); |
| 842 | - block.format_full = get_lua_string(state, -1); | 842 | + block.format_full = dupe_lua_string(state, -1); |
| 843 | c.lua_settop(state, -2); | 843 | c.lua_settop(state, -2); |
| 844 | 844 | ||
| 845 | _ = c.lua_getfield(state, -1, "battery_name"); | 845 | _ = c.lua_getfield(state, -1, "battery_name"); |
| 846 | - block.battery_name = get_lua_string(state, -1); | 846 | + block.battery_name = dupe_lua_string(state, -1); |
| 847 | c.lua_settop(state, -2); | 847 | c.lua_settop(state, -2); |
| 848 | } | 848 | } |
| 849 | c.lua_settop(state, -2); | 849 | c.lua_settop(state, -2); |
| @@ -988,7 +988,7 @@ fn create_block_table(state: *c.lua_State, block_type: [*:0]const u8, arg: ?[]co | |||
| 988 | fn lua_set_terminal(state: ?*c.lua_State) callconv(.c) c_int { | 988 | fn lua_set_terminal(state: ?*c.lua_State) callconv(.c) c_int { |
| 989 | const cfg = config orelse return 0; | 989 | const cfg = config orelse return 0; |
| 990 | const s = state orelse return 0; | 990 | const s = state orelse return 0; |
| 991 | - if (get_string_arg(s, 1)) |term| { | 991 | + if (dupe_lua_string(s, 1)) |term| { |
| 992 | cfg.terminal = term; | 992 | cfg.terminal = term; |
| 993 | } | 993 | } |
| 994 | return 0; | 994 | return 0; |
| @@ -1025,7 +1025,7 @@ fn lua_set_tags(state: ?*c.lua_State) callconv(.c) c_int { | |||
| 1025 | var i: usize = 0; | 1025 | var i: usize = 0; |
| 1026 | while (i < len and i < 9) : (i += 1) { | 1026 | while (i < len and i < 9) : (i += 1) { |
| 1027 | _ = c.lua_rawgeti(s, 1, @intCast(i + 1)); | 1027 | _ = c.lua_rawgeti(s, 1, @intCast(i + 1)); |
| 1028 | - if (get_lua_string(s, -1)) |tag_str| { | 1028 | + if (dupe_lua_string(s, -1)) |tag_str| { |
| 1029 | cfg.tags[i] = tag_str; | 1029 | cfg.tags[i] = tag_str; |
| 1030 | } | 1030 | } |
| 1031 | c.lua_settop(s, -2); | 1031 | c.lua_settop(s, -2); |
| @@ -1037,7 +1037,7 @@ fn lua_set_tags(state: ?*c.lua_State) callconv(.c) c_int { | |||
| 1037 | fn lua_autostart(state: ?*c.lua_State) callconv(.c) c_int { | 1037 | fn lua_autostart(state: ?*c.lua_State) callconv(.c) c_int { |
| 1038 | const cfg = config orelse return 0; | 1038 | const cfg = config orelse return 0; |
| 1039 | const s = state orelse return 0; | 1039 | const s = state orelse return 0; |
| 1040 | - if (get_string_arg(s, 1)) |cmd| { | 1040 | + if (dupe_lua_string(s, 1)) |cmd| { |
| 1041 | cfg.add_autostart(cmd) catch return 0; | 1041 | cfg.add_autostart(cmd) catch return 0; |
| 1042 | } | 1042 | } |
| 1043 | return 0; | 1043 | return 0; |
| @@ -1054,7 +1054,7 @@ fn lua_set_layout_symbol(state: ?*c.lua_State) callconv(.c) c_int { | |||
| 1054 | const cfg = config orelse return 0; | 1054 | const cfg = config orelse return 0; |
| 1055 | const s = state orelse return 0; | 1055 | const s = state orelse return 0; |
| 1056 | const name = get_string_arg(s, 1) orelse return 0; | 1056 | const name = get_string_arg(s, 1) orelse return 0; |
| 1057 | - const symbol = get_string_arg(s, 2) orelse return 0; | 1057 | + const symbol = dupe_lua_string(s, 2) orelse return 0; |
| 1058 | 1058 | ||
| 1059 | const layout_map = .{ | 1059 | const layout_map = .{ |
| 1060 | .{ "tiling", &cfg.layout_tile_symbol }, | 1060 | .{ "tiling", &cfg.layout_tile_symbol }, |
| @@ -1158,6 +1158,13 @@ fn get_lua_string(state: *c.lua_State, idx: c_int) ?[]const u8 { | |||
| 1158 | return std.mem.span(cstr); | 1158 | return std.mem.span(cstr); |
| 1159 | } | 1159 | } |
| 1160 | 1160 | ||
| 1161 | +fn dupe_lua_string(state: *c.lua_State, idx: c_int) ?[]const u8 { | ||
| 1162 | + const cfg = config orelse return null; | ||
| 1163 | + const lua_str = get_lua_string(state, idx) orelse return null; | ||
| 1164 | + const duped = cfg.allocator.dupe(u8, lua_str) catch return null; | ||
| 1165 | + return duped; | ||
| 1166 | +} | ||
| 1167 | + | ||
| 1161 | fn parse_color(state: *c.lua_State, idx: c_int) u32 { | 1168 | fn parse_color(state: *c.lua_State, idx: c_int) u32 { |
| 1162 | const lua_type = c.lua_type(state, idx); | 1169 | const lua_type = c.lua_type(state, idx); |
| 1163 | if (lua_type == c.LUA_TNUMBER) { | 1170 | if (lua_type == c.LUA_TNUMBER) { |