🐛 [BUG] - SA Rekey Silently Discards the Managed New-IV Field When the Target SA Has `shivf_len == 0` — Empty `else { // TODO }`, Then Reports Success

Abierto
#552 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
70/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
c

Línea de trabajo

Start in src/sa/internal/sa_interface_inmemory.template.c at sa_rekey(), especially lines 1313-1358, and compare the parsing behavior with configs/yaml/ep_mp.yaml and configs/ksy-templates/ep/ep_sa_management.ksy. Reproduce the SPI 0 sequence from the issue and inspect the IV output. Done means the fixed New-IV field is handled consistently when shivf_len is zero and the reported result matches the command data.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

bug
Description

Bug Report: SA Rekey Silently Discards the Managed New-IV Field When the Target SA Has shivf_len == 0 — Empty else { // TODO }, Then Reports Success

Summary

Field Value
Product NASA CryptoLib (SDLS Protocol Implementation)
Version 1.5.0
Component src/sa/internal/sa_interface_inmemory.template.c — sa_rekey()
Issue The Rekey SA command data field carries a fixed-size, mission-managed New IV (ep_iv_length, 12 bytes). sa_rekey only consumes it when the target SA's shivf_len > 0; when shivf_len == 0 the else branch is an empty { // Set SN / // TODO } that reads nothing, so the New-IV bytes present in the PDU are silently dropped. The SA is nonetheless transitioned to SA_KEYED and the function returns CRYPTO_LIB_SUCCESS.
Bug Type Conformance / silent data loss
Severity Medium — a Rekey command is accepted and acknowledged as successful while a managed field it carried is discarded with no indication to the caller
Impact A ground segment that rekeys such an SA and supplies a New IV receives success, but the IV it sent is not applied. Combined with sa_rekey's unconditional return CRYPTO_LIB_SUCCESS (see BUG-005), there is no way for the caller to learn the field was ignored. The empty branch is also a latent silent-drop for any non-zero-length managed field that should have been consumed there.

Description

Per CCSDS 355.1-B-1 §5.5.1.4 (figure 5-14), the Rekey SA command's data field is
SPI | New Enc Key ID | New Auth Key ID | New ARSN(managed) | New IV(managed), where the
managed sizes are mission-level Extended-Procedures parameters — the same on every Rekey
PDU on the link — not properties of the SA being rekeyed. In this project's configuration the
managed New IV is 12 bytes (configs/yaml/ep_mp.yaml: ep_iv_length: 12), and the KSY models it
as a fixed field (configs/ksy-templates/ep/ep_sa_management.ksy, rekey_sa_cmd_t:
- id: new_iv / size: 12).

sa_rekey, however, decides whether to consume that field from the target SA's shivf_len:

if (sa[spi].sa_state == SA_UNKEYED)                                    // :1300
{   // Encryption Key
    sa[spi].ekid = ...; count += 2;                                    // :1302-1304
    sa[spi].akid = ...; count += 2;                                    // :1305-1307

    if (sa[spi].shivf_len > 0)                                         // :1313
    {   // Set IV - authenticated encryption
        for (x = count; x < (sa[spi].shivf_len + count); x++)
        {
            *(sa[spi].iv + x - count) = (uint8_t)sdls_frame.tlv_pdu.data[x];   // :1315-1323
        }
    }
    else
    {   // Set SN
        // TODO                                                        // :1326-1327  *** New IV discarded ***
    }

    sa[spi].sa_state = SA_KEYED;                                       // :1334
}
...
return CRYPTO_LIB_SUCCESS;                                             // :1358

(src/sa/internal/sa_interface_inmemory.template.c:1300-1358)

When the target SA has shivf_len == 0, the guard at :1313 is false and control enters the
empty else at :1325-1328. Nothing reads the 12 New-IV bytes the PDU carries; count is not
even advanced. The SA is then set SA_KEYED at :1334 and success is returned at :1358.

Two further weaknesses in the same block, neither the primary defect but both worth noting:

  • The consuming loop at :1315 is bounded by shivf_len (the transmitted IV portion) rather
    than iv_len (the full IV), and is never checked against the number of bytes actually present
    in the PDU — so even on the taken branch the field's fixed managed length is not what governs
    the copy.
  • :1358 returns CRYPTO_LIB_SUCCESS unconditionally (the spi >= NUM_SA and "not UNKEYED"
    paths reach it too). This is the defect already filed as BUG-005 for sa_rekey/sa_start/
    sa_expire; it compounds the present bug by removing the last opportunity to signal that the
    New IV was dropped, but it is a separate finding.

Affected Code

  • src/sa/internal/sa_interface_inmemory.template.c:1325-1328 (1.5.0) — the empty
    else { // Set SN / // TODO } that discards the managed New IV
  • :1313-1323 (1.5.0) — the shivf_len-gated (not iv_len-gated) consume path
Affected Versions

Byte-for-byte identical else { // Set SN / // TODO } in sa_rekey across every version
inspected:

Version Location
1.5.0 :1325-1328 (guard :1313, SA_KEYED :1334, return :1358)
1.4.2 :1325-1328 (// Set SN at :1326)
1.3.0 :846-861 (// Set SN at :859)

Reproduction (from the fuzzing logs)

An 8-frame TC sequence on VCID 4 (TC_SDLS_EP_VCID). Packets 1–5 create/rekey/start SPI 63 and
OTAR+activate key 130; packets 6–7 stop and expire SPI 0 over SPI 63, leaving SPI 0 (the
plaintext-passthrough SA, est=0, ast=0, shivf_len=0, iv_len=0) in the UNKEYED state. Packet 8
is an SA Rekey of SPI 0, carried inside an authenticated-encrypted frame on SPI 63.

Packet 8 (002c103a0000003f000000000000000000000000ddf2a2aed81d61011356893e1cf8f3a91233ecc587741b22ce31bda4364befd9612d3f0cda7d48), decrypted SA Rekey PDU:

	 TLV PDU
		 type       = 0x0
		 uf         = 0x0
		 sg         = 0x1
		 pid        = 0x6            <-- PID_REKEY_SA
		 pdu_len    = 0x0090         <-- 18 bytes: spi(2) + ekid(2) + akid(2) + New IV(12)
		 data[0]    = 0x00
		 data[1]    = 0x00           <-- target SPI = 0

SA Rekey
SPI 0 IV updated to: 0x              <-- no hex digits: zero IV bytes were copied
SPI 0 changed to KEYED state with encrypted Key ID 130.
	 spi  = 0
	 ekid = 130
Status code: 0

The decisive line is SPI 0 IV updated to: 0x with nothing after it: the PDU carried 12 New-IV
bytes, sa_rekey copied none of them (the empty else at :1325-1328, taken because
shivf_len == 0), yet still transitioned SPI 0 to KEYED and returned success.


Discovered using the StratoFuzz.

Branch Name

dev

Reproduction steps
look above
Screenshots

Logs

OS

Linux

Lenguaje dominante
C
Estrellas
178
Forks
70
Merge medio
1 h 32 min
PR fusionados (30 d)
1

Preparar el entorno

  • Sin Dockerfile ni archivo de Docker Compose
  • Tiene una plantilla de pull request
  • Sin guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de nasa/CryptoLib

Todos los issues de nasa/CryptoLib

Issues similares

Más issues de C

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.