Bug 16087 (CVE-2026-58216) - [SECURITY] CVE-2026-58216: kpasswd service: 6-byte heap OOB read in packet parser
Summary: [SECURITY] CVE-2026-58216: kpasswd service: 6-byte heap OOB read in packet pa...
Status: RESOLVED FIXED
Alias: CVE-2026-58216
Product: Samba 4.1 and newer
Classification: Unclassified
Component: AD: LDB/DSDB/SAMDB (show other bugs)
Version: unspecified
Hardware: All All
: P5 normal (vote)
Target Milestone: ---
Assignee: Douglas Bagnall
QA Contact: Samba QA Contact
URL:
Keywords:
Depends on:
Blocks: 16039
  Show dependency treegraph
 
Reported: 2026-05-27 02:22 UTC by Douglas Bagnall
Modified: 2026-08-13 14:31 UTC (History)
2 users (show)

See Also:


Attachments
original report (1.66 KB, text/markdown)
2026-05-27 02:22 UTC, Douglas Bagnall
no flags Details
draft advisory (1.96 KB, text/plain)
2026-06-05 04:31 UTC, Douglas Bagnall
no flags Details
patch for master (1.20 KB, patch)
2026-06-05 04:32 UTC, Douglas Bagnall
metze: review+
metze: ci-passed+
Details
test that fails to crash (4.56 KB, patch)
2026-06-05 04:37 UTC, Douglas Bagnall
no flags Details
advisory v2 (adding CVE, credit) (2.01 KB, text/plain)
2026-07-08 23:42 UTC, Douglas Bagnall
metze: review+
Details
advisory with fixed score value (2.01 KB, text/plain)
2026-07-20 14:51 UTC, Björn Jacke
metze: review+
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Douglas Bagnall 2026-05-27 02:22:07 UTC
Created attachment 18994 [details]
original report

Tristan (TristanInSec@gmail.com) reports (as attached):

The kpasswd request parser in `source4/kdc/kpasswd-service.c` miscalculates the length of the encrypted data portion. The kpasswd packet format (RFC 3244) is:


  [2-byte length][2-byte version][2-byte AP-REQ length][AP-REQ][encrypted data]
  \_________________ HEADER_LEN = 6 __________________/


At line 140, the encrypted data length is computed as:

  enc_data_len = len - ap_req_len;    // WRONG: includes HEADER_LEN

The correct calculation:

  enc_data_len = len - HEADER_LEN - ap_req_len;

Since the blob starts at offset `HEADER_LEN + ap_req_len` but its length includes the 6 header bytes, it extends 6 bytes past the end of the request buffer. This blob is passed to `gensec_unwrap()` at line 270, which reads the full contents including the OOB bytes.

## Example

With `len=100` and `ap_req_len=50`:
- `enc_data_len = 100 - 50 = 50` (should be 44)
- Blob starts at offset 56, extends to offset 105
- Buffer ends at offset 99
- 6 bytes read past buffer end

## Impact

6-byte heap OOB read. The extra bytes are processed by the GENSEC layer during decryption. The decryption will fail (corrupted ciphertext), but the read occurs before the failure. If the buffer sits at a page boundary, this can crash the AD DC KDC process.
Comment 1 Douglas Bagnall 2026-05-27 02:39:01 UTC
Tristan suggests 

 CVSS 3.1: 4.3 (AV:N/AC:L/PR:L/UI:N/S:U/C:L/I:N/A:N)

which is saying an unprivileged user can easily leak a little bit of process memory.

It is not clear to me how the extra six bytes might be revealed to the user after gensec_unwrap() fails.

The other claim remains: 

> If the buffer sits at a page boundary, this can crash the AD DC KDC process.
Comment 2 Douglas Bagnall 2026-05-28 00:42:59 UTC
(In reply to Douglas Bagnall from comment #1)
> It is not clear to me how the extra six bytes might be revealed to the user after gensec_unwrap() fails.

In particular, from line 350

		code = smb_krb5_mk_error(kdc->smb_krb5_context->krb5_context,
					 KRB5KDC_ERR_NONE + error_code,
					 NULL, /* e_text */
					 &k_dec_data,
					 NULL, /* client */
					 server_principal,
					 &k_enc_data);
		krb5_free_principal(kdc->smb_krb5_context->krb5_context,
				    server_principal);
		if (code != 0) {
			DBG_ERR("Failed to create krb5 error reply: %s\n",
				error_message(code));
			goto done;
		}

		enc_data_blob = data_blob_talloc(tmp_ctx,
						 k_enc_data.data,
						 k_enc_data.length);

enc_data_blob is overwritten with k_enc_data.

Tracing through, I do not see the trailing memory from the earlier enc_data_blob in *reply.
Comment 3 TristanInSec 2026-05-28 22:29:10 UTC
Good analysis. You are right -- I had not traced past the gensec_unwrap() failure. The enc_data_blob is overwritten at line 364 with the Kerberos error reply from smb_krb5_mk_error(), so the 6 OOB bytes never reach the client. C:L was wrong in my original scoring.

The residual risk is the OOB read itself: the 6 bytes are always read by gensec_unwrap (AC:L), but since the data does not leak, the only impact is DoS if the allocation sits at a page boundary (which would make it AC:H for the crash).

The off-by-6 calculation is still a clear bug (enc_data_len = len - ap_req_len should be enc_data_len = len - HEADER_LEN - ap_req_len), and in a security-sensitive component like the KDC, any OOB read is worth patching regardless of whether it meets the advisory threshold. Happy if it is fixed as a regular bug.
Comment 4 Douglas Bagnall 2026-05-29 02:05:15 UTC
(In reply to TristanInSec from comment #3)
We think usually gensec_unwrap() does not fail (the KRB_PRIV parser in Heimdal doesn't read the trailing bytes, it follows the ASN.1 in the packet).
Comment 5 TristanInSec 2026-05-29 20:59:18 UTC
(In reply to Douglas Bagnall from comment #4)

The ASN.1-bounded parsing is a fair point for the common case. Two things though:

1. "Usually" -- are there GENSEC mechanisms or code paths where the full blob length is consumed rather than the ASN.1-defined length?

2. Regardless of the current consumer, the DATA_BLOB carries the wrong length. Any future code that trusts .length (a copy, a log, a different GENSEC backend) would inherit the off-by-6. For a one-line fix in the KDC, the cost of not fixing it is higher than fixing it.
Comment 6 Douglas Bagnall 2026-05-31 02:57:39 UTC
(In reply to TristanInSec from comment #5)
I am not sure other backends matter, given kpasswd_process says:

	status = gensec_start_mech_by_name(gensec_security, "krb5");

and is itself only called from source4/kdc/kdc-heimdal.c.

But we shouldn't care about that. If a crafted packet can crash the server, we are going to fix it as a security bug anyway.
Comment 7 Douglas Bagnall 2026-06-05 04:31:27 UTC
Created attachment 19008 [details]
draft advisory
Comment 8 Douglas Bagnall 2026-06-05 04:32:46 UTC
Created attachment 19009 [details]
patch for master
Comment 9 Douglas Bagnall 2026-06-05 04:37:53 UTC
Created attachment 19010 [details]
test that fails to crash

I have added an advisory and patch, and a test that never actually causes a crash. It may be that memory allocation patterns on my machine never allow a segfault, or it could be that I am just not trying properly.
Comment 10 Douglas Bagnall 2026-06-24 00:11:48 UTC
Tristan, how do you want to be credited?

Do you want us to mention a surname or company name?
Comment 11 TristanInSec 2026-06-25 22:45:39 UTC
(In reply to Douglas Bagnall from comment #10)

Hi,

Thank you for asking, it is greatly appreciated.

Could you please use: "Tristan Madani"

Or, if a longer attribution is appropriate:
"Tristan Madani (@TristanInSec), Talence Security"

If you provide backlinks, the website is:
https://talencesecurity.com

Best,
Tristan
Comment 12 Douglas Bagnall 2026-07-08 23:42:15 UTC
Created attachment 19077 [details]
advisory v2 (adding CVE, credit)
Comment 13 Stefan Metzmacher 2026-07-09 08:00:56 UTC
Comment on attachment 19009 [details]
patch for master

>From 85cc88b97ef8fa4f54d9432c2730d748feb7b8b0 Mon Sep 17 00:00:00 2001
>From: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
>Date: Sun, 31 May 2026 12:48:11 +1200
>Subject: [PATCH] kdc:kpasswd: calculate correct size for password blob
>
>We were making the enc_data_blob 6 bytes too big.
>
>Its payload is an ASN.1 structure that knows its own size, so the
>extra bytes are not usually read by Heimdal, but a crafted packet
>could force them to be read.
>
>BUG: https://bugzilla.samba.org/show_bug.cgi?id=16087
>
>Reported-by: Tristan <TristanInSec@gmail.com>
>Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
>---
> source4/kdc/kpasswd-service.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
>diff --git a/source4/kdc/kpasswd-service.c b/source4/kdc/kpasswd-service.c
>index c671eb46d07..0284fb4f133 100644
>--- a/source4/kdc/kpasswd-service.c
>+++ b/source4/kdc/kpasswd-service.c
>@@ -137,7 +137,7 @@ kdc_code kpasswd_process(struct kdc_server *kdc,
> 
> 	ap_req_blob = data_blob_const(&request->data[HEADER_LEN], ap_req_len);
> 
>-	enc_data_len = len - ap_req_len;
>+	enc_data_len = len - (ap_req_len + HEADER_LEN);
> 	enc_data_blob = data_blob_const(&request->data[HEADER_LEN + ap_req_len],
> 					enc_data_len);
> 
>-- 
>2.43.0
>
Comment 14 Douglas Bagnall 2026-07-10 02:00:18 UTC
patch for master applies back to 4.7.
Comment 15 Björn Jacke 2026-07-17 13:43:14 UTC
adding vendors here.

scheduled release 2026-07-28 10:00 UTC.
Comment 16 Andrea Mattiazzo 2026-07-20 08:36:31 UTC
Probably a typo also here, CVSS:3.1/AV:N/AC:H/PR:L/UI:N/S:U/C:N/I:N/A:H results in a 5.3 score, not 6.5
Comment 17 Björn Jacke 2026-07-20 14:51:59 UTC
Created attachment 19122 [details]
advisory with fixed score value
Comment 18 Douglas Bagnall 2026-07-21 08:19:04 UTC
(In reply to Björn Jacke from comment #17)
I think the typo might be more in claiming /AC:H/ rather than /AC:L/ (which would be 6.5), but whichever.
Comment 19 Samba QA Contact 2026-07-28 10:24:12 UTC
This bug was referenced in samba v4-24-stable (Release samba-4.24.5):

1ab79135f5b803c99b06289378d090f69821f066
Comment 20 Samba QA Contact 2026-07-28 10:24:36 UTC
This bug was referenced in samba v4-22-stable (Release samba-4.22.11):

5ce6e63978f62daafaab44ee77284d72a0603225
Comment 21 Samba QA Contact 2026-07-28 10:24:41 UTC
This bug was referenced in samba v4-23-stable (Release samba-4.23.10):

4fb7188bd7cc1e15d88c58541a561ddaa3ae0a37
Comment 22 Samba QA Contact 2026-07-28 10:49:52 UTC
This bug was referenced in samba v4-22-test:

5ce6e63978f62daafaab44ee77284d72a0603225
Comment 23 Samba QA Contact 2026-07-28 12:15:29 UTC
This bug was referenced in samba v4-24-test:

d25f3ff0834daeb612e454e84ac325ed743f38ea
Comment 24 Samba QA Contact 2026-07-28 12:27:28 UTC
This bug was referenced in samba v4-23-test:

91c9696e2dada5160fea5f32764ca5ba7271284c
Comment 25 Samba QA Contact 2026-07-28 14:41:26 UTC
This bug was referenced in samba v4-24-test:

1ab79135f5b803c99b06289378d090f69821f066
Comment 26 Samba QA Contact 2026-07-28 14:44:09 UTC
This bug was referenced in samba v4-23-test:

4fb7188bd7cc1e15d88c58541a561ddaa3ae0a37
Comment 27 Samba QA Contact 2026-07-28 17:03:12 UTC
This bug was referenced in samba master:

7a8fb86ae8cc907f2d26049dbf71c561c40a8de6
Comment 28 Samba QA Contact 2026-08-03 19:36:53 UTC
This bug was referenced in samba v4-23-stable (Release samba-4.23.11):

91c9696e2dada5160fea5f32764ca5ba7271284c
Comment 29 Samba QA Contact 2026-08-13 14:31:11 UTC
This bug was referenced in samba v4-24-stable (Release samba-4.24.6):

d25f3ff0834daeb612e454e84ac325ed743f38ea