Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp863032yba; Wed, 24 Apr 2019 10:49:22 -0700 (PDT) X-Google-Smtp-Source: APXvYqy0GpBrHyIpo/KQzge42/16paAlM2bTSuIwzFXATYybD2Xcdht36aVC/Mlq5QHNzxYbt/Iw X-Received: by 2002:a63:cc48:: with SMTP id q8mr2950874pgi.202.1556128161977; Wed, 24 Apr 2019 10:49:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556128161; cv=none; d=google.com; s=arc-20160816; b=zUDuzeCPs1s6p+54t9WGuIIjIY1iaukvvzXQtIW3Cyfm51UmdVI0q7IEaPx6tR5zDl s5lX7VQVFn92+0G682r0iWbmrktM0p75LxF8w8lmk0KDU54ElqB3eGZrrVlkB84UdPcJ D7ycpJ/QYuLT6DsnGJlB1IaCRxxqTne6KmYBOTlnHhxoMOg+arSNawIX7WtTu2S00Frs tSC6lwkFILRGNFZRAoCQV1CecqXGUTYrFc4HFokH0BfWfaRO3C12e5ioU8zYoJUBto9/ QeQ+nB7cwsUPGYfVF66Ymjp08nKJCmavJvccF3I3uLB+jNpu34h3wtB5b8Koo+65/hXZ +xUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date; bh=oqk0wz3Hu7eoEgftkCTsLWujH1LKsv6wsk1leju+shE=; b=IJdZDARV9zo1Ve4ItP+yv1tgm8hBfXJuWROo57k7Lqi5UW6I8t6FfANGc8N8k4pgJL 2MyhH2OeXFUrvPGLeHUzsoOzE9tn0yaWpT5DjwYaAc+av/rH1EEcmto+fUtpG8tWwWbX rdCULyNjiBu4ODjRlmteYyuAgqDoZ+ZVOkKKTf9K7SmZZYAvFcBBnq23R3G/wwnQkZaR LQoW3NrHPJQAv7j+0w05FnZx2Dgttd6JZAEFAQKx0pu8kCrC2xX9R2g52QSCiXZvG9tJ U9HRWFQ6UraZoEjxNvAVLu4jeP+eu3BBy+yEcsSHbbaV4fpw8zkdN4iC0Zxf59MbOJkl 6LUA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 3si20526668plo.300.2019.04.24.10.49.06; Wed, 24 Apr 2019 10:49:21 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391952AbfDXRrc (ORCPT + 99 others); Wed, 24 Apr 2019 13:47:32 -0400 Received: from mx2.suse.de ([195.135.220.15]:43948 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2391156AbfDXRrb (ORCPT ); Wed, 24 Apr 2019 13:47:31 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 03243AE58; Wed, 24 Apr 2019 17:47:29 +0000 (UTC) Date: Wed, 24 Apr 2019 19:47:27 +0200 (CEST) From: Miroslav Benes To: Joe Lawrence cc: linux-kernel@vger.kernel.org, live-patching@vger.kernel.org, linux-kbuild@vger.kernel.org, Jessica Yu , Jiri Kosina , Joao Moreira , Josh Poimboeuf , Konstantin Khlebnikov , Masahiro Yamada , Michael Matz , Nicolai Stange , Petr Mladek Subject: Re: [PATCH v3 3/9] livepatch: Add klp-convert tool In-Reply-To: <20190423203529.GA15743@redhat.com> Message-ID: References: <20190410155058.9437-1-joe.lawrence@redhat.com> <20190410155058.9437-4-joe.lawrence@redhat.com> <20190423203529.GA15743@redhat.com> User-Agent: Alpine 2.21 (LSU 202 2017-01-01) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 23 Apr 2019, Joe Lawrence wrote: > On Wed, Apr 10, 2019 at 11:50:52AM -0400, Joe Lawrence wrote: > > > > [ ... snip ... ] > > > > +static bool convert_rela(struct section *oldsec, struct rela *r, > > + struct sympos *sp, struct elf *klp_elf) > > +{ > > + struct section *sec; > > + struct rela *r1, *r2; > > + > > + sec = get_or_create_klp_rela_section(oldsec, sp, klp_elf); > > + if (!sec) { > > + WARN("Can't create or access klp.rela section (%s.%s)\n", > > + sp->object_name, sp->symbol_name); > > + return false; > > + } > > + > > + if (!convert_klp_symbol(r->sym, sp)) { > > + WARN("Unable to convert symbol name (%s.%s)\n", sec->name, > > + r->sym->name); > > + return false; > > + } > > + > > + /* Move the converted rela to klp rela section */ > > + list_for_each_entry_safe(r1, r2, &oldsec->relas, list) { > > + if (r1->sym->name == r->sym->name) { > > + list_del(&r1->list); > > + list_add(&r1->list, &sec->relas); > > + } > > + } > > + return true; > > +} > > This one took a while to find and debug, but I believe that > convert_rela()'s list removal is not as safe as it thinks it is. > > Start with its calling context from main() below: > > > + list_for_each_entry_safe(sec, aux, &klp_elf->sections, list) { > > + if (!is_rela_section(sec)) > > + continue; > > + > > + list_for_each_entry_safe(rela, tmprela, &sec->relas, list) { > > + if (!must_convert(rela->sym)) > > + continue; > > + > > + if (!find_missing_position(rela->sym, &sp)) { > > + WARN("Unable to find missing symbol: %s", > > + rela->sym->name); > > + return -1; > > + } > > + if (!convert_rela(sec, rela, &sp, klp_elf)) { > > + WARN("Unable to convert relocation: %s", > > + rela->sym->name); > > + return -1; > > + } > > + } > > + } > > AFAIK the *_safe list traversals, they cache the ->next value at the > beginning of each iteration, so that one could blow the current element > in position away. The cached ->next value is then assigned when moving > to the next iteration. > > But notice how convert_rela() looks through the entire list of > relocations, moving each rela with a matching symbol? > > Consider a slight tweak to samples/livepatch-annotated.c: > > > static int livepatch_cmdline_proc_show(struct seq_file *m, void *v) > { > + if (saved_command_line) > + saved_command_line[0] = '\0'; > + > seq_printf(m, "%s livepatch=1\n", saved_command_line); > return 0; > } > > > On my system, this generates relocations like this: > > % eu-readelf --relocs samples/livepatch/livepatch-annotated-sample.o > Relocation section [ 2] '.rela.text' for section [ 1] '.text' at offset 0x98 contains 9 entries: > Offset Type Value Addend Name > 0x0000000000000001 X86_64_PC32 000000000000000000 -4 __fentry__ > 0x0000000000000008 X86_64_PC32 000000000000000000 -4 saved_command_line > 0x0000000000000017 X86_64_PC32 000000000000000000 -4 saved_command_line > 0x000000000000001e X86_64_32S 000000000000000000 +0 .rodata.str1.1 > 0x0000000000000023 X86_64_PC32 000000000000000000 -4 seq_printf > 0x0000000000000031 X86_64_PC32 000000000000000000 -4 __fentry__ > 0x0000000000000038 X86_64_32S 000000000000000000 +0 .data > 0x0000000000000051 X86_64_PC32 000000000000000000 -4 __fentry__ > 0x000000000000003d X86_64_PC32 000000000000000000 -4 klp_enable_patch > > We now have back-to-back rela's with sym->name = "saved_command_line". > When the first is converted, convert_rela() will move both of them to > the klp rela section. The linked list values may be consistent, but the > cached ->next value will be bogus and the in-flight-traversal will run > off the rails. Yep, valid, if I'm reading the code correctly. > I think we can work around it with a combination of 1) only moving a > single rela symbol at a time in convert_rela and 2) processing the > second (third, etc.) a little bit more so that they are moved > individually. > > I hacked this together and it works against the livepatch-annotate.c > test above so far... > > -->8-- -->8-- -->8-- -->8-- -->8-- -->8-- -->8-- -->8-- -->8-- -->8-- > > diff --git a/scripts/livepatch/klp-convert.c b/scripts/livepatch/klp-convert.c > index 82c27d219372..126395f1c0cd 100644 > --- a/scripts/livepatch/klp-convert.c > +++ b/scripts/livepatch/klp-convert.c > @@ -517,6 +517,7 @@ static bool convert_rela(struct section *oldsec, struct rela *r, > if (r1->sym->name == r->sym->name) { > list_del(&r1->list); > list_add(&r1->list, &sec->relas); > + break; > } > } > return true; Couldn't we remove the loop all together instead of breaking it? list_del(&r->list); list_add(&r->list, &sec->relas); could be sufficient. > @@ -549,8 +550,8 @@ static bool is_converted(char *sname) > } > > /* > - * Checks if symbol must be converted (conditions): > - * not resolved, not already converted or isn't an exported symbol > + * Checks if symbol must be or was already converted (conditions): > + * not resolved or isn't an exported symbol > */ > static bool must_convert(struct symbol *sym) > { > @@ -566,7 +567,7 @@ static bool must_convert(struct symbol *sym) > if (strcmp(sym->name, ".TOC.") == 0) > return false; > > - return (!(is_converted(sym->name) || is_exported(sym->name))); > + return (!is_exported(sym->name)); > } > > /* Checks if a section is a klp rela section */ > @@ -640,7 +641,8 @@ int main(int argc, const char **argv) > if (!must_convert(rela->sym)) > continue; > > - if (!find_missing_position(rela->sym, &sp)) { > + if (!is_converted(rela->sym->name) && > + !find_missing_position(rela->sym, &sp)) { > WARN("Unable to find missing symbol: %s", > rela->sym->name); > return -1; Looks good. Miroslav