krb5 commit: Simplify db2 principal marshalling

ghudson at mit.edu ghudson at mit.edu
Mon Nov 23 23:52:44 EST 2026


https://github.com/krb5/krb5/commit/33ab01ade3856eb33bf2e791c13e81b7625c4215
commit 33ab01ade3856eb33bf2e791c13e81b7625c4215
Author: Greg Hudson <ghudson at mit.edu>
Date:   Tue Nov 3 10:33:02 2026 -0500

    Simplify db2 principal marshalling
    
    In krb5_encode_princ_entry(), ignore the len, e_data, and e_length
    fields.  In krb5_decode_princ_entry(), error out if the marshalled
    length is not KRB5_KDB_V1_BASE_LENGTH, always set entry->len to that
    value, and never set e_data or e_length.  Add a test case supplied by
    Aysha Afrah Ziya.
    
    This change brings the db2 module into alignment with the other two
    modules.  The original database design apparently envisioned the base
    length as an extensibility mechanism, with e_data/e_length preserving
    any extra content found in entries written by future code.  But that
    mechanism could not work as originally written (reading an extended
    entry and writing it again would cause the original code to abort in
    krb5_encode_princ_entry()), and all extensions have instead been
    represented using tl-data values.  e_data has been repurposed as a
    private pointer for third-party KDB modules.
    
    This change incidentally fixes a bug in commit
    247b7a6e14252d24cb75ebb7c653bb6b8501b0b9 (not included in any release)
    where entry->len was checked too late to guard against overflowing the
    allocated buffer.

 src/plugins/kdb/db2/kdb_xdr.c | 35 ++++++-----------------------------
 src/tests/kdbtest.c           |  9 +++++++++
 2 files changed, 15 insertions(+), 29 deletions(-)

diff --git a/src/plugins/kdb/db2/kdb_xdr.c b/src/plugins/kdb/db2/kdb_xdr.c
index 7d04b267d..3206d53f1 100644
--- a/src/plugins/kdb/db2/kdb_xdr.c
+++ b/src/plugins/kdb/db2/kdb_xdr.c
@@ -70,12 +70,11 @@ krb5_encode_princ_entry(krb5_context context, krb5_data *content,
      *         [ attributes, max_life, max_renewable_life, expiration,
      *           pw_expiration, last_success, last_failed, fail_auth_count ]
      *         [ n_key_data, n_tl_data ]
-     * then XX bytes [ e_length ] for the extra data [ e_data ]
      * then XX bytes [ 2 for length + length for string ] for the principal,
      * then (4 [type + length] + tl_data_length) bytes per tl_data
      * then (4 + (4 + key_data_length) per key_data_contents) bytes per key_data
      */
-    content->length = entry->len + entry->e_length;
+    content->length = KRB5_KDB_V1_BASE_LENGTH;
 
     if ((retval = krb5_unparse_name(context, entry->princ, &unparse_princ)))
         return(retval);
@@ -118,7 +117,7 @@ krb5_encode_princ_entry(krb5_context context, krb5_data *content,
     nextloc = (unsigned char *)content->data;
 
     /* Base Length */
-    krb5_kdb_encode_int16(entry->len, nextloc);
+    krb5_kdb_encode_int16(KRB5_KDB_V1_BASE_LENGTH, nextloc);
     nextloc += 2;
 
     /* Attributes */
@@ -161,18 +160,6 @@ krb5_encode_princ_entry(krb5_context context, krb5_data *content,
     krb5_kdb_encode_int16(entry->n_key_data, nextloc);
     nextloc += 2;
 
-    if (entry->len != KRB5_KDB_V1_BASE_LENGTH) {
-        retval = KRB5_KDB_TRUNCATED_RECORD;
-        k5_setmsg(context, retval, _("KDB entry length must be at least %d"),
-                  KRB5_KDB_V1_BASE_LENGTH);
-    }
-
-    /* Any extra data that this version doesn't understand. */
-    if (entry->e_length) {
-        memcpy(nextloc, entry->e_data, entry->e_length);
-        nextloc += entry->e_length;
-    }
-
     /*
      * Now we get to the principal.
      * To squeze a few extra bytes out it is always assumed to come
@@ -260,6 +247,10 @@ krb5_decode_princ_entry(krb5_context context, krb5_data *content,
 
     /* Base Length */
     krb5_kdb_decode_int16(nextloc, entry->len);
+    if (entry->len != KRB5_KDB_V1_BASE_LENGTH) {
+        retval = KRB5_KDB_TRUNCATED_RECORD;
+        goto error_out;
+    }
     nextloc += 2;
 
     /* Attributes */
@@ -312,20 +303,6 @@ krb5_decode_princ_entry(krb5_context context, krb5_data *content,
         goto error_out;
     }
 
-    /* Check for extra data */
-    if (entry->len > KRB5_KDB_V1_BASE_LENGTH) {
-        entry->e_length = entry->len - KRB5_KDB_V1_BASE_LENGTH;
-        if (entry->e_length > (size_t)sizeleft) {
-            retval = KRB5_KDB_TRUNCATED_RECORD;
-            goto error_out;
-        }
-        entry->e_data = k5memdup(nextloc, entry->e_length, &retval);
-        if (entry->e_data == NULL)
-            goto error_out;
-        nextloc += entry->e_length;
-        sizeleft -= entry->e_length;
-    }
-
     /*
      * Get the principal name for the entry
      * (stored as a string which gets unparsed.)
diff --git a/src/tests/kdbtest.c b/src/tests/kdbtest.c
index 565d7912f..a728cee4c 100644
--- a/src/tests/kdbtest.c
+++ b/src/tests/kdbtest.c
@@ -391,6 +391,15 @@ main(void)
     CHECK(krb5_db_delete_principal(ctx, &sample_princ));
     CHECK(krb5_db_put_principal(ctx, ent));
 
+    /* Check that an entry length below the base record length is ignored.
+     * (Past versons of the DB2 module would crash instead.) */
+    ent->len = 1;
+    ent->mask = KADM5_KEY_DATA;
+    CHECK(krb5_db_put_principal(ctx, ent));
+    krb5_db_free_principal(ctx, ent);
+    CHECK(krb5_db_get_principal(ctx, &sample_princ, 0, &ent));
+    CHECK_COND(ent->len == KRB5_KDB_V1_BASE_LENGTH);
+
     /* Check that a zero-length db_args tl-data value is rejected. */
     CHECK(krb5_dbe_update_tl_data(ctx, ent, &tl_empty_db_args));
     CHECK_COND(krb5_db_put_principal(ctx, ent) == EINVAL);


More information about the cvs-krb5 mailing list