new file mode 100644
@@ -0,0 +1,145 @@
+From 8a3ace26c32e1646456788dc471f4581c7ba0db3 Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= <michal@weblate.org>
+Date: Fri, 6 Nov 2026 10:00:38 +0100
+Subject: [PATCH] Size backup text hex buffer for encoded content
+
+(cherry picked from commit 6ea5148f35995aa8ca02b0c5e4654a125405e799)
+
+CVE: CVE-2026-138164
+Upstream-Status: Backport [https://github.com/gammu/gammu/commit/6ea5148f35995aa8ca02b0c5e4654a125405e799]
+
+Signed-off-by: Ankur Tyagi <ankur.tyagi85@gmail.com>
+---
+ libgammu/service/backup/backtext.c | 11 ++++-
+ tests/CMakeLists.txt | 10 ++++-
+ tests/backup-text-hex.c | 64 ++++++++++++++++++++++++++++++
+ 3 files changed, 81 insertions(+), 4 deletions(-)
+ create mode 100644 tests/backup-text-hex.c
+
+diff --git a/libgammu/service/backup/backtext.c b/libgammu/service/backup/backtext.c
+index c443a620f..6b9c902dd 100644
+--- a/libgammu/service/backup/backtext.c
++++ b/libgammu/service/backup/backtext.c
+@@ -164,6 +164,8 @@ static GSM_Error SaveBackupText(FILE *file, const char *myname, const char *myva
+ {
+ char buffer[10000]={0};
+ unsigned char buffer2[10000]={0};
++ char *hexbuffer;
++ size_t text_length;
+
+ if (myname[0] == 0x00) {
+ if (UseUnicode) {
+@@ -186,8 +188,13 @@ static GSM_Error SaveBackupText(FILE *file, const char *myname, const char *myva
+ } else {
+ EncodeSpecialChars(buffer, DecodeUnicodeString(myvalue));
+ fprintf(file, "%s = \"%s\"%c%c", myname, buffer, 13, 10);
+- EncodeHexBin(buffer, myvalue, UnicodeLength(myvalue) * 2);
+- fprintf(file, "%sUnicode = %s%c%c", myname, buffer, 13, 10);
++ text_length = UnicodeLength(myvalue);
++ if (text_length > ((size_t)-1 - 1) / 4) return ERR_MOREMEMORY;
++ hexbuffer = malloc(text_length * 4 + 1);
++ if (hexbuffer == NULL) return ERR_MOREMEMORY;
++ EncodeHexBin(hexbuffer, myvalue, text_length * 2);
++ fprintf(file, "%sUnicode = %s%c%c", myname, hexbuffer, 13, 10);
++ free(hexbuffer);
+ }
+ }
+ return ERR_NONE;
+diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
+index c3151b637..143170c07 100644
+--- a/tests/CMakeLists.txt
++++ b/tests/CMakeLists.txt
+@@ -102,15 +102,21 @@ if (WITH_BACKUP)
+ "${GAMMU_TEST_PATH}/read-backup${CMAKE_EXECUTABLE_SUFFIX}"
+ "${Gammu_SOURCE_DIR}/tests/backups/${TESTBACKUP}")
+ endforeach(TESTBACKUP $BACKUPS)
+-endif (WITH_BACKUP)
+
+-if (WITH_BACKUP)
+ add_executable(backup-checksum backup-checksum.c)
+ add_coverage(backup-checksum)
+ target_link_libraries(backup-checksum libGammu ${LIBINTL_LIBRARIES})
+ add_test(backup-checksum
+ "${GAMMU_TEST_PATH}/backup-checksum${CMAKE_EXECUTABLE_SUFFIX}"
+ "${CMAKE_CURRENT_BINARY_DIR}/backup-checksum.backup")
++
++ add_executable(backup-text-hex backup-text-hex.c)
++ add_coverage(backup-text-hex)
++ target_link_libraries(backup-text-hex libGammu ${LIBINTL_LIBRARIES})
++ add_test(backup-text-hex
++ "${GAMMU_TEST_PATH}/backup-text-hex${CMAKE_EXECUTABLE_SUFFIX}"
++ "${CMAKE_CURRENT_BINARY_DIR}/backup-text-hex-input.backup"
++ "${CMAKE_CURRENT_BINARY_DIR}/backup-text-hex-output.backup")
+ endif (WITH_BACKUP)
+
+ # Basic check for statemachine functionality
+diff --git a/tests/backup-text-hex.c b/tests/backup-text-hex.c
+new file mode 100644
+index 000000000..55291377c
+--- /dev/null
++++ b/tests/backup-text-hex.c
+@@ -0,0 +1,64 @@
++#include <gammu.h>
++#include <stdio.h>
++#include <stdlib.h>
++#include <string.h>
++
++#include "common.h"
++
++static void write_unicode(FILE *file, const char *text)
++{
++ while (*text) {
++ test_result(fputc(*text++, file) != EOF);
++ test_result(fputc(0, file) != EOF);
++ }
++}
++
++static void check_text(const char *input, const char *output, size_t length)
++{
++ static const char header[] =
++ "[Backup]\r\nFormat = 1.04\r\n"
++ "[Calendar001]\r\nLocation = 1\r\nType = Memo\r\nText = \"";
++ GSM_Backup *backup = malloc(sizeof(*backup));
++ FILE *file = fopen(input, "wb");
++ char line[20000];
++ size_t i;
++ int found = 0;
++
++ test_result(backup != NULL);
++ test_result(file != NULL);
++ test_result(fputc(0xff, file) != EOF);
++ test_result(fputc(0xfe, file) != EOF);
++ write_unicode(file, header);
++ for (i = 0; i < length; i++) write_unicode(file, "A");
++ write_unicode(file, "\"\r\n");
++ test_result(fclose(file) == 0);
++
++ test_result(GSM_ReadBackupFile(input, backup, GSM_Backup_GammuUCS2) == ERR_NONE);
++ test_result(GSM_SaveBackupFile((char *)output, backup, GSM_Backup_Gammu) == ERR_NONE);
++ GSM_FreeBackup(backup);
++ free(backup);
++
++ file = fopen(output, "rb");
++ test_result(file != NULL);
++ while (fgets(line, sizeof(line), file) != NULL) {
++ if (strncmp(line, "TextUnicode = ", 14) == 0) {
++ found = 1;
++ test_result(strlen(line + 14) == length * 4 + 2);
++ }
++ }
++ test_result(found);
++ test_result(fclose(file) == 0);
++ test_result(remove(input) == 0);
++ test_result(remove(output) == 0);
++}
++
++int main(int argc, char **argv)
++{
++ test_result(argc == 3);
++ check_text(argv[1], argv[2], 2400);
++ check_text(argv[1], argv[2], 2499);
++ check_text(argv[1], argv[2], 2500);
++ check_text(argv[1], argv[2], 2600);
++ check_text(argv[1], argv[2], 4090);
++ return 0;
++}
@@ -12,6 +12,7 @@ SRC_URI = "https://dl.cihar.com/${BPN}/releases/${BP}.tar.xz \
file://CVE-2026-138160_2.patch \
file://CVE-2026-138161.patch \
file://CVE-2026-138163.patch \
+ file://CVE-2026-138164.patch \
"
SRC_URI[sha256sum] = "d8f152314d7e4d3d643610d742845e0a016ce97c234ad4b1151574e1b09651ee"