From: Xuesen Guo Subject: Re: [PATCH] readnote: Add bzImage kernel support Date: Thu, 26 Apr 2012 16:50:02 +0800 Message-ID: <1335430202.4742.11.camel@goosenl-desktop> References: <1335425950.4881.59.camel@dagon.hellion.org.uk> Reply-To: Xuesen.Guo@hitachiconsulting.com Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============5414332124988364012==" Return-path: In-Reply-To: <1335425950.4881.59.camel@dagon.hellion.org.uk> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Ian Campbell Cc: "xen-devel@lists.xensource.com" , Ian Jackson , Stefano Stabellini List-Id: xen-devel@lists.xenproject.org --===============5414332124988364012== Content-Type: multipart/alternative; boundary="=-SPx70R9x5q3+niTb9dOF" --=-SPx70R9x5q3+niTb9dOF Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Many thanks for your comments. I will resend the patch according to your suggestions. --Xuesen On Thu, 2012-04-26 at 08:39 +0100, Ian Campbell wrote: > On Thu, 2012-04-26 at 04:00 +0100, Xuesen Guo wrote: > > Add the check of bzImage kernel and make it work > > with RHEL 6 bzipped kernels > >=20 > > Signed-off-by: Xuesen Guo > >=20 > > diff -r d690c7e896a2 -r ec8e99606fbe tools/xcutils/readnotes.c > > --- a/tools/xcutils/readnotes.c=09Thu Apr 05 11:06:03 2012 +0100 > > +++ b/tools/xcutils/readnotes.c=09Thu Apr 26 10:57:09 2012 +0800 > > @@ -18,6 +18,48 @@ > > =20 > > static xc_interface *xch; > > =20 > > +/* According to the implemation of xc_dom_probe_bzimage_kernel() funct= ion */ > > +/* We add support of bzImage kernel */ > > +/* Copied from tools/libxc/xc_doom_bzImageloader.c */ > > +struct setup_header { > > +=09uint8_t _pad0[0x1f1]; /* skip uninteresting stuff */ > > +=09uint8_t setup_sects; > > +=09uint16_t root_flags; > > +=09uint32_t syssize; > > +=09uint16_t ram_size; > > +=09uint16_t vid_mode; > > +=09uint16_t root_dev; > > +=09uint16_t boot_flag; > > +=09uint16_t jump; > > +=09uint32_t header; > > +#define HDR_MAGIC "HdrS" > > +#define HDR_MAGIC_SZ 4 > > +=09uint16_t version; > > +#define VERSION(h,l) (((h)<<8) | (l)) > > +=09uint32_t realmode_swtch; > > +=09uint16_t start_sys; > > +=09uint16_t kernel_version; > > +=09uint8_t type_of_loader; > > +=09uint8_t loadflags; > > +=09uint16_t setup_move_size; > > +=09uint32_t code32_start; > > +=09uint32_t ramdisk_image; > > +=09uint32_t ramdisk_size; > > +=09uint32_t bootsect_kludge; > > +=09uint16_t heap_end_ptr; > > +=09uint16_t _pad1; > > +=09uint32_t cmd_line_ptr; > > +=09uint32_t initrd_addr_max; > > +=09uint32_t kernel_alignment; > > +=09uint8_t relocatable_kernel; > > +=09uint8_t _pad2[3]; > > +=09uint32_t cmdline_size; > > +=09uint32_t hardware_subarch; > > +=09uint64_t hardware_subarch_data; > > +=09uint32_t payload_offset; > > +=09uint32_t payload_length; > > +} __attribute__((packed)); > > + > > static void print_string_note(const char *prefix, struct elf_binary *e= lf, > > =09=09=09 const elf_note *note) > > { > > @@ -131,6 +173,9 @@ int main(int argc, char **argv) > > =09const elf_shdr *shdr; > > =09int notes_found =3D 0; > > =20 > > +=09struct setup_header *hdr; > > +=09uint64_t payload_offset, payload_length; > > + > > =09if (argc !=3D 2) > > =09{ > > =09=09fprintf(stderr, "Usage: readnotes \n"); > > @@ -159,6 +204,27 @@ int main(int argc, char **argv) > > =09=09fprintf(stderr, "Unable to map %s: %s\n", f, strerror(errno)); > > =09=09return 1; > > =09} > > +=09 > > +=09/* Check the magic of bzImage kernel */ > > +=09hdr =3D (struct setup_header *)image; > > +=09if ( memcmp(&hdr->header, HDR_MAGIC, HDR_MAGIC_SZ) =3D=3D 0 ) > > +=09{ > > +=09=09if ( hdr->version < VERSION(2,8) ) > > +=09=09{ > > +=09=09=09printf("%s: boot protocol too old (%04x)", __FUNCTION__, hdr-= >version); > > +=09=09=09return 1; > > +=09=09} > > + > > +=09=09/* upcast to 64 bits to avoid overflow */ > > +=09=09/* setup_sects is u8 and so cannot overflow */ > > +=09=09payload_offset =3D (hdr->setup_sects + 1) * 512; > > +=09=09payload_offset +=3D hdr->payload_offset; > > +=09=09payload_length =3D hdr->payload_length; >=20 > Up to this point this is all the same as xc_dom_probe_bzimage_kernel and > therefore looks to me to be safe and correct. >=20 > However xc_dom_probe_bzimage_kernel has some additional checks before it > trusts the offset and length. Specifically it compares them against the > size of the on disk kernel image: > if ( payload_offset >=3D dom->kernel_size ) > { > xc_dom_panic(dom->xch, XC_INVALID_KERNEL, "%s: payload offset ove= rflow", > __FUNCTION__); > return -EINVAL; > } > if ( (payload_offset + payload_length) > dom->kernel_size ) > { > xc_dom_panic(dom->xch, XC_INVALID_KERNEL, "%s: payload length ove= rflow", > __FUNCTION__); > return -EINVAL; > } >=20 > I think you likely want to retain some similar check here, using > st.st_size as the boundary instead of dom->kernel_size. >=20 > > + > > +=09=09image =3D image + payload_offset; > > +=09=09st.st_size =3D payload_length; > > +=09} > > +=09 > > =09size =3D st.st_size; >=20 > The usage of size vs st.st_size in the existing code is not very > obvious, IMHO it would be preferable to consistently always use size > from this point on, since changing st.st_size has the possibility of > surprise. IOW the tail here would be: >=20 > =09=09image =3D image + payload_offset; > =09=09size =3D payload_length > =09} else { > =09=09size =3D st.st_size; > =09} >=20 > and then use size and not st.st_size for the remainder of the function. >=20 > Ian. >=20 > > =09usize =3D xc_dom_check_gzip(xch, image, st.st_size); > >=20 > > This e-mail is intended solely for the person or entity to which it is = addressed > > and may contain confidential and/or privileged information. Any review,= dissemination, > > copying, printing or other use of this e-mail by persons or entities ot= her than the=20 > > addressee is prohibited. If you have received this e-mail in error, ple= ase contact > > the sender immediately and delete the material from any computer. > > To unsubscribe send an email to: Unsubscribe@hitachiconsulting.com=20 > > Hitachi Consulting (China) Co., Ltd. (HCCD0411) > >=20 > >=20 > >=20 > > _______________________________________________ > > Xen-devel mailing list > > Xen-devel@lists.xen.org > > http://lists.xen.org/xen-devel >=20 >=20 >=20 --=-SPx70R9x5q3+niTb9dOF Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Many thanks for your comments.
I will resend the patch according to your suggestions.

--Xuesen


On Thu, 2012-04-26 at 08:39 +0100, Ian Campbell wrote:
On Thu, 2012-04-26 at 04:00 +0100, Xuesen Guo wrote:
> Add the check of bzImage kernel and make it work
> with RHEL 6 bzipped kernels
>=20
> Signed-off-by: Xuesen Guo <Xuesen.Guo@hitachiconsulting.com>
>=20
> diff -r d690c7e896a2 -r ec8e99606fbe tools/xcutils/readnotes.c
> --- a/tools/xcutils/readnotes.c=09Thu Apr 05 11:06:03 2012 +0100
> +++ b/tools/xcutils/readnotes.c=09Thu Apr 26 10:57:09 2012 +0800
> @@ -18,6 +18,48 @@
> =20
>  static xc_interface *xch;
> =20
> +/* According to the implemation of xc_dom_probe_bzimage_kernel() func=
tion */
> +/* We add support of bzImage kernel */
> +/* Copied from tools/libxc/xc_doom_bzImageloader.c */
> +struct setup_header {
> +=09uint8_t  _pad0[0x1f1];  /* skip uninteresting stuff */
> +=09uint8_t  setup_sects;
> +=09uint16_t root_flags;
> +=09uint32_t syssize;
> +=09uint16_t ram_size;
> +=09uint16_t vid_mode;
> +=09uint16_t root_dev;
> +=09uint16_t boot_flag;
> +=09uint16_t jump;
> +=09uint32_t header;
> +#define HDR_MAGIC  "HdrS"
> +#define HDR_MAGIC_SZ 4
> +=09uint16_t version;
> +#define VERSION(h,l) (((h)<<8) | (l))
> +=09uint32_t realmode_swtch;
> +=09uint16_t start_sys;
> +=09uint16_t kernel_version;
> +=09uint8_t  type_of_loader;
> +=09uint8_t  loadflags;
> +=09uint16_t setup_move_size;
> +=09uint32_t code32_start;
> +=09uint32_t ramdisk_image;
> +=09uint32_t ramdisk_size;
> +=09uint32_t bootsect_kludge;
> +=09uint16_t heap_end_ptr;
> +=09uint16_t _pad1;
> +=09uint32_t cmd_line_ptr;
> +=09uint32_t initrd_addr_max;
> +=09uint32_t kernel_alignment;
> +=09uint8_t  relocatable_kernel;
> +=09uint8_t  _pad2[3];
> +=09uint32_t cmdline_size;
> +=09uint32_t hardware_subarch;
> +=09uint64_t hardware_subarch_data;
> +=09uint32_t payload_offset;
> +=09uint32_t payload_length;
> +} __attribute__((packed));
> +
>  static void print_string_note(const char *prefix, struct elf_binary *=
elf,
>  =09=09=09      const elf_note *note)
>  {
> @@ -131,6 +173,9 @@ int main(int argc, char **argv)
>  =09const elf_shdr *shdr;
>  =09int notes_found =3D 0;
> =20
> +=09struct setup_header *hdr;
> +=09uint64_t payload_offset, payload_length;
> +
>  =09if (argc !=3D 2)
>  =09{
>  =09=09fprintf(stderr, "Usage: readnotes <elfimage>\n"=
);
> @@ -159,6 +204,27 @@ int main(int argc, char **argv)
>  =09=09fprintf(stderr, "Unable to map %s: %s\n", f, strerror=
(errno));
>  =09=09return 1;
>  =09}
> +=09
> +=09/* Check the magic of bzImage kernel */
> +=09hdr =3D (struct setup_header *)image;
> +=09if ( memcmp(&hdr->header, HDR_MAGIC, HDR_MAGIC_SZ) =3D=3D 0=
 )
> +=09{
> +=09=09if ( hdr->version < VERSION(2,8) )
> +=09=09{
> +=09=09=09printf("%s: boot protocol too old (%04x)", __FUNCT=
ION__, hdr->version);
> +=09=09=09return 1;
> +=09=09}
> +
> +=09=09/* upcast to 64 bits to avoid overflow */
> +=09=09/* setup_sects is u8 and so cannot overflow */
> +=09=09payload_offset =3D (hdr->setup_sects + 1) * 512;
> +=09=09payload_offset +=3D hdr->payload_offset;
> +=09=09payload_length =3D hdr->payload_length;

Up to this point this is all the same as xc_dom_probe_bzimage_kernel and
therefore looks to me to be safe and correct.

However xc_dom_probe_bzimage_kernel has some additional checks before it
trusts the offset and length. Specifically it compares them against the
size of the on disk kernel image:
    if ( payload_offset >=3D dom->kernel_size )
    {
        xc_dom_panic(dom->xch, XC_INVALID_KERNEL, "%s: payload offs=
et overflow",
                     __FUNCTION__);
        return -EINVAL;
    }
    if ( (payload_offset + payload_length) > dom->kernel_size )
    {
        xc_dom_panic(dom->xch, XC_INVALID_KERNEL, "%s: payload leng=
th overflow",
                     __FUNCTION__);
        return -EINVAL;
    }

I think you likely want to retain some similar check here, using
st.st_size as the boundary instead of dom->kernel_size.

> +
> +=09=09image =3D image + payload_offset;
> +=09=09st.st_size =3D payload_length;
> +=09}
> +=09
>  =09size =3D st.st_size;

The usage of size vs st.st_size in the existing code is not very
obvious, IMHO it would be preferable to consistently always use size
from this point on, since changing st.st_size has the possibility of
surprise. IOW the tail here would be:

=09=09image =3D image + payload_offset;
=09=09size =3D payload_length
=09} else {
=09=09size =3D st.st_size;
 =09}

and then use size and not st.st_size for the remainder of the function.

Ian.

>  =09usize =3D xc_dom_check_gzip(xch, image, st.st_size);
>=20
> This e-mail is intended solely for the person or entity to which it is=
 addressed
> and may contain confidential and/or privileged information. Any review=
, dissemination,
> copying, printing or other use of this e-mail by persons or entities o=
ther than the=20
> addressee is prohibited. If you have received this e-mail in error, pl=
ease contact
> the sender immediately and delete the material from any computer.
> To unsubscribe send an email to: Unsubscribe@hitachiconsulting.com=20
> Hitachi Consulting (China) Co., Ltd. (HCCD0411)
>=20
>=20
>=20
> _______________________________________________
> Xen-devel mailing list
> Xen-devel@lists.xen.org
> http://lists.xen.org/xen-de=
vel




--=-SPx70R9x5q3+niTb9dOF-- --===============5414332124988364012== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --===============5414332124988364012==--