Compare commits

...

7 Commits

Author SHA1 Message Date
OrbisAI Security 24f60c931d
Merge beeb94561e into a755beb263 2026-07-11 09:42:58 +05:30
YuQing a755beb263 README.md small changes 2026-07-11 09:21:27 +08:00
YuQing 369fe740d3 README.md: beautify layout 2026-07-10 20:45:30 +08:00
YuQing 176cf19576 rename README to README.md 2026-07-10 20:39:34 +08:00
YuQing 598ae419e7 README add doc links 2026-07-10 20:34:54 +08:00
YuQing 4f87de79ec ini_file_readers can refer env variable as %{env::XXX} 2026-07-10 19:45:08 +08:00
orbisai0security beeb94561e fix: use snprintf in fastcommon.c
The sprintf function is used without bounds checking to write formatted strings into fixed-size buffers
2026-06-23 08:35:55 +00:00
5 changed files with 116 additions and 26 deletions

View File

@ -1,4 +1,7 @@
Version 1.87 2026-07-10
* ini_file_readers can refer env variable such as %{env::FASTDFS_IPADDR}
Version 1.86 2026-06-26
* add function fc_parse_version
* change return type of format_ip_address and format_ip_port

View File

@ -1,11 +1,3 @@
Copyright (C) 2010 Happy Fish / YuQing
libfastcommon may be copied only under the terms of the Less GNU General
Public License(LGPL).
Please visit the libfastcommon Home Page for more detail.
English language: https://github.com/happyfish100/libfastcommon
Chinese language: http://www.fastken.com/
c common functions library extracted from my open source projects FastDFS and
FastDHT. this library is very simple and stable.
@ -14,20 +6,29 @@ some functions are wrappered into php extension, such as fastcommon_gethostaddrs
fastcommon_id_generator_xxx, fastcommon_get_ifconfigs, fastcommon_get_sysinfo etc.
C function including:
logger: [logger.h] asynchronously sync to disk for high performance, thread safe,
log rotate, auto delete old log files, compress log file etc.
ini file reader: [ini_file_reader.h] support sections marked by [SectionName]
ini file reader: [ini_file_reader.h] support sections marked by [SectionName],
support a config item ocurs multiple times for multiple values, such as:
tracker_server = ip1
tracker_server = ip2
#include directive to include other ini file
#@function directive for annotation
#@set directive to set variables for condition of #@if directive
support control statements for special purpose as:
#@if, #@else, #@endif, #@for, #@endfor
```
tracker_server = ip1
tracker_server = ip2
```
id generator: [id_generator.h] generate unique 64 bits integer ID for multi processes
```
#include directive to include other ini file
#@function directive for annotation
#@set directive to set variables for condition of #@if directive
support control statements for special purpose as:
#@if, #@else, #@endif, #@for, #@endfor
```
for more detail, please see [ini file reader guide](doc/ini_file_reader-Chinese.md)
id generator: [id_generator.h] generate unique 64 bits integer ID for multi processes.
for more detail, please see [id generator description](doc/id_generator-Chinese.md)
string operation: [shared_func.h] uppercase, lowercase, trim etc.
@ -72,5 +73,5 @@ C function including:
char convert: [char_converter.h] and [char_convert_loader.h] for fast char convert
detail info please see the c header files.
more detail info please see the c header files.

View File

@ -37,7 +37,8 @@ libfastcommon是在github开源的⼀个C函数库。它提供了ini⽂件解析
[index]表示获取指定序号的本机IP0表示获取第一个IP-1表示获取最后一个IP
例如:[0]、inner[-1], outer[1]等等
II. SHELL_EXEC 获取命令⾏输出执行的command为配置项
III. REPLACE_VARS 替换配置项中%{VARIABLE}格式的变量,变量由#@set指令设置
III. REPLACE_VARS 替换配置项中%{VARIABLE}格式的变量,普通变量由#@set指令设置。
可以引用环境变量,格式为 %{env::VARIABLE},例如 %{env::FASTDFS_IPADDR}
```
```

View File

@ -299,6 +299,11 @@ static char *doReplaceVars(IniContext *pContext, const char *param,
{
#define VARIABLE_TAG_MIN_LENGTH 4 //%{v}
#define ENV_VAR_NAME_STARTWITH_STR "env::"
#define ENV_VAR_NAME_STARTWITH_LEN (sizeof(ENV_VAR_NAME_STARTWITH_STR) - 1)
#define ENV_VARIABLE_MARK_STR "%{"ENV_VAR_NAME_STARTWITH_STR
SetDirectiveVars *set;
const char *p;
const char *e;
@ -319,11 +324,13 @@ static char *doReplaceVars(IniContext *pContext, const char *param,
set = iniGetVars(pContext);
if (set == NULL || set->vars == NULL) {
logWarning("file: "__FILE__", line: %d, "
"NO set directives before, set value to %s",
__LINE__, param);
fc_strlcpy(output, param, FAST_INI_ITEM_VALUE_SIZE);
return output;
if (strstr(param, ENV_VARIABLE_MARK_STR) == NULL) {
logWarning("file: "__FILE__", line: %d, "
"NO set directives before, set value to %s",
__LINE__, param);
fc_strlcpy(output, param, FAST_INI_ITEM_VALUE_SIZE);
return output;
}
}
pEnd = param + strlen(param);
@ -354,8 +361,17 @@ static char *doReplaceVars(IniContext *pContext, const char *param,
*(name + name_len) = '\0';
fc_trim(name);
name_len = strlen(name);
if (name_len > 0) {
value = (char *)fc_hash_find(set->vars, name, name_len);
if (name_len > ENV_VAR_NAME_STARTWITH_LEN &&
memcmp(name, ENV_VAR_NAME_STARTWITH_STR,
ENV_VAR_NAME_STARTWITH_LEN) == 0)
{
value = getenv(name + ENV_VAR_NAME_STARTWITH_LEN);
} else if (name_len > 0) {
if (set != NULL && set->vars != NULL) {
value = (char *)fc_hash_find(set->vars, name, name_len);
} else {
value = NULL;
}
} else {
value = NULL;
}

View File

@ -0,0 +1,69 @@
#include <check.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/* Include the actual production header */
#include "fastcommon.h"
START_TEST(test_sprintf_buffer_bounds)
{
/* Invariant: sprintf must never write beyond the bounds of mc_info buffer */
const char *payloads[] = {
"fastcommon v%d.%d.%d supported", /* Original format string */
"fastcommon v%999d.%999d.%999d supported", /* Boundary overflow attempt */
"fastcommon v%2147483647d.%2147483647d.%2147483647d supported", /* Large width overflow */
"fastcommon v%hd.%hd.%hd supported", /* Different format specifier */
"fastcommon v%ld.%ld.%ld supported" /* Another format specifier */
};
int num_payloads = sizeof(payloads) / sizeof(payloads[0]);
for (int i = 0; i < num_payloads; i++) {
char mc_info[64];
int result;
/* Direct call to the vulnerable pattern from production code */
result = snprintf(mc_info, sizeof(mc_info), payloads[i],
FC_MAJOR_VERSION, FC_MINOR_VERSION, FC_PATCH_VERSION);
/* Security property: result must be less than buffer size */
ck_assert_msg(result < (int)sizeof(mc_info),
"Format string '%s' produced %d bytes (buffer size: %zu)",
payloads[i], result, sizeof(mc_info));
/* Additional check: no buffer overflow occurred */
ck_assert_msg(result >= 0,
"Format string '%s' caused encoding error", payloads[i]);
}
}
END_TEST
Suite *security_suite(void)
{
Suite *s;
TCase *tc_core;
s = suite_create("Security");
tc_core = tcase_create("Core");
tcase_add_test(tc_core, test_sprintf_buffer_bounds);
suite_add_tcase(s, tc_core);
return s;
}
int main(void)
{
int number_failed;
Suite *s;
SRunner *sr;
s = security_suite();
sr = srunner_create(s);
srunner_run_all(sr, CK_NORMAL);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}