RPM Package Manager, CVS Repository
  http://rpm5.org/cvs/
  ____________________________________________________________________________

  Server: rpm5.org                         Name:   Jeff Johnson
  Root:   /v/rpm/cvs                       Email:  j...@rpm5.org
  Module: libtpm                           Date:   22-Sep-2013 03:31:10
  Branch: HEAD                             Handle: 2013092201310900

  Modified files:
    libtpm/libtpm/lib       keyswap.c

  Log:
    - tpm: coverity fix.

  Summary:
    Revision    Changes     Path
    1.7         +44 -25     libtpm/libtpm/lib/keyswap.c
  ____________________________________________________________________________

  patch -p0 <<'@@ .'
  Index: libtpm/libtpm/lib/keyswap.c
  ============================================================================
  $ cvs diff -u -r1.6 -r1.7 keyswap.c
  --- libtpm/libtpm/lib/keyswap.c       21 Sep 2013 23:06:29 -0000      1.6
  +++ libtpm/libtpm/lib/keyswap.c       22 Sep 2013 01:31:09 -0000      1.7
  @@ -33,11 +33,10 @@
       int rc = 0;
       uint32_t handle;
   
  -    if (shandle == 0x00000000 ||
  -     (shandle >= TPM_KH_SRK &&
  -      shandle <= TPM_KH_EK) || shandle == 0xffffffff) {
  +    if (shandle == 0x00000000
  +     ||      (shandle >= TPM_KH_SRK && shandle <= TPM_KH_EK)
  +     || shandle == 0xffffffff)
        return 1;
  -    }
   
       for (ctr = 2; ctr < capabilities->used; ctr += sizeof(handle)) {
        tpm_buffer_load32(capabilities, ctr, &handle);
  @@ -68,11 +67,10 @@
   
   static int isKeySwapable(uint32_t shandle)
   {
  -    if (shandle == 0x00000000 ||
  -     (shandle >= TPM_KH_SRK &&
  -      shandle <= TPM_KH_EK) || shandle == 0xffffffff) {
  +    if (shandle == 0x00000000
  +     ||      (shandle >= TPM_KH_SRK && shandle <= TPM_KH_EK)
  +     || shandle == 0xffffffff)
        return 0;
  -    }
       return 1;
   }
   
  @@ -82,30 +80,39 @@
       char *filename = createKeyFilename(handle);
       STACK_TPM_BUFFER(context);
       uint32_t ret = 0;
  +    FILE *fp = NULL;
   
  -    if (filename == NULL)
  +    if (filename == NULL) {
        ret = ERR_MEM_ERR;
  +     goto exit;
  +    }
  +
   #if 0
       printf("Swapping OUT key with handle %08x\n", handle);
   #endif
   
       TSS_sha1("KEY", 3, labelhash);
   
  -    if (ret == 0)
  -     ret = TPM_SaveContext_UseRoom(handle,
  -                                   TPM_RT_KEY,
  -                                   (char *) labelhash, &context);
  +    ret = TPM_SaveContext_UseRoom(handle, TPM_RT_KEY,
  +                           (char *) labelhash, &context);
  +    if (ret)
  +     goto exit;
   
  -    if (ret == 0) {
  -     FILE *f = fopen(filename, "w+");
  -     if (f) {
  -         int n = fwrite(context.buffer, context.used, 1, f);
  -         (void) n;
  -         if (fclose(f) != 0)
  -             ret = ERR_BAD_FILE_CLOSE;
  -     } else
  -         ret = ERR_BAD_FILE;
  +    fp = fopen(filename, "w+");
  +    if (fp == NULL) {
  +     ret = ERR_BAD_FILE;
  +     goto exit;
  +    }
  +    if (fwrite(context.buffer, context.used, 1, fp) != 1) {
  +     ret = ERR_BAD_FILE_WRITE;
  +     goto exit;
  +    }
  +    if (fclose(fp) != 0) {
  +     ret = ERR_BAD_FILE_CLOSE;
  +     fp = NULL;
  +     goto exit;
       }
  +    fp = NULL;
   
       if (ret == 0) {
        /*ret = */ TPM_EvictKey_UseRoom(handle);
  @@ -123,6 +130,11 @@
        printf("Could NOT swap out key with handle %08x.\n", handle);
   #endif
   
  +exit:
  +    if (fp)
  +     fclose(fp);
  +    if (filename)
  +     free(filename);
       return ret;
   }
   
  @@ -135,8 +147,10 @@
       uint32_t newhandle;
       uint32_t ret;
   
  -    if (filename == NULL)
  +    if (filename == NULL) {
        ret = ERR_MEM_ERR;
  +     goto exit;
  +    }
   
       ret = TPM_ReadFile(filename, &mycontext, &contextSize);
       if (ret & ERR_MASK) {
  @@ -146,10 +160,11 @@
   #if 0
        fprintf(stderr, "Could not read from keyfile %s.\n", filename);
   #endif
  -     return ret;
  +     goto exit;
       }
       SET_TPM_BUFFER(&context, mycontext, contextSize);
       free(mycontext);
  +    mycontext = NULL;
   
       ret = TPM_LoadContext(handle, 1, &context, &newhandle);
   
  @@ -162,7 +177,6 @@
               newhandle, handle);
       if (ret == 0)
        unlink(filename);
  -    free(filename);
   #if 0
       if (ret == 0)
        fprintf(stderr, "SWAP IN Swapped in key with handle %08x.\n",
  @@ -172,6 +186,11 @@
                handle);
   #endif
   
  +exit:
  +    if (mycontext)
  +     free(mycontext);
  +    if (filename)
  +     free(filename);
       return ret;
   }
   
  @@ .
______________________________________________________________________
RPM Package Manager                                    http://rpm5.org
CVS Sources Repository                                rpm-cvs@rpm5.org

Reply via email to