[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] xen/livepatch: Fix .altinstructions safety checks


  • To: Jan Beulich <jbeulich@xxxxxxxx>
  • From: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
  • Date: Mon, 17 Apr 2023 11:45:56 +0100
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=citrix.com; dmarc=pass action=none header.from=citrix.com; dkim=pass header.d=citrix.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=OuGMmpMgVv3Sqli+PmWRBIzdFVx7DqqibqKW7hrQ+I0=; b=cnbu78Gs8VoKo5sFkhVAaLSGWZVRCeVYzMRmrEm2UAwji9UYdA8TuCILmFp282zObBKRpAlB4kzB/LmFDnp5J7v2Z1CbvqTZNetoy8tZMPQTeg+1sh1GvWHwrEpO4UKrjkaFSOP96i3NxeUnREUXGuGFq83zxq4INUXNfuDMSsWPZ/pgAfqGxZFDkXuy208U9aaU+qwknHqeeMTjE0/KojByvWq1lzQIhJWZENjbHQcW1OcKaXDZsOQVMlSO9XbfhNPoSagtljkCoECLBPxON18K7cQ1La5w0w5TRUMmUzKOONAcIIIG39Qgo4tCy2Gl83mvB3c98dlRMrN9plNHFg==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fk8N8LAtgO8M0XRmaprDASx9eRdubCQk0BqvV0rtNbO5f5xaKlpcqJBDek1ycJnB4vn6PnqV7pTDv4qNS9hl9a+JEhGSznlMFGwE+mi4KsaT462djFhGJngHo8i3JmZ1T28ipPGhePUFVIE2TgrDVXkXsvwj4wS9m+7demaYCHmu2RjoaJui/csDN/TYhVKP26U0dKQuU34RuW4uHdmJm1QBp6qerh6vUve7Wa12mO2rv/z1qwIl/E15o5Q2czazUwzxJTOBCqUN9P6aW77QWQrMqu7WBYVPP8E3V6LvElC39Icoy9DGmVb5DHmL0vaBvKIJWxwPfApf6xqYMXbZHQ==
  • Authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=citrix.com;
  • Cc: Konrad Rzeszutek Wilk <konrad.wilk@xxxxxxxxxx>, Ross Lagerwall <ross.lagerwall@xxxxxxxxxx>, Xen-devel <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • Delivery-date: Mon, 17 Apr 2023 10:46:19 +0000
  • Ironport-data: A9a23:fXYJUqmGAA4CsYPhyLjyVj7o5gywJ0RdPkR7XQ2eYbSJt1+Wr1Gzt xIWWm7Qa/veYGPyfNklPIix9EoOuMKBytU2S1c+pHwwRCMWpZLJC+rCIxarNUt+DCFhoGFPt JxCN4aafKjYaleG+39B55C49SEUOZmgH+a6U6icfHgqH2eIcQ954Tp7gek1n4V0ttawBgKJq LvartbWfVSowFaYCEpNg064gE4p7aWaVA8w5ARkPqgX5QGGzRH5MbpETU2PByqgKmVrNrbSq 9brlNmR4m7f9hExPdKp+p6TnpoiG+O60aCm0xK6aoD66vRwjnVaPpUTbZLwXXx/mTSR9+2d/ f0W3XCGpaXFCYWX8AgVe0Ew/yiTpsSq8pefSZS0mZT7I0Er7xIAahihZa07FdRwxwp5PY1B3 fcGKTEgfy+xvMm7g5WgEu10qMgjE/C+aevzulk4pd3YJdAPZMmbBo/suppf1jp2gd1SF/HDY cZfcSBocBnLfxxIPBEQFY46m+CrwHL4dlW0qnrM/fZxvzeVk1Q3ieCwWDbWUoXiqcF9t0CUv G/ZuU/+BQkXLoe3wjuZ6HO8wOTImEsXXapLTODorKY33wb7Kmo7KRNNXGC7p9WCh2mCd+BzJ 0I5yyY0ov1nnKCsZpynN/Gim1aGtBMBX9tbE8Uh9RqAjKHT5m6xBW8CVDdNLsMnsMweQiYj3 VuE2djuAFRHoLCTDH6Q6LqQhTezIjQOa38PYzceSgkI6MWlp5s85i8jVf5mGa+xy9fzRjf5x mnSqDBk3u1Oy8kWy6+84FbLxSq2oYTERRI04QORWX+56gR+Z8iuYInABUXn0Mus5b2xFjGp1 EXoUeDEhAzSJflhTBCwfdg=
  • Ironport-hdrordr: A9a23:L5MM/qEknJWYyNzypLqFcJHXdLJyesId70hD6qkvc3Fom52j/f xGws5x6fatskd2ZJkh8erhBEDyewKkyXcV2/hnAV7MZniDhILFFu9fBOjZskvd8k/Fh4lgPM 5bGsATZ+EYZmIK7voSlTPIdurIt+P3kpxA692+815dCSVRL41w5QZwDQiWVmdsQhNdOJY/HJ 2AouJaujuJYx0sH4yGL0hAe9KGi8zAlZrgbxJDLQUg8hOygTSh76O/OwSE3y0ZTyhEzd4ZgC P4ek3Cl++eWsOAu1PhPlzonttrcRzau5V+7fm3+4Uow/PX+0eVjcpaKv2/VXsO0ZmSAR4R4a LxSlEbTo1OAjrqDxuIiAqo1A/63Dk07Xj+jVeenHv4uMT8ACk3EsxbmOtiA2nkAmcbzaFBOZ hwrhGknosSCQmFkDX25tDOWR0vnk2ooWA6mepWi3BES4MRZLJYsIRapSpuYeM9NTO/7JpiHP hlDcna6voTeVSGb2rBtm0qxNC3RHw8EhqPX0BHsM2I1Dpdmmx/0iIjtbkit2ZF8Ih4R4hP5u zCPKgtnLZSTtUOZaY4H+sFSdvfMB29ffsNChPtHb3KLtB5B5uWke+L3Fwc3pDXRKA1
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On 17/04/2023 11:01 am, Jan Beulich wrote:
> On 15.04.2023 04:22, Andrew Cooper wrote:
>> The prior check has && vs || mixups, making it tautologically false and thus
>> providing no safety at all.  There are boundary errors too.
>>
>> First start with a comment describing how the .altinstructions and
>> .altinstr_replacement sections interact, and perform suitable cross-checking.
>>
>> Second, rewrite the alt_instr loop entirely from scratch.  Origin sites have
>> non-zero size, and must be fully contained within .text.
> Or .init.text, which may be worth making explicit (perhaps also in the
> respective code comment). Or am I misremembering and livepatch blobs,
> unlike e.g. Linux modules, don't support the concept of .init.* sections?

Here, we're talking strictly about the .alt* and .text of the livepatch
itself.

I suppose it would be nice if the safety check / other one-time hooks
could live in a local .init.text for the livepatch itself, but we don't
have this concept at the moment.

>
>>  Any non-zero sized
>> replacements must be fully contained within .altinstr_replacement.
> Yes, but if they're all zero-sized, in principle no .altinstr_replacement
> section could be there. Not sure though whether that's worth supporting
> as a special case.

This is discussed in the source code comment.

Right now, all zero-length replacements reference the
altinstr_replacement section, so the section is present even if it's not
got any data in it.

If this changes in the future, we can accommodate.

> Furthermore, ...
>
>> Fixes: f8a10174e8b1 ("xsplice: Add support for alternatives")
>> Signed-off-by: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
>> ---
>> CC: Konrad Rzeszutek Wilk <konrad.wilk@xxxxxxxxxx>
>> CC: Ross Lagerwall <ross.lagerwall@xxxxxxxxxx>
>>
>> As a further observation, .altinstr_replacement shouldn't survive beyond its
>> use in apply_alternatives(), but the disp32 relative references (for x86 at
>> least) in alt_instr force .altinstr_replacement to be close to the payload
>> while being applied.
> ... if .altinstr_replacement is retained right now anyway, isn't it legal
> to fold it with another section (e.g. .text) while linking?

Why would it be?

We're auditing what the current tools actually produce, not any
arbitrary theoretical one.

>
>> --- a/xen/common/livepatch.c
>> +++ b/xen/common/livepatch.c
>> @@ -803,28 +803,82 @@ static int prepare_payload(struct payload *payload,
>>      if ( sec )
>>      {
>>  #ifdef CONFIG_HAS_ALTERNATIVE
>> +        /*
>> +         * (As of April 2023), Alternatives are formed of:
>> +         * - An .altinstructions section with an array of struct 
>> alt_instr's.
>> +         * - An .altinstr_replacement section containing instructions bytes.
> Since this is generic code, perhaps drop "bytes"? (Or else use "instruction
> bytes"?)

Technically bytes is ok here, but yeah - it will be slightly better without.

>> +         * An individual alt_instr contains:
>> +         * - An orig reference, pointing into .text with a nonzero length
>> +         * - A repl reference, pointing into .altinstr_replacement
>> +         *
>> +         * It is legal to have zero-length replacements, meaning it is legal
>> +         * for the .altinstr_replacement section to be empty too.  An
>> +         * implementation detail means that a zero-length replacement's repl
>> +         * reference will be the start of the .altinstr_replacement section.
> "will" or "may"? And especially if indeed "will", is it really worth 
> mentioning
> this here in this way, posing a fair risk of the comment going stale entirely
> unnoticed?

Hmm.  Thinking about it, I expect that it's not actually always the start.

The code uses pushsection/ref/{repl bytes}/popsection, so for an empty
replacement it will probably reference the end of the previously
replacement.

I should tweak the comment, but the logic is fine.  All I check is that
[repl, size) is entirely within altinstr_replacement, with no special
case at 0.

>
>> +         */
>> +        const struct livepatch_elf_sec *repl_sec;
>>          struct alt_instr *a, *start, *end;
>>  
>>          if ( !section_ok(elf, sec, sizeof(*a)) )
>>              return -EINVAL;
>>  
>> +        /* Tolerate an empty .altinstructions section... */
>> +        if ( sec->sec->sh_size == 0 )
>> +            goto alt_done;
>> +
>> +        /* ... but otherwise, there needs to be something to alter... */
>> +        if ( payload->text_size == 0 )
>> +        {
>> +            printk(XENLOG_ERR LIVEPATCH "%s Alternatives provided, but no 
>> .text\n",
>> +                   elf->name);
>> +            return -EINVAL;
>> +        }
>> +
>> +        /* ... and something to be altered to. */
>> +        repl_sec = livepatch_elf_sec_by_name(elf, ".altinstr_replacement");
>> +        if ( !repl_sec )
>> +        {
>> +            printk(XENLOG_ERR LIVEPATCH "%s .altinstructions provided, but 
>> no .altinstr_replacement\n",
>> +                   elf->name);
>> +            return -EINVAL;
>> +        }
>> +
>>          start = sec->load_addr;
>>          end = sec->load_addr + sec->sec->sh_size;
>>  
>>          for ( a = start; a < end; a++ )
>>          {
>> -            const void *instr = ALT_ORIG_PTR(a);
>> -            const void *replacement = ALT_REPL_PTR(a);
>> +            const void *orig = ALT_ORIG_PTR(a);
>> +            const void *repl = ALT_REPL_PTR(a);
>> +
>> +            /* orig must be fully within .text. */
>> +            if ( orig               < payload->text_addr ||
>> +                 a->orig_len        > payload->text_size ||
>> +                 orig + a->orig_len > payload->text_addr + 
>> payload->text_size )
>> +            {
>> +                printk(XENLOG_ERR LIVEPATCH
>> +                       "%s Alternative orig %p+%#x outside payload text 
>> %p+%#lx\n",
>> +                       elf->name, orig, a->orig_len, payload->text_addr, 
>> payload->text_size);
>> +                return -EINVAL;
>> +            }
>>  
>> -            if ( (instr < region->start && instr >= region->end) ||
>> -                 (replacement < region->start && replacement >= 
>> region->end) )
>> +            /*
>> +             * repl must be fully within .altinstr_replacement, even if they
>> +             * happen to both have zero length.
> Who is "they ... both" here? Surely it doesn't matter here whether "orig_len"
> is zero.

I haven't explicitly rejected it, but an orig_len of 0 is an error.

"they" is repl_len and altinstr_replacement.


And FYI, I need to repost this as part of a 3-patch series in order to
not break the ARM build.

~Andrew



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.