hackergame2022-writeups
                                
                                 hackergame2022-writeups copied to clipboard
                                
                                    hackergame2022-writeups copied to clipboard
                            
                            
                            
                        “安全的在线测评”第一问的非预期解:直接读取 `static.out` 的内容
出题人在打包 docker image 时把 data/ 目录用 COPY 加入了 image 中,并且设置目录拥有者为 judger,目录的权限是 766。
在 online_judge.py 中预期使用 os.makedir() 来设置 data 目录的权限为 700,但是 os.makedir() 不改变已存在的目录的权限,见 python 3.11 文档 os.makedirs。所以 runner 用户可以读取 data 中的内容。
而 static.out 是其他用户可读的。所以有下面的非预期解。
#include <stdio.h>
#include <stdlib.h>
// static_data_path
char *sdata_input_path = "./data/static.in";
char *sdata_output_path = "./data/static.out";
const int LEN = 2048;
int main() {
    char *lines[2];
    size_t ns[2];
    FILE *fp = fopen(sdata_output_path, "r");
    if (fp == NULL) {
        printf("failed to read static output");
        exit(1);
    }
    // read and show the 2 prime numbers.
    getline(&lines[0], &ns[0], fp);
    printf("%s", lines[0]);
    getline(&lines[1], &ns[0], fp);
    printf("%s", lines[1]);
    free(lines[0]);
    free(lines[1]);
    fclose(fp);
    return 0;
}
我说怎么这题和杯窗鹅影第一题做法一模一样(甚至代码改个文件名交上去就PASS了),完全不像HG的出题风格,原来是非预期解…… 然后成功把我带到按顺序输出的沟里了,代码糊完发现读不出dynamic一脸蒙圈0.0