Print this page
NEX-9323  cfgadm FC plugin allocates insufficient memory for internal
buffers
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
Reviewed by: Rick McNeal <rick.mcneal@nexenta.com>

Split Close
Expand all
Collapse all
          --- old/usr/src/lib/cfgadm_plugins/fp/common/cfga_rep.c
          +++ new/usr/src/lib/cfgadm_plugins/fp/common/cfga_rep.c
↓ open down ↓ 12 lines elided ↑ open up ↑
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  /*
  22   22   * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
       23 + * Copyright 2017 Nexenta Systems, Inc. All rights reserved.
  23   24   * Use is subject to license terms.
  24   25   */
  25   26  
  26   27  
  27   28  
  28   29  #include <libgen.h>
       30 +#include <limits.h>
  29   31  #include "cfga_fp.h"
  30   32  
  31   33  /* The following are used by update_fabric_wwn_list() */
  32   34  #define COPY_EXT        ".cpy."         /* Extn used in naming backup file */
  33   35  #define TMP_EXT         ".tmp."         /* Extn used in naming temp file */
  34   36  static char *HDR =
  35   37  "#\n"
  36   38  "# fabric_WWN_map\n"
  37   39  "#\n"
  38   40  "# The physical ap_id list of configured fabric devices.\n"
↓ open down ↓ 176 lines elided ↑ open up ↑
 215  217   * 'update_str', the c_repbuf is copied to a OLD_FAB_REPOSITORY and t_repbuf
 216  218   * is made FAB_REPOSITORY.
 217  219   *
 218  220   */
 219  221  int
 220  222  update_fabric_wwn_list(int cmd, const char *update_str, char **errstring)
 221  223  {
 222  224          int     fd, copy_fd, tmp_fd, new_file_flag = 0;
 223  225          int     len, write_offset, bytes_left;
 224  226          int     sizeof_rep_hdr = strlen(HDR);
      227 +        int     pid_maxlen = snprintf(NULL, 0, "%d", PID_MAX) + 1;
 225  228          char    *repbuf, *c_repbuf, *t_repbuf;
 226  229          char    *copy_rep, *tmp_rep, *upd_str;
 227  230          off_t   filesize, size;
 228  231          struct stat     stbuf;
 229  232  
 230  233          /* Do some initializations */
 231  234          fd = copy_fd = tmp_fd = -1;
 232  235          repbuf = c_repbuf = t_repbuf = NULL;
 233  236          copy_rep = tmp_rep = upd_str = NULL;
 234  237          size = filesize = write_offset = bytes_left = 0;
↓ open down ↓ 61 lines elided ↑ open up ↑
 296  299                  cfga_err(errstring, errno, ERR_UPD_REP, 0);
 297  300                  return (FPCFGA_LIB_ERR);
 298  301          }
 299  302  
 300  303          strcpy(upd_str, update_str);
 301  304          strcat(upd_str, "\n");          /* Append a new line char */
 302  305          len = strlen(upd_str);
 303  306  
 304  307          if (filesize > 0) {
 305  308                  if ((copy_rep = (char *)calloc(1, strlen(FAB_REPOSITORY) +
 306      -                                sizeof (COPY_EXT) + sizeof (pid_t))) == NULL) {
      309 +                                sizeof (COPY_EXT) + pid_maxlen)) == NULL) {
 307  310                          cfga_err(errstring, errno, ERR_UPD_REP, 0);
 308  311                          CLEANUP_N_RET(FPCFGA_LIB_ERR);
 309  312                  }
 310  313  
 311  314                  (void) sprintf(copy_rep, "%s%s%ld", FAB_REPOSITORY, COPY_EXT,
 312  315                                                                  getpid());
 313  316  
 314  317                  if ((copy_fd = open(copy_rep, O_RDWR | O_CREAT | O_TRUNC,
 315  318                                                  S_IRUSR | S_IWUSR)) < 0) {
 316  319                          cfga_err(errstring, errno, ERR_UPD_REP, 0);
↓ open down ↓ 106 lines elided ↑ open up ↑
 423  426                   * we dont expect upd_str to match anything in the header.
 424  427                   */
 425  428                  if (search_line(c_repbuf, filesize, upd_str,
 426  429                                  len - 1, &write_offset, &bytes_left) == 0) {
 427  430                          /* line already exists in repository or len == 0 */
 428  431                          CLEANUP_N_RET(FPCFGA_OK); /* SUCCESS */
 429  432                  }
 430  433  
 431  434                  /* construct temp file name using pid. */
 432  435                  if ((tmp_rep = (char *)calloc(1, strlen(FAB_REPOSITORY) +
 433      -                                sizeof (TMP_EXT) + sizeof (pid_t))) == NULL) {
      436 +                                sizeof (TMP_EXT) + pid_maxlen)) == NULL) {
 434  437                          cfga_err(errstring, errno, ERR_UPD_REP, 0);
 435  438                          CLEANUP_N_RET(FPCFGA_LIB_ERR);
 436  439                  }
 437  440  
 438  441                  (void) sprintf(tmp_rep, "%s%s%ld", FAB_REPOSITORY,
 439  442                                                          TMP_EXT, getpid());
 440  443  
 441  444                  /* Open tmp repository file in absolute mode */
 442  445                  if ((tmp_fd = open(tmp_rep, O_RDWR|O_CREAT|O_TRUNC,
 443  446                                                  S_IRUSR | S_IWUSR)) < 0) {
↓ open down ↓ 74 lines elided ↑ open up ↑
 518  521                  }
 519  522  
 520  523                  if (search_line(c_repbuf, filesize, upd_str, len - 1,
 521  524                                          &write_offset, &bytes_left) != 0) {
 522  525                          /* this line does not exists - nothing to remove */
 523  526                          CLEANUP_N_RET(FPCFGA_OK); /* SUCCESS */
 524  527                  }
 525  528  
 526  529                  /* construct temp file name using pid. */
 527  530                  if ((tmp_rep = (char *)calloc(1, strlen(FAB_REPOSITORY) +
 528      -                                sizeof (TMP_EXT) + sizeof (pid_t))) == NULL) {
      531 +                                sizeof (TMP_EXT) + pid_maxlen)) == NULL) {
 529  532                          cfga_err(errstring, errno, ERR_UPD_REP, 0);
 530  533                          CLEANUP_N_RET(FPCFGA_LIB_ERR);
 531  534                  }
 532  535  
 533  536                  (void) sprintf(tmp_rep, "%s%s%ld", FAB_REPOSITORY,
 534  537                                                          TMP_EXT, getpid());
 535  538  
 536  539                  /* Open tmp repository file in absolute mode */
 537  540                  if ((tmp_fd = open(tmp_rep, O_RDWR|O_CREAT|O_TRUNC,
 538  541                                                  S_IRUSR | S_IWUSR)) < 0) {
↓ open down ↓ 76 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX