On 31.05.2023 12:40, Oleksii wrote: > On Tue, 2023-05-30 at 18:00 +0200, Jan Beulich wrote: >> On 29.05.2023 14:13, Oleksii Kurochko wrote: >>> +static uint32_t read_instr(unsigned long pc) >>> +{ >>> + uint16_t instr16 = *(uint16_t *)pc; >>> + >>> + if ( GET_INSN_LENGTH(instr16) == 2 ) >>> + return (uint32_t)instr16; >>> + else >>> + return *(uint32_t *)pc; >>> +} >> >> As long as this function is only used on Xen code, it's kind of okay. >> There you/we control whether code can change behind our backs. But as >> soon as you might use this on guest code, the double read is going to >> be a problem (I think; I wonder how hardware is supposed to deal with >> the situation: Maybe they indeed fetch in 16-bit quantities?). > I'll check how the hardware fetches instructions. > > I am trying to figure out why the double-read can be a problem. It > looks pretty safe to read 16 bits ( they will be available for any > instruction length with the assumption that the minimal instruction > length is 16 ), then check the length of the instruction, and if it is > 32-bit instruction, read it as uint32_t.
Simply consider what happens if a buggy or malicious entity changes the code between the two reads. And not just with the detection of "break" in mind that you use it for here. Jan