| 1 |
/* SPDX-License-Identifier: Apache-2.0 */ |
| 2 |
|
| 3 |
/* gss-token-util.h |
| 4 |
* |
| 5 |
* Copyright 2026 Rainer Gerhards and Adiscon GmbH. |
| 6 |
* |
| 7 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 |
* you may not use this file except in compliance with the License. |
| 9 |
* You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
*/ |
| 13 |
|
| 14 |
#ifndef GSS_TOKEN_UTIL_H_INCLUDED |
| 15 |
#define GSS_TOKEN_UTIL_H_INCLUDED 1 |
| 16 |
|
| 17 |
/** |
| 18 |
* @file gss-token-util.h |
| 19 |
* @brief Shared helpers for bounded GSS token framing in rsyslog. |
| 20 |
* |
| 21 |
* This header centralizes small, header-only helpers that validate GSS wire |
| 22 |
* framing and derive conservative token-size limits. It exists so the common |
| 23 |
* GSS runtime helper and both GSS modules use the same length-decoding and |
| 24 |
* size-bound logic instead of open-coding slightly different checks. |
| 25 |
* |
| 26 |
* Current users include: |
| 27 |
* - `runtime/gss-misc.c` for framed token receive time/size enforcement |
| 28 |
* - `plugins/imgssapi/imgssapi.c` for server-side handshake/message limits |
| 29 |
* - `plugins/omgssapi/omgssapi.c` for client-side handshake limits |
| 30 |
*/ |
| 31 |
|
| 32 |
#include <limits.h> |
| 33 |
#include <stddef.h> |
| 34 |
#include <stdint.h> |
| 35 |
|
| 36 |
#include "rsyslog.h" |
| 37 |
|
| 38 |
#define GSS_TOKEN_MAX_HANDSHAKE_BYTES (1024U * 1024U) |
| 39 |
#define GSS_TOKEN_MAX_WRAP_OVERHEAD_BYTES (64U * 1024U) |
| 40 |
#define GSS_TOKEN_IO_TIMEOUT_SECS 10U |
| 41 |
|
| 42 |
/** |
| 43 |
* @brief Decode a 4-byte GSS wire length field and validate it against a limit. |
| 44 |
* |
| 45 |
* GSS token framing in rsyslog uses a fixed 4-byte network-order length prefix. |
| 46 |
* Callers use this helper before allocating buffers or attempting to read token |
| 47 |
* bodies from the peer. |
| 48 |
* |
| 49 |
* @param[in] lenbuf Four bytes containing the network-order token length. |
| 50 |
* @param[in] max_len Maximum token size the caller is willing to accept. |
| 51 |
* @param[out] token_len Decoded token length on success. |
| 52 |
* |
| 53 |
* @retval RS_RET_OK Length decoded and within @p max_len. |
| 54 |
* @retval RS_RET_INVALID_PARAMS @p token_len is NULL. |
| 55 |
* @retval RS_RET_INVALID_VALUE Decoded length exceeds @p max_len. |
| 56 |
*/ |
| 57 |
static inline rsRetVal gssTokenDecodeLength(const unsigned char lenbuf[4], size_t max_len, size_t *const token_len) { |
| 58 |
const uint32_t wire_len = |
| 59 |
((uint32_t)lenbuf[0] << 24) | ((uint32_t)lenbuf[1] << 16) | ((uint32_t)lenbuf[2] << 8) | (uint32_t)lenbuf[3]; |
| 60 |
|
| 61 |
if (token_len == NULL) { |
| 62 |
return RS_RET_INVALID_PARAMS; |
| 63 |
} |
| 64 |
|
| 65 |
*token_len = (size_t)wire_len; |
| 66 |
if ((size_t)wire_len > max_len) { |
| 67 |
return RS_RET_INVALID_VALUE; |
| 68 |
} |
| 69 |
|
| 70 |
return RS_RET_OK; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* @brief Derive a safe wrapped-token size limit from a plaintext message limit. |
| 75 |
* |
| 76 |
* Wrapped GSS messages carry protocol overhead beyond the unwrapped syslog |
| 77 |
* payload. Receivers use this helper to cap the framed token size while still |
| 78 |
* accepting valid wrapped messages for a given plaintext maximum. |
| 79 |
* |
| 80 |
* @param[in] max_plaintext_len Maximum accepted plaintext message length. |
| 81 |
* |
| 82 |
* @return The wrapped-token limit derived from @p max_plaintext_len. If adding |
| 83 |
* the configured overhead would overflow `size_t`, `SIZE_MAX` is |
| 84 |
* returned instead. |
| 85 |
*/ |
| 86 |
static inline size_t gssTokenGetMessageLimit(size_t max_plaintext_len) { |
| 87 |
if (max_plaintext_len > SIZE_MAX - GSS_TOKEN_MAX_WRAP_OVERHEAD_BYTES) { |
| 88 |
return SIZE_MAX; |
| 89 |
} |
| 90 |
|
| 91 |
return max_plaintext_len + GSS_TOKEN_MAX_WRAP_OVERHEAD_BYTES; |
| 92 |
} |
| 93 |
|
| 94 |
#endif |