Re: [PATCH][v10] PM / hibernate: Verify the consistent of e820 memory map by md5 digest
Hi, On Thu, Oct 13, 2016 at 03:10:18PM +0200, Denys Vlasenko wrote: > On Fri, Sep 9, 2016 at 2:21 PM, Chen Yu wrote: > > On some platforms, there is occasional panic triggered when trying to > > resume from hibernation, a typical panic looks like: > > > > "BUG: unable to handle kernel paging request at 880085894000 > > IP: [] load_image_lzo+0x8c2/0xe70" > > > > Investigation carried out by Lee Chun-Yi show that this is because > > e820 map has been changed by BIOS across hibernation, and one > > of the page frames from suspend kernel is right located in restore > > kernel's unmapped region, so panic comes out when accessing unmapped > > kernel address. > > > > In order to expose this issue earlier, the md5 hash of e820 map > > is passed from suspend kernel to restore kernel, and the restore > > kernel will terminate the resume process once it finds the md5 > > hash are not the same. > > > > As the format of image header has been modified, the magic number > > should also be adjusted as kernels with the same RESTORE_MAGIC have > > to use the same header format and interpret all of the fields in > > it in the same way. > > > > If the suspend kernel is built without md5 support, and the restore > > kernel has md5 support, then the latter will bypass the check process. > > Vice versa the restore kernel will bypass the check if it does not > > support md5 operation. > > > > Note: > > 1. Without this patch applied, it is possible that BIOS has > >provided an inconsistent memory map, but the resume kernel is still > >able to restore the image anyway(e.g, E820_RAM region is the superset > >of the previous one), although the system might be unstable. So this > >patch tries to treat any inconsistent e820 as illegal. > > > > 2. Another case is, this patch replies on comparing the e820_saved, but > >currently the e820_save might not be strictly the same across > >hibernation, even if BIOS has provided consistent e820 map - In > >theory mptable might modify the BIOS-provided e820_saved dynamically > >in early_reserve_e820_mpc_new, which would allocate a buffer from > >E820_RAM, and marks it from E820_RAM to E820_RESERVED). > >This is a potential and rare case we need to deal with in OS in > >the future. > > > > Suggested-by: Pavel Machek > > Suggested-by: Rafael J. Wysocki > > Cc: Rafael J. Wysocki > > Cc: Pavel Machek > > Cc: Lee Chun-Yi > > Cc: Borislav Petkov > > Acked-by: Pavel Machek > > Signed-off-by: Chen Yu > > --- > > > +static int get_e820_md5(struct e820map *map, void *buf) > > +{ > > + struct scatterlist sg; > > + struct crypto_ahash *tfm; > > + struct ahash_request *req; > > + int ret = 0; > > + > > + tfm = crypto_alloc_ahash("md5", 0, CRYPTO_ALG_ASYNC); > > + if (IS_ERR(tfm)) > > + return -ENOMEM; > > + > > + req = ahash_request_alloc(tfm, GFP_KERNEL); > > + if (!req) { > > + ret = -ENOMEM; > > + goto free_ahash; > > + } > > I looked elsewhere in kernel, and there is this idiom for placing > struct ahash_request on stack. Instead of the ahash_request_alloc() > and never-actually-tirggering-error handling, you can do: > > { > AHASH_REQUEST_ON_STACK(req, tfm); > > > + > > + sg_init_one(&sg, (u8 *)map, sizeof(struct e820map)); > > + ahash_request_set_callback(req, 0, NULL, NULL); > > + ahash_request_set_crypt(req, &sg, buf, sizeof(struct e820map)); > > + > > + if (crypto_ahash_digest(req)) > > + ret = -EINVAL; > > + > > + ahash_request_free(req); > > + free_ahash: > > and, naturally, the free() and the label would not be needed too, > just close the one extra brace: > > > + crypto_free_ahash(tfm); > > + > > + return ret; > > } > > > +} OK, thanks for point it out, will do in next version. Yu
Re: [PATCH][v10] PM / hibernate: Verify the consistent of e820 memory map by md5 digest
On Fri, Sep 9, 2016 at 2:21 PM, Chen Yu wrote: > On some platforms, there is occasional panic triggered when trying to > resume from hibernation, a typical panic looks like: > > "BUG: unable to handle kernel paging request at 880085894000 > IP: [] load_image_lzo+0x8c2/0xe70" > > Investigation carried out by Lee Chun-Yi show that this is because > e820 map has been changed by BIOS across hibernation, and one > of the page frames from suspend kernel is right located in restore > kernel's unmapped region, so panic comes out when accessing unmapped > kernel address. > > In order to expose this issue earlier, the md5 hash of e820 map > is passed from suspend kernel to restore kernel, and the restore > kernel will terminate the resume process once it finds the md5 > hash are not the same. > > As the format of image header has been modified, the magic number > should also be adjusted as kernels with the same RESTORE_MAGIC have > to use the same header format and interpret all of the fields in > it in the same way. > > If the suspend kernel is built without md5 support, and the restore > kernel has md5 support, then the latter will bypass the check process. > Vice versa the restore kernel will bypass the check if it does not > support md5 operation. > > Note: > 1. Without this patch applied, it is possible that BIOS has >provided an inconsistent memory map, but the resume kernel is still >able to restore the image anyway(e.g, E820_RAM region is the superset >of the previous one), although the system might be unstable. So this >patch tries to treat any inconsistent e820 as illegal. > > 2. Another case is, this patch replies on comparing the e820_saved, but >currently the e820_save might not be strictly the same across >hibernation, even if BIOS has provided consistent e820 map - In >theory mptable might modify the BIOS-provided e820_saved dynamically >in early_reserve_e820_mpc_new, which would allocate a buffer from >E820_RAM, and marks it from E820_RAM to E820_RESERVED). >This is a potential and rare case we need to deal with in OS in >the future. > > Suggested-by: Pavel Machek > Suggested-by: Rafael J. Wysocki > Cc: Rafael J. Wysocki > Cc: Pavel Machek > Cc: Lee Chun-Yi > Cc: Borislav Petkov > Acked-by: Pavel Machek > Signed-off-by: Chen Yu > --- > +static int get_e820_md5(struct e820map *map, void *buf) > +{ > + struct scatterlist sg; > + struct crypto_ahash *tfm; > + struct ahash_request *req; > + int ret = 0; > + > + tfm = crypto_alloc_ahash("md5", 0, CRYPTO_ALG_ASYNC); > + if (IS_ERR(tfm)) > + return -ENOMEM; > + > + req = ahash_request_alloc(tfm, GFP_KERNEL); > + if (!req) { > + ret = -ENOMEM; > + goto free_ahash; > + } I looked elsewhere in kernel, and there is this idiom for placing struct ahash_request on stack. Instead of the ahash_request_alloc() and never-actually-tirggering-error handling, you can do: { AHASH_REQUEST_ON_STACK(req, tfm); > + > + sg_init_one(&sg, (u8 *)map, sizeof(struct e820map)); > + ahash_request_set_callback(req, 0, NULL, NULL); > + ahash_request_set_crypt(req, &sg, buf, sizeof(struct e820map)); > + > + if (crypto_ahash_digest(req)) > + ret = -EINVAL; > + > + ahash_request_free(req); > + free_ahash: and, naturally, the free() and the label would not be needed too, just close the one extra brace: > + crypto_free_ahash(tfm); > + > + return ret; } > +}
RE: [PATCH][v10] PM / hibernate: Verify the consistent of e820 memory map by md5 digest
Hi, Sorry for late response, I missed the thread in mailbox, > -Original Message- > From: rjwyso...@gmail.com [mailto:rjwyso...@gmail.com] On Behalf Of > Rafael J. Wysocki > Sent: Monday, September 19, 2016 7:46 PM > To: Chen, Yu C > Cc: Linux PM; the arch/x86 maintainers; Linux Kernel Mailing List; Thomas > Gleixner; Ingo Molnar; H. Peter Anvin; Wysocki, Rafael J; Pavel Machek; Lee > Chun-Yi; Borislav Petkov > Subject: Re: [PATCH][v10] PM / hibernate: Verify the consistent of e820 > memory map by md5 digest > > On Fri, Sep 9, 2016 at 2:21 PM, Chen Yu wrote: > > On some platforms, there is occasional panic triggered when trying to > > resume from hibernation, a typical panic looks like: > > [cut] > > > @@ -211,10 +292,15 @@ int arch_hibernation_header_save(void *addr, > unsigned int max_size) > > */ > > int arch_hibernation_header_restore(void *addr) { > > + bool e820_mismatch = false; > > The extra local variable can be avoided if you structure the code slightly > differently. > > > struct restore_data_record *rdr = addr; > > > > restore_jump_address = rdr->jump_address; > > jump_address_phys = rdr->jump_address_phys; > > restore_cr3 = rdr->cr3; > > - return (rdr->magic == RESTORE_MAGIC) ? 0 : -EINVAL; > > + > > + e820_mismatch = hibernation_e820_mismatch(rdr->e820_digest); > > Also calling hibernation_e820_mismatch() before checking rdr->magic may not > be useful at all. > Yes. > > + > > + return (rdr->magic == RESTORE_MAGIC) ? > > + (e820_mismatch ? -ENODEV : 0) : -EINVAL; > > So what about: > > if (rdr->magic != RESTORE_MAGIC) > return -EINVAL; > > if (hibernation_e820_mismatch(rdr->e820_digest)) > return -ENODEV; > > return 0; > > Ok, will change it to this one. Thanks. > > } > > -- > > Thanks, > Rafael Thanks, Yu
RE: [PATCH][v10] PM / hibernate: Verify the consistent of e820 memory map by md5 digest
Hi, Sorry for late response, I missed this thread in mailbox, > -Original Message- > From: rjwyso...@gmail.com [mailto:rjwyso...@gmail.com] On Behalf Of > Rafael J. Wysocki > Sent: Monday, September 19, 2016 7:46 PM > To: Chen, Yu C > Cc: Linux PM; the arch/x86 maintainers; Linux Kernel Mailing List; Thomas > Gleixner; Ingo Molnar; H. Peter Anvin; Wysocki, Rafael J; Pavel Machek; Lee > Chun-Yi; Borislav Petkov > Subject: Re: [PATCH][v10] PM / hibernate: Verify the consistent of e820 > memory map by md5 digest > > On Fri, Sep 9, 2016 at 2:21 PM, Chen Yu wrote: > > On some platforms, there is occasional panic triggered when trying to > > resume from hibernation, a typical panic looks like: > > [cut] > > > @@ -211,10 +292,15 @@ int arch_hibernation_header_save(void *addr, > unsigned int max_size) > > */ > > int arch_hibernation_header_restore(void *addr) { > > + bool e820_mismatch = false; > > The extra local variable can be avoided if you structure the code slightly > differently. > > > struct restore_data_record *rdr = addr; > > > > restore_jump_address = rdr->jump_address; > > jump_address_phys = rdr->jump_address_phys; > > restore_cr3 = rdr->cr3; > > - return (rdr->magic == RESTORE_MAGIC) ? 0 : -EINVAL; > > + > > + e820_mismatch = hibernation_e820_mismatch(rdr->e820_digest); > > Also calling hibernation_e820_mismatch() before checking rdr->magic may not > be useful at all. Yes. > > > + > > + return (rdr->magic == RESTORE_MAGIC) ? > > + (e820_mismatch ? -ENODEV : 0) : -EINVAL; > > So what about: > > if (rdr->magic != RESTORE_MAGIC) > return -EINVAL; > > if (hibernation_e820_mismatch(rdr->e820_digest)) > return -ENODEV; > > return 0; > OK, will change it to this one. Thanks! > > } > > -- > > Thanks, > Rafael Thanks, Yu
Re: [PATCH][v10] PM / hibernate: Verify the consistent of e820 memory map by md5 digest
On Fri, Sep 9, 2016 at 2:21 PM, Chen Yu wrote: > On some platforms, there is occasional panic triggered when trying to > resume from hibernation, a typical panic looks like: [cut] > @@ -211,10 +292,15 @@ int arch_hibernation_header_save(void *addr, unsigned > int max_size) > */ > int arch_hibernation_header_restore(void *addr) > { > + bool e820_mismatch = false; The extra local variable can be avoided if you structure the code slightly differently. > struct restore_data_record *rdr = addr; > > restore_jump_address = rdr->jump_address; > jump_address_phys = rdr->jump_address_phys; > restore_cr3 = rdr->cr3; > - return (rdr->magic == RESTORE_MAGIC) ? 0 : -EINVAL; > + > + e820_mismatch = hibernation_e820_mismatch(rdr->e820_digest); Also calling hibernation_e820_mismatch() before checking rdr->magic may not be useful at all. > + > + return (rdr->magic == RESTORE_MAGIC) ? > + (e820_mismatch ? -ENODEV : 0) : -EINVAL; So what about: if (rdr->magic != RESTORE_MAGIC) return -EINVAL; if (hibernation_e820_mismatch(rdr->e820_digest)) return -ENODEV; return 0; > } > -- Thanks, Rafael