From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43661) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fOhY5-0001Lg-Mw for qemu-devel@nongnu.org; Fri, 01 Jun 2018 06:44:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fOhY4-0005WZ-QU for qemu-devel@nongnu.org; Fri, 01 Jun 2018 06:44:45 -0400 Received: from mail-wm0-x244.google.com ([2a00:1450:400c:c09::244]:34584) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fOhY4-0005WE-Jf for qemu-devel@nongnu.org; Fri, 01 Jun 2018 06:44:44 -0400 Received: by mail-wm0-x244.google.com with SMTP id q4-v6so5978544wmq.1 for ; Fri, 01 Jun 2018 03:44:44 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <20180529220338.10879-1-jusual@mail.ru> <20180529220338.10879-3-jusual@mail.ru> From: Stefan Hajnoczi Date: Fri, 1 Jun 2018 11:44:43 +0100 Message-ID: Content-Type: text/plain; charset="UTF-8" Subject: Re: [Qemu-devel] [RFC 2/3] hw/char/nrf51_uart: Implement nRF51 SoC UART List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: sundeep subbaraya Cc: Julia Suvorova , qemu-devel , Peter Maydell , Jim Mussared , =?UTF-8?Q?Steffen_G=C3=B6rtz?= , Joel Stanley On Fri, Jun 1, 2018 at 11:41 AM, Stefan Hajnoczi wrote: > On Thu, May 31, 2018 at 2:58 PM, sundeep subbaraya > wrote: >> On Wed, May 30, 2018 at 3:33 AM, Julia Suvorova via Qemu-devel >> wrote: >>> +static uint64_t uart_read(void *opaque, hwaddr addr, unsigned int size) >>> +{ >>> + Nrf51UART *s = NRF51_UART(opaque); >>> + uint64_t r; >>> + >>> + switch (addr) { >>> + case A_RXD: >>> + r = s->rx_fifo[s->rx_fifo_pos]; >>> + if (s->rx_fifo_len > 0) { >>> + s->rx_fifo_pos = (s->rx_fifo_pos + 1) % UART_FIFO_LENGTH; >>> + s->rx_fifo_len--; >>> + qemu_chr_fe_accept_input(&s->chr); >>> + } >>> + break; >>> + >>> + case A_INTENSET: >>> + case A_INTENCLR: >>> + case A_INTEN: >>> + r = s->reg[A_INTEN]; >>> + break; >>> + default: >>> + r = s->reg[addr]; >> >> You can use R_* macros for registers and access regs[ ] with addr/4 as index. >> It is better than using big regs[ ] array out of which most of >> locations go unused. > > Good point. The bug is more severe than an inefficiency. > s->reg[addr] allows out-of-bounds accesses. This is a security bug. > > The memory region is 0x1000 *bytes* long, but the array has 0x1000 > 32-bit *elements*. A read from address 0xfffc results in a memory > load from s->reg + 0xfffc * sizeof(s->reg[0]). That's beyond the end > of the array! Sorry, I was wrong. The array is large enough after all. It's just an inefficiency, but still worth fixing. Similar issues could lead to out-of-bound accesses. Stefan