Print this page
7804 fdisk_read_master_part_table() causes 'format' to crash
Reviewed by: Jason King <jason.brian.king@gmail.com>
Reviewed by: Hans Rosenfeld <hans.rosenfeld@nexenta.com>
Reviewed by: Robert Mustacchi <rm@joyent.com>


   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.

  23  */
  24 
  25 #include <stdio.h>
  26 #include <stdlib.h>
  27 #include <string.h>
  28 #include <strings.h>
  29 #include <unistd.h>
  30 #include <errno.h>
  31 #include <fcntl.h>
  32 #include <ctype.h>
  33 #include <sys/stat.h>
  34 #include <sys/types.h>
  35 #include <sys/param.h>
  36 #include <sys/systeminfo.h>
  37 #include <sys/efi_partition.h>
  38 #include <sys/byteorder.h>
  39 
  40 #include <sys/vtoc.h>
  41 #include <sys/tty.h>
  42 #include <sys/dktp/fdisk.h>


 751 fdisk_alloc_part_table()
 752 {
 753         int size = sizeof (struct ipart);
 754         struct ipart *table;
 755 
 756         if ((table = calloc(4, size)) == NULL) {
 757                 return (NULL);
 758         }
 759 
 760         return (table);
 761 }
 762 
 763 /*
 764  * Reads the master fdisk partition table from the device assuming that it has
 765  * a valid table.
 766  * MBR is supposed to be of 512 bytes no matter what the device block size is.
 767  */
 768 static int
 769 fdisk_read_master_part_table(ext_part_t *epp)
 770 {
 771         uchar_t buf[512];
 772         int sectsize = 512;

 773         int size = sizeof (struct ipart);
 774         int cpcnt = FD_NUMPART * size;
 775 
 776         if (lseek(epp->dev_fd, 0, SEEK_SET) < 0) {
 777                 return (EIO);
 778         }











 779         if (read(epp->dev_fd, buf, sectsize) < sectsize) {

 780                 return (EIO);
 781         }
 782 
 783         /*LINTED*/
 784         if (LE_16((*(uint16_t *)&buf[510])) != MBB_MAGIC) {
 785                 bzero(epp->mtable, cpcnt);

 786                 return (FDISK_EBADMAGIC);
 787         }
 788 
 789         bcopy(&buf[FDISK_PART_TABLE_START], epp->mtable, cpcnt);

 790 
 791         return (FDISK_SUCCESS);
 792 }
 793 
 794 int
 795 fdisk_ext_part_exists(ext_part_t *epp)
 796 {
 797         int i;
 798         struct ipart *part_table = epp->mtable;
 799 
 800         if (part_table == NULL) {
 801                 /* No extended partition found */
 802                 return (0);
 803         }
 804 
 805         for (i = 0; i < FD_NUMPART; i++) {
 806                 if (fdisk_is_dos_extended(LE_8(part_table[i].systid))) {
 807                         break;
 808                 }
 809         }




   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright 2017 The MathWorks, Inc.  All rights reserved.
  24  */
  25 
  26 #include <stdio.h>
  27 #include <stdlib.h>
  28 #include <string.h>
  29 #include <strings.h>
  30 #include <unistd.h>
  31 #include <errno.h>
  32 #include <fcntl.h>
  33 #include <ctype.h>
  34 #include <sys/stat.h>
  35 #include <sys/types.h>
  36 #include <sys/param.h>
  37 #include <sys/systeminfo.h>
  38 #include <sys/efi_partition.h>
  39 #include <sys/byteorder.h>
  40 
  41 #include <sys/vtoc.h>
  42 #include <sys/tty.h>
  43 #include <sys/dktp/fdisk.h>


 752 fdisk_alloc_part_table()
 753 {
 754         int size = sizeof (struct ipart);
 755         struct ipart *table;
 756 
 757         if ((table = calloc(4, size)) == NULL) {
 758                 return (NULL);
 759         }
 760 
 761         return (table);
 762 }
 763 
 764 /*
 765  * Reads the master fdisk partition table from the device assuming that it has
 766  * a valid table.
 767  * MBR is supposed to be of 512 bytes no matter what the device block size is.
 768  */
 769 static int
 770 fdisk_read_master_part_table(ext_part_t *epp)
 771 {
 772         struct dk_minfo_ext dkmp_ext;
 773         uchar_t *buf;
 774         int sectsize;
 775         int size = sizeof (struct ipart);
 776         int cpcnt = FD_NUMPART * size;
 777 
 778         if (lseek(epp->dev_fd, 0, SEEK_SET) < 0) {
 779                 return (EIO);
 780         }
 781         if (ioctl(epp->dev_fd, DKIOCGMEDIAINFOEXT, &dkmp_ext) < 0) {
 782                 return (EIO);
 783         }
 784         if (dkmp_ext.dki_lbsize < 512) {
 785                 return (EIO);
 786         }
 787         sectsize = dkmp_ext.dki_lbsize;
 788         buf = calloc(sectsize, sizeof (uchar_t));
 789         if (buf == NULL) {
 790                 return (ENOMEM);
 791         }
 792         if (read(epp->dev_fd, buf, sectsize) < sectsize) {
 793                 free(buf);
 794                 return (EIO);
 795         }
 796 
 797         /*LINTED*/
 798         if (LE_16((*(uint16_t *)&buf[510])) != MBB_MAGIC) {
 799                 bzero(epp->mtable, cpcnt);
 800                 free(buf);
 801                 return (FDISK_EBADMAGIC);
 802         }
 803 
 804         bcopy(&buf[FDISK_PART_TABLE_START], epp->mtable, cpcnt);
 805         free(buf);
 806 
 807         return (FDISK_SUCCESS);
 808 }
 809 
 810 int
 811 fdisk_ext_part_exists(ext_part_t *epp)
 812 {
 813         int i;
 814         struct ipart *part_table = epp->mtable;
 815 
 816         if (part_table == NULL) {
 817                 /* No extended partition found */
 818                 return (0);
 819         }
 820 
 821         for (i = 0; i < FD_NUMPART; i++) {
 822                 if (fdisk_is_dos_extended(LE_8(part_table[i].systid))) {
 823                         break;
 824                 }
 825         }