s3-smbd: Change allocation of smb_acl_t to talloc()
authorAndrew Bartlett <abartlet@samba.org>
Sun, 12 Aug 2012 10:41:35 +0000 (20:41 +1000)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 15 Aug 2012 01:44:43 +0000 (11:44 +1000)
The acl element is changed to be a talloc child, and is no longer one element
longer than requested by virtue of the acl[1] base pointer.

This also avoids one of the few remaining cases of over-allocation of a structure.

Andrew Bartlett

source3/include/smb_acls.h
source3/lib/sysacls.c
source3/modules/vfs_aixacl2.c
source3/modules/vfs_aixacl_util.c
source3/modules/vfs_gpfs.c
source3/modules/vfs_hpuxacl.c
source3/modules/vfs_posixacl.c
source3/modules/vfs_solarisacl.c
source3/modules/vfs_tru64acl.c

index 16bb61f6706ff37bd6ccf4751adcd3254e855588..4998e4b07d80336e5ab3a572344d0f646f5f12f0 100644 (file)
@@ -54,7 +54,7 @@ typedef struct smb_acl_t {
        int     size;
        int     count;
        int     next;
-       struct smb_acl_entry acl[1];
+       struct smb_acl_entry *acl;
 } *SMB_ACL_T;
 
 typedef struct smb_acl_entry   *SMB_ACL_ENTRY_T;
index 592aef6d43d67dffe2a035301e80147702143a7f..7e387e444bf449fa8d869f36785f3df5e01ad0dc 100644 (file)
@@ -258,15 +258,7 @@ SMB_ACL_T sys_acl_init(int count)
                return NULL;
        }
 
-       /*
-        * note that since the definition of the structure pointed
-        * to by the SMB_ACL_T includes the first element of the
-        * acl[] array, this actually allocates an ACL with room
-        * for (count+1) entries
-        */
-       if ((a = (struct smb_acl_t *)SMB_MALLOC(
-                    sizeof(struct smb_acl_t) +
-                    count * sizeof(struct smb_acl_entry))) == NULL) {
+       if ((a = talloc(NULL, struct smb_acl_t)) == NULL) {
                errno = ENOMEM;
                return NULL;
        }
@@ -275,6 +267,13 @@ SMB_ACL_T sys_acl_init(int count)
        a->count = 0;
        a->next = -1;
 
+       a->acl = talloc_array(a, struct smb_acl_entry, count+1);
+       if (!a->acl) {
+               TALLOC_FREE(a);
+               errno = ENOMEM;
+               return NULL;
+       }
+
        return a;
 }
 
@@ -357,7 +356,7 @@ int sys_acl_free_text(char *text)
 
 int sys_acl_free_acl(SMB_ACL_T acl_d) 
 {
-       SAFE_FREE(acl_d);
+       TALLOC_FREE(acl_d);
        return 0;
 }
 
index 3f13a6fa87f35678da837e15ef494b2e70f8acc2..dd705ea319e314e3e0f2f2df766da809101dfe68 100644 (file)
@@ -229,7 +229,7 @@ static SMB_ACL_T aixjfs2_get_posix_acl(const char *path, acl_type_t type)
 
  done:
         if (errno != 0) {
-                SAFE_FREE(result);
+                TALLOC_FREE(result);
         }
         return result;
 }
index b359c401efee2c651f4c7f4474417ccb280fc0d6..bd5ccbbdc2334103377b010918aed0de45d94d22 100644 (file)
@@ -27,14 +27,13 @@ SMB_ACL_T aixacl_to_smbacl(struct acl *file_acl)
        struct acl_entry *acl_entry;
        struct ace_id *idp;
        
-       struct smb_acl_t *result = SMB_MALLOC_P(struct smb_acl_t);
+       struct smb_acl_t *result = sys_acl_init(0);
        struct smb_acl_entry *ace;
        int i;
        
        if (result == NULL) {
                return NULL;
        }
-       ZERO_STRUCTP(result);
        
        /* Point to the first acl entry in the acl */
        acl_entry =  file_acl->acl_ext;
@@ -64,11 +63,9 @@ SMB_ACL_T aixacl_to_smbacl(struct acl *file_acl)
                        idp = acl_entry->ace_id;
                        DEBUG(10,("idp->id_data is %d\n",idp->id_data[0]));
                        
-                       result = SMB_REALLOC(result, sizeof(struct smb_acl_t) +
-                                    (sizeof(struct smb_acl_entry) *
-                                     (result->count+1)));
+                       result->acl = talloc_realloc(result, result->acl, result->count+1);
                        if (result == NULL) {
-                               DEBUG(0, ("SMB_REALLOC failed\n"));
+                               DEBUG(0, ("talloc_realloc failed\n"));
                                errno = ENOMEM;
                                return NULL;
                        }
@@ -117,7 +114,7 @@ SMB_ACL_T aixacl_to_smbacl(struct acl *file_acl)
                                break;
                        default:
                                DEBUG(0, ("unknown ace->type\n"));
-                               SAFE_FREE(result);
+                               TALLOC_FREE(result);
                                return(0);
                        }
                
@@ -141,15 +138,14 @@ SMB_ACL_T aixacl_to_smbacl(struct acl *file_acl)
        for( i = 1; i < 4; i++) {
                DEBUG(10,("i is %d\n",i));
 
-                       result = SMB_REALLOC(result, sizeof(struct smb_acl_t) +
-                                    (sizeof(struct smb_acl_entry) *
-                                     (result->count+1)));
-                       if (result == NULL) {
-                               DEBUG(0, ("SMB_REALLOC failed\n"));
-                               errno = ENOMEM;
-                               DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
-                               return NULL;
-                       }
+               result->acl = talloc_realloc(result, result->acl, result->count+1);
+               if (result->acl == NULL) {
+                       TALLOC_FREE(result);
+                       DEBUG(0, ("talloc_realloc failed\n"));
+                       errno = ENOMEM;
+                       DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
+                       return NULL;
+               }
                        
                ace = &result->acl[result->count];
                
index 4e4df22ebebdc1cb9cb5ed8e49fe82cfd833f945..874d00d048ce6fb273c3510e828c6d5e0ec60812 100644 (file)
@@ -594,7 +594,7 @@ static SMB_ACL_T gpfs2smb_acl(const struct gpfs_acl *pacl)
                        DEBUG(10, ("Got invalid ace_type: %d\n",
                                   g_ace->ace_type));
                        errno = EINVAL;
-                       SAFE_FREE(result);
+                       TALLOC_FREE(result);
                        return NULL;
                }
 
@@ -648,7 +648,7 @@ static SMB_ACL_T gpfsacl_get_posix_acl(const char *path, gpfs_aclType_t type)
  done:
 
        if (errno != 0) {
-               SAFE_FREE(result);
+               TALLOC_FREE(result);
        }
        return result;  
 }
index 1b5d8d087d3159fc59866482e99faf519b06cba9..f8661b1134ca00c441752a46371e0d224f78d320 100644 (file)
@@ -386,7 +386,7 @@ int hpuxacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
  done:
        DEBUG(10, ("hpuxacl_sys_acl_delete_def_file %s.\n",
                   ((ret != 0) ? "failed" : "succeeded" )));
-       SAFE_FREE(smb_acl);
+       TALLOC_FREE(smb_acl);
        return ret;
 }
 
@@ -506,11 +506,8 @@ static SMB_ACL_T hpux_acl_to_smb_acl(HPUX_ACL_T hpux_acl, int count,
                if (!_IS_OF_TYPE(hpux_acl[i], type)) {
                        continue;
                }
-               result = SMB_REALLOC(result, 
-                                    sizeof(struct smb_acl_t) +
-                                    (sizeof(struct smb_acl_entry) *
-                                     (result->count + 1)));
-               if (result == NULL) {
+               result->acl = talloc_realloc(result, result->acl, struct smb_acl_entry, result->count + 1);
+               if (result->acl == NULL) {
                        DEBUG(10, ("error reallocating memory for SMB_ACL\n"));
                        goto fail;
                }
@@ -534,7 +531,7 @@ static SMB_ACL_T hpux_acl_to_smb_acl(HPUX_ACL_T hpux_acl, int count,
        }
        goto done;
  fail:
-       SAFE_FREE(result);
+       TALLOC_FREE(result);
  done:
        DEBUG(10, ("hpux_acl_to_smb_acl %s\n",
                   ((result == NULL) ? "failed" : "succeeded")));
index d304f6fe8eecc47eb609c50efbab115eb49f39a9..407a3a1724f7f8733b6e04c578aac696dfdfe52d 100644 (file)
@@ -214,28 +214,27 @@ static bool smb_ace_to_internal(acl_entry_t posix_ace,
 
 static struct smb_acl_t *smb_acl_to_internal(acl_t acl)
 {
-       struct smb_acl_t *result = SMB_MALLOC_P(struct smb_acl_t);
+       struct smb_acl_t *result = sys_acl_init(0);
        int entry_id = ACL_FIRST_ENTRY;
        acl_entry_t e;
        if (result == NULL) {
                return NULL;
        }
-       ZERO_STRUCTP(result);
        while (acl_get_entry(acl, entry_id, &e) == 1) {
 
                entry_id = ACL_NEXT_ENTRY;
 
-               result = (struct smb_acl_t *)SMB_REALLOC(
-                       result, sizeof(struct smb_acl_t) +
-                       (sizeof(struct smb_acl_entry) * (result->count+1)));
-               if (result == NULL) {
-                       DEBUG(0, ("SMB_REALLOC failed\n"));
+               result->acl = talloc_realloc(result, result->acl, 
+                                            struct smb_acl_entry, result->count+1);
+               if (result->acl == NULL) {
+                       TALLOC_FREE(result);
+                       DEBUG(0, ("talloc_realloc failed\n"));
                        errno = ENOMEM;
                        return NULL;
                }
 
                if (!smb_ace_to_internal(e, &result->acl[result->count])) {
-                       SAFE_FREE(result);
+                       TALLOC_FREE(result);
                        return NULL;
                }
 
index 598f25f7f8f811b6e245fbb82b643e22b0db2b2f..ff9f1a62e33c405d40195096e8ed73f7ad805f10 100644 (file)
@@ -323,7 +323,7 @@ int solarisacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
  done:
        DEBUG(10, ("solarisacl_sys_acl_delete_def_file %s.\n",
                   ((ret != 0) ? "failed" : "succeeded" )));
-       SAFE_FREE(smb_acl);
+       TALLOC_FREE(smb_acl);
        return ret;
 }
 
@@ -440,11 +440,8 @@ static SMB_ACL_T solaris_acl_to_smb_acl(SOLARIS_ACL_T solaris_acl, int count,
                if (!_IS_OF_TYPE(solaris_acl[i], type)) {
                        continue;
                }
-               result = SMB_REALLOC(result, 
-                                    sizeof(struct smb_acl_t) +
-                                    (sizeof(struct smb_acl_entry) *
-                                     (result->count + 1)));
-               if (result == NULL) {
+               result->acl = talloc_realloc(result, result->acl, struct smb_acl_entry, result->count + 1);
+               if (result->acl == NULL) {
                        DEBUG(10, ("error reallocating memory for SMB_ACL\n"));
                        goto fail;
                }
@@ -469,7 +466,7 @@ static SMB_ACL_T solaris_acl_to_smb_acl(SOLARIS_ACL_T solaris_acl, int count,
        goto done;
        
  fail:
-       SAFE_FREE(result);
+       TALLOC_FREE(result);
  done:
        DEBUG(10, ("solaris_acl_to_smb_acl %s\n",
                   ((result == NULL) ? "failed" : "succeeded")));
index 3f91a4753a2f6eaa007bee143619021c71c58e26..09f8c3933f6b2d1a5f90a03a35390ec5fe1f134f 100644 (file)
@@ -160,28 +160,27 @@ static struct smb_acl_t *tru64_acl_to_smb_acl(const struct acl *tru64_acl)
 
        DEBUG(10, ("Hi! This is tru64_acl_to_smb_acl.\n"));
        
-       if ((result = SMB_MALLOC_P(struct smb_acl_t)) == NULL) {
-               DEBUG(0, ("SMB_MALLOC_P failed in tru64_acl_to_smb_acl\n"));
+       if ((result = sys_acl_init(0)) == NULL) {
+               DEBUG(0, ("sys_acl_init() failed in tru64_acl_to_smb_acl\n"));
                errno = ENOMEM;
                goto fail;
        }
-       ZERO_STRUCTP(result);
        if (acl_first_entry((struct acl *)tru64_acl) != 0) {
                DEBUG(10, ("acl_first_entry failed: %s\n", strerror(errno)));
                goto fail;
        }
        while ((entry = acl_get_entry((struct acl *)tru64_acl)) != NULL) {
-               result = SMB_REALLOC(result, sizeof(struct smb_acl_t) +
-                                       (sizeof(struct smb_acl_entry) * 
-                                        (result->count + 1)));
-               if (result == NULL) {
-                       DEBUG(0, ("SMB_REALLOC failed in tru64_acl_to_smb_acl\n"));
+               result->acl = talloc_realloc(result, result->acl, struct smb_acl_entry, 
+                                            result->count + 1);
+               if (result->acl == NULL) {
+                       TALLOC_FREE(result);
+                       DEBUG(0, ("talloc_realloc failed in tru64_acl_to_smb_acl\n"));
                        errno = ENOMEM;
                        goto fail;
                }
                /* XYZ */
                if (!tru64_ace_to_smb_ace(entry, &result->acl[result->count])) {
-                       SAFE_FREE(result);
+                       TALLOC_FREE(result);
                        goto fail;
                }
                result->count += 1;
@@ -189,9 +188,7 @@ static struct smb_acl_t *tru64_acl_to_smb_acl(const struct acl *tru64_acl)
        return result;
 
 fail:
-       if (result != NULL) {
-               SAFE_FREE(result);
-       }
+       TALLOC_FREE(result);
        DEBUG(1, ("tru64_acl_to_smb_acl failed!\n"));
        return NULL;
 }