首页 > 其他分享 >Glibc---__gen_tempname函数原理分析学习

Glibc---__gen_tempname函数原理分析学习

时间:2022-12-13 23:02:32浏览次数:81  
标签:__ tmpl ---__ Glibc len try int 62 gen

引言

__gen_tempname是Glibc库stdio.h头文件中生成临时文件名的函数,包含了相关临时文件名的生成逻辑,我们来一起分析一下临时文件名的生成过程。

__gen_tempname函数参数说明

char *__tmpl---代表传入的文件/文件夹路径 int __suffixlen---后缀长度 int __flags---文件创建后缀 int __kind---创建类型,标识是文件/文件夹/还是返回一个临时名字,但不使用

按照一般典型的临时文件输入,__tmpl是"tmp/tmpfXXXXXX"这样的字符串,后面6个"X"就是需要进行填充的序号,所以在忘下一层调用时,填入了一个后缀长度6。

//代码参考:glibc/include/stdio.h
139 extern int __gen_tempname (char *__tmpl, int __suffixlen, int __flags,
140 int __kind) attribute_hidden;
141 /* The __kind argument to __gen_tempname may be one of: */
142 # define __GT_FILE 0 /* create a file */
143 # define __GT_DIR 1 /* create a directory */
144 # define __GT_NOCREATE 2 /* just find a name not currently in use *///代码参考:glibc/sysdeps/posix/tempname.c
332 int
333 __gen_tempname (char *tmpl, int suffixlen, int flags, int kind)
334 {
335 return gen_tempname_len (tmpl, suffixlen, flags, kind, 6);
336 }

gen_tempname_len的逻辑

在函数开头的注释中也做了相关说明:对tmpl保存的字符串进行修改,针对不同的kind做如下处理:

  • __GT_NOCREATE:返回一个当前检测之后不存在的临时文件名
  • __GT_FILE:返回一个可读写的fd,文件的mode为0600
  • __GT_DIR:创建一个文件夹,mode为0700

具体实现方式是通过传入的三个函数指针实现的:try_file,try_dir,try_nocreate,然后调用try_tempname_len完成核心的tempname构造逻辑。

//代码参考:glibc/sysdeps/posix/tempname.c
203 /* Generate a temporary file name based on TMPL. TMPL must match the
204 rules for mk[s]temp (i.e., end in at least X_SUFFIX_LEN "X"s,
205 possibly with a suffix).
206 The name constructed does not exist at the time of the call to
207 this function. TMPL is overwritten with the result.
208
209 KIND may be one of:
210 __GT_NOCREATE: simply verify that the name does not exist
211 at the time of the call.
212 __GT_FILE: create the file using open(O_CREAT|O_EXCL)
213 and return a read-write fd. The file is mode 0600.
214 __GT_DIR: create a directory, which will be mode 0700.
215
216 We use a clever algorithm to get hard-to-predict names. */
217 #ifdef _LIBC
218 static
219 #endif
220 int
221 gen_tempname_len (char *tmpl, int suffixlen, int flags, int kind,
222 size_t x_suffix_len)
223 {
224 static int (*const tryfunc[]) (char *, void *) =
225 {
226 [__GT_FILE] = try_file,
227 [__GT_DIR] = try_dir,
228 [__GT_NOCREATE] = try_nocreate
229 };
230 return try_tempname_len (tmpl, suffixlen, &flags, tryfunc[kind],
231 x_suffix_len);
232 }

三个try函数的实现逻辑

try_file

传入已构造好的文件路径tmpl和对应flags信息,调用函数__open打开,__open函数的实现逻辑可以参考​​C++学习---__libc_open函数的原理​

174 static int
175 try_file (char *tmpl, void *flags)
176 {
177 int *openflags = flags;
178 return __open (tmpl,
179 (*openflags & ~O_ACCMODE)
180 | O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
181 }

try_dir

传入已构造好的文件路径tmpl和对应flags信息,调用函数__mkdir创建文件夹,注意,这里的falgs信息其实是完全没有使用的,后面的S_IRUSR,S_IWUSR,S_IXUSR实际上是定义文件夹的读写执行权限。

183 static int
184 try_dir (char *tmpl, _GL_UNUSED void *flags)
185 {
186 return __mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR);
187 }

//glibc/io/fcntl.h
105 # define S_IRUSR __S_IREAD /* Read by owner. */
106 # define S_IWUSR __S_IWRITE /* Write by owner. */
107 # define S_IXUSR __S_IEXEC /* Execute by owner. */
108 /* Read, write, and execute by owner. */
109 # define S_IRWXU (__S_IREAD|__S_IWRITE|__S_IEXEC)

try_nocreate

首先通过__lstat64_time64获取当前传入路径tmpl的文件状态,如果该文件已经创建,那就会查询成功,返回0,这时就要将错误状态置为EEXIST(文件存在);或者非0,即文件未被创建,查询信息失败,此时查看第二个条件而且错误码被置为溢出,那此时也置为EEXIST(文件存在)。

最后通过比较errno和ENOENT(No such file or directory)的值,如果是ENOENT表明该临时文件名没有被使用过,try_nocreate返回0表示成功。

189 static int
190 try_nocreate (char *tmpl, _GL_UNUSED void *flags)
191 {
192 struct_stat64 st;
193
194 if (__lstat64_time64 (tmpl, &st) == 0 || errno == EOVERFLOW)
195 __set_errno (EEXIST);
196 return errno == ENOENT ? 0 : -1;
197 }

try_tempname_len的实现逻辑

1.入参分析

  • char *tmpl---临时文件名:如tmp/tmpfXXXXXX
  • int suffixlen---后缀长度
  • void *args---flags信息
  • int (*tryfunc) (char *, void *)---try类型函数指针表
  • size_t x_suffix_len---后缀X的个数,一般是6
//glibc/sysdeps/posix/tempname.c
234 #ifdef _LIBC
235 static
236 #endif
237 int
238 try_tempname_len (char *tmpl, int suffixlen, void *args,
239 int (*tryfunc) (char *, void *), size_t x_suffix_len)
240 {
241 size_t len;
242 char *XXXXXX;
243 unsigned int count;
244 int fd = -1;
245 int save_errno = errno;

2.定义尝试的文件数量大小ATTEMPTS_MIN

因为我们总共有6个X位置需要填充,每个位置上可以是如下的任何一个字符:

"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"--共62个字符

所以所有的组合数为62662^6626,但是实际上我们没有必要尝试所有的组合,我们尝试62362^3623种组合,这种组合就要求X_SUFFIX_LEN即X的长度至少是3个,足够我们进行替换。

同时为了兼容POSIX的定义,我们需要与TMP_MAX做比较,选择两者中较大的那个

33 #ifndef TMP_MAX                                                            34 # define TMP_MAX 238328 //实际上也是62*62*62247   /* A lower bound on the number of temporary files to attempt to
248 generate. The maximum total number of temporary file names that
249 can exist for a given template is 62**6. It should never be
250 necessary to try all of these combinations. Instead if a reasonable
251 number of names is tried (we define reasonable as 62**3) fail to
252 give the system administrator the chance to remove the problems.
253 This value requires that X_SUFFIX_LEN be at least 3. */
254 #define ATTEMPTS_MIN (62 * 62 * 62)
255
256 /* The number of times to attempt to generate a temporary file. To
257 conform to POSIX, this must be no smaller than TMP_MAX. */
258 #if ATTEMPTS_MIN < TMP_MAX
259 unsigned int attempts = TMP_MAX;
260 #else
261 unsigned int attempts = ATTEMPTS_MIN;
262 #endif

3.随机数准备

random_value定义为uint_fast64_t,即64位uint,这里取了v的地址与max_align_t的除数作为初始化种子; vdigits表示当前可以有多少个字符可以从v中解析出来; use_getrandom的值取决于tryfunc函数指针是否直接是try_nocreate,如果是,代表只生成文件名; unfair_min,V的最小不公平值。如果V小于此值,V可以公平地生成BASE_62_DIGITS数字。否则,它可能会有偏差,取值就是RANDOM_VALUE_MAX去除RANDOM_VALUE_MAX % BASE_62_POWER即余数部分。

72 /* Use getrandom if it works, falling back on a 64-bit linear
73 congruential generator that starts with Var's value
74 mixed in with a clock's low-order bits if available. */75 typedef uint_fast64_t random_value;
76 #define RANDOM_VALUE_MAX UINT_FAST64_MAX77 #define BASE_62_DIGITS 10 /* 62**10 < UINT_FAST64_MAX */78 #define BASE_62_POWER (62LL * 62 * 62 * 62 * 62 * 62 * 62 * 62 * 62 * 62)264 /* A random variable. The initial value is used only the for fallback path
265 on 'random_bits' on 'getrandom' failure. Its initial value tries to use
266 some entropy from the ASLR and ignore possible bits from the stack
267 alignment. */
268 random_value v = ((uintptr_t) &v) / alignof (max_align_t);
269
270 /* How many random base-62 digits can currently be extracted from V. */
271 int vdigits = 0;
272
273 /* Whether to consume entropy when acquiring random bits. On the
274 first try it's worth the entropy cost with __GT_NOCREATE, which
275 is inherently insecure and can use the entropy to make it a bit
276 less secure. On the (rare) second and later attempts it might
277 help against DoS attacks. */
278 bool use_getrandom = tryfunc == try_nocreate;
279
280 /* Least unfair value for V. If V is less than this, V can generate
281 BASE_62_DIGITS digits fairly. Otherwise it might be biased. */
282 random_value const unfair_min
283 = RANDOM_VALUE_MAX - RANDOM_VALUE_MAX % BASE_62_POWER;

4.tmpl的数据准备

检查tmpl的合法性,主要是检查x_suffix_len的数量是否正确,否则判断为EINVAL(Invalid argument);

然后将XXXXXX赋值为X字符开始的位置。

285   len = strlen (tmpl);
286 if (len < x_suffix_len + suffixlen
287 || strspn (&tmpl[len - x_suffix_len - suffixlen], "X") < x_suffix_len)
288 {
289 __set_errno (EINVAL);
290 return -1;
291 }
292
293 /* This is where the Xs start. */
294 XXXXXX = &tmpl[len - x_suffix_len - suffixlen];

5.循环验证部分

  • 1.从0开始遍历attempts尝试数量,至多是626262次,中间成功就return退出函数
  • 2.对x_suffix_len中的每一个字符进行遍历填充
  • 3.随机数循环,vdigits表示可以从随机数中抽取的字符,如果为0,那就要通过random_bits获取随机数,而且要保证v大于其最小不公平值unfair_min,实际上这一步就是给出最随机的一个随机数,然后vdigits就被赋值为BASE_62_DIGITS = 10,表示最多可以从中生成10个字符
  • 4.填充字符,使用v(随机数)%62求余,对应的index找到对应的letters字符数组中的字符,然后v/62变小,vdigits--,减少一位,直到x_suffix_len个x填充完成;
  • 5.调用函数指针tryfunc执行对应的操作,并处理返回的结果。
296   for (count = 0; count < attempts; ++count)
297 {
298 for (size_t i = 0; i < x_suffix_len; i++)
299 {
300 if (vdigits == 0)
301 {
302 do
303 {
304 v = random_bits (v, use_getrandom);
305 use_getrandom = true;
306 }
307 while (unfair_min <= v);
308
309 vdigits = BASE_62_DIGITS;
310 }
311
312 XXXXXX[i] = letters[v % 62];
313 v /= 62;
314 vdigits--;
315 }
316
317 fd = tryfunc (tmpl, args);
318 if (fd >= 0)
319 {
320 __set_errno (save_errno);
321 return fd;
322 }
323 else if (errno != EEXIST)
324 return -1;
325 }

随机数部分

随机数算法的目的就是保证在每一次进入函数时都生成尽可能随机的数,这里就不深入分析了,可以看到实际上这里是有两种方式的,根据use_getrandom(即上面的是否是try_nocreate),决定是调用系统函数__getrandom生成随机数,还是使用传入的随机数种子(即V的地址,实际上每次函数运行,这个地址可能都会变化),然后再与当前的时间做运算,通过特定的规则返回随机数。

80 static random_value
81 random_bits (random_value var, bool use_getrandom)
82 {
83 random_value r;
84 /* Without GRND_NONBLOCK it can be blocked for minutes on some systems. */85 if (use_getrandom && __getrandom (&r, sizeof r, GRND_NONBLOCK) == sizeof r)
86 return r;
87 #if _LIBC || (defined CLOCK_MONOTONIC && HAVE_CLOCK_GETTIME)88 /* Add entropy if getrandom did not work. */89 struct __timespec64 tv;
90 __clock_gettime64 (CLOCK_MONOTONIC, &tv);
91 var ^= tv.tv_nsec;
92 #endif93 return 2862933555777941757 * var + 3037000493;
94 }

总结

__gen_tempname实际上是通过随机数生成算法,实现对临时文件名的填充,中间有很多细节的部分值得研究和分析,能够了解到其内部实现。

标签:__,tmpl,---__,Glibc,len,try,int,62,gen
From: https://blog.51cto.com/u_15830688/5935257

相关文章

  • k8s源码分析8-Audit审计功能
    audit审计的总结Kubernetes审计(Auditing)功能提供了与安全相关的、按时间顺序排列的记录集,记录每个用户、使用KubernetesAPI的应用以及控制面自身引发的活动审计功......
  • 《原则》-记录
    弄清楚其他时间、其他地点、其他人身上发生的事,因为如果不这么做,就不知道这些事情会不会发生在自己身上,而且一旦发生在自己身上,将不知道如何应对。当所有人想法都一样时,这......
  • 【221213-4】解方程:5x平方+x=x*(5x平方-1)开方+2
    ......
  • React报错之Too many re-renders
    总览产生"Toomanyre-renders.Reactlimitsthenumberofrenderstopreventaninfiniteloop"错误有多方面的原因:在一个组件的渲染方法中调用一个设置状态的函数......
  • grails的controller和action那点事---远程调试groovy代码
     最近由于项目需要,用到了grails,这玩意确实好用,生产率高有类型python的速度与简洁。仅第一印象,用的还不深入,说的不对请轻拍。遇到的几个问题:1.groovy远程调试玩Java的应该......
  • 知识回顾-JDK有哪些垃圾收集器及收集器组合
    目录经典垃圾收集器新生代Serial收集器ParNew收集器ParallelScavenge收集器老年代SerialOld收集器ParallelOld收集器CMS收集器G1收集器ZGC收集器如何获取使用的默认的垃......
  • day01-家具网购项目说明
    家具网购项目说明1.项目前置技术Java基础正则表达式MysqlJDBC数据库连接池技术满汉楼项目(包括框架图)JavaWeb2.相关说明这里先使用原生的servlet/过滤器,后台......
  • Go-15 Golang语言中time包以及日期函数
    packagemainimport( "fmt" "time")//Golangtime包以及日期函数funcmain(){ /* 1.time.Now()获取当前时间 2.Format方法会格式化输出日期字符串 3......
  • #yyds干货盘点#【愚公系列】2022年12月 微信小程序-自定义导航栏功能的实现
    前言导航栏是指位于页面顶部或者侧边区域的,在页眉横幅图片上边或下边的一排水平导航按钮,它起着链接站点或者软件内的各个页面的作用。小程序原生导航栏的限制除了胶囊按......
  • 操作系统-文件管理
    4.1初识文件文件的属性文件名:相同目录不可重名标识符:各文件标识符唯一,对用户无可读性类型位置:存放路径,在外存中地址(对用户地址不可见)保护信息:对文件进行......