From 07b9e554fa1c3389d4b7f866c76ceb61ecc487ef Mon Sep 17 00:00:00 2001 From: Versa NexusIX Date: Fri, 20 Nov 2026 02:52:21 +0900 Subject: [PATCH] ssl: fix string comparison in SSLContext#{min,max}version= The strncmp() incorrectly accepts truncated strings or strings containing embedded NUL bytes. Check that the string lengths match before comparing the contents. Fixes https://github.com/ruby/openssl/issues/1158 Co-authored-by: Kazuki Yamaguchi --- ext/openssl/ossl_ssl.c | 3 ++- test/openssl/test_ssl.rb | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/ext/openssl/ossl_ssl.c b/ext/openssl/ossl_ssl.c index b29e45f84..d0268b7aa 100644 --- a/ext/openssl/ossl_ssl.c +++ b/ext/openssl/ossl_ssl.c @@ -911,7 +911,8 @@ parse_proto_version(VALUE str) str = rb_sym2str(str); StringValue(str); for (i = 0; i < numberof(map); i++) - if (!strncmp(map[i].name, RSTRING_PTR(str), RSTRING_LEN(str))) + if ((size_t)RSTRING_LEN(str) == strlen(map[i].name) && + memcmp(map[i].name, RSTRING_PTR(str), RSTRING_LEN(str)) == 0) return map[i].version; rb_raise(rb_eArgError, "unrecognized version %+"PRIsVALUE, str); } diff --git a/test/openssl/test_ssl.rb b/test/openssl/test_ssl.rb index 3a548e36f..8d80ec832 100644 --- a/test/openssl/test_ssl.rb +++ b/test/openssl/test_ssl.rb @@ -1542,6 +1542,9 @@ def test_minmax_version_symbols assert_raise(ArgumentError) { ctx.min_version = :bogus } + assert_raise(ArgumentError) { + ctx.min_version = "TLS" + } } end