aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--include/libbb.h177
-rw-r--r--include/platform.h2
-rw-r--r--include/usage.src.h6
3 files changed, 114 insertions, 71 deletions
diff --git a/include/libbb.h b/include/libbb.h
index 4d6193795..fba898943 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1011,13 +1011,13 @@ unsigned bb_clk_tck(void) FAST_FUNC;
1011 1011
1012#if SEAMLESS_COMPRESSION 1012#if SEAMLESS_COMPRESSION
1013/* Autodetects gzip/bzip2 formats. fd may be in the middle of the file! */ 1013/* Autodetects gzip/bzip2 formats. fd may be in the middle of the file! */
1014int setup_unzip_on_fd(int fd, int fail_if_not_compressed) FAST_FUNC; 1014int setup_unzip_on_fd(int fd, int die_if_not_compressed) FAST_FUNC;
1015/* Autodetects .gz etc */ 1015/* Autodetects .gz etc */
1016extern int open_zipped(const char *fname, int fail_if_not_compressed) FAST_FUNC; 1016extern int open_zipped(const char *fname, int die_if_not_compressed) FAST_FUNC;
1017extern void *xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC; 1017extern void *xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC;
1018#else 1018#else
1019# define setup_unzip_on_fd(...) (0) 1019# define setup_unzip_on_fd(...) (0)
1020# define open_zipped(fname, fail_if_not_compressed) open((fname), O_RDONLY); 1020# define open_zipped(fname, die_if_not_compressed) open((fname), O_RDONLY);
1021# define xmalloc_open_zipped_read_close(fname, maxsz_p) xmalloc_open_read_close((fname), (maxsz_p)) 1021# define xmalloc_open_zipped_read_close(fname, maxsz_p) xmalloc_open_read_close((fname), (maxsz_p))
1022#endif 1022#endif
1023/* lzma has no signature, need a little helper. NB: exist only for ENABLE_FEATURE_SEAMLESS_LZMA=y */ 1023/* lzma has no signature, need a little helper. NB: exist only for ENABLE_FEATURE_SEAMLESS_LZMA=y */
@@ -1113,6 +1113,32 @@ char *bin2hex(char *dst, const char *src, int count) FAST_FUNC;
1113/* Reverse */ 1113/* Reverse */
1114char* hex2bin(char *dst, const char *src, int count) FAST_FUNC; 1114char* hex2bin(char *dst, const char *src, int count) FAST_FUNC;
1115 1115
1116/* Returns strlen as a bonus */
1117//size_t replace_char(char *s, char what, char with) FAST_FUNC;
1118static inline size_t replace_char(char *str, char from, char to)
1119{
1120 char *p = str;
1121 while (*p) {
1122 if (*p == from)
1123 *p = to;
1124 p++;
1125 }
1126 return p - str;
1127}
1128
1129extern const char c_escape_conv_str00[];
1130#define c_escape_conv_str07 (c_escape_conv_str00+3)
1131
1132void FAST_FUNC xorbuf_3(void *dst, const void *src1, const void *src2, unsigned count);
1133void FAST_FUNC xorbuf(void* buf, const void* mask, unsigned count);
1134void FAST_FUNC xorbuf16_aligned_long(void* buf, const void* mask);
1135void FAST_FUNC xorbuf64_3_aligned64(void *dst, const void *src1, const void *src2);
1136#if BB_UNALIGNED_MEMACCESS_OK
1137# define xorbuf16(buf,mask) xorbuf16_aligned_long(buf,mask)
1138#else
1139void FAST_FUNC xorbuf16(void* buf, const void* mask);
1140#endif
1141
1116/* Generate a UUID */ 1142/* Generate a UUID */
1117void generate_uuid(uint8_t *buf) FAST_FUNC; 1143void generate_uuid(uint8_t *buf) FAST_FUNC;
1118 1144
@@ -1806,18 +1832,25 @@ extern char *pw_encrypt(const char *clear, const char *salt, int cleanup) FAST_F
1806extern int obscure(const char *old, const char *newval, const struct passwd *pwdp) FAST_FUNC; 1832extern int obscure(const char *old, const char *newval, const struct passwd *pwdp) FAST_FUNC;
1807/* 1833/*
1808 * rnd is additional random input. New one is returned. 1834 * rnd is additional random input. New one is returned.
1809 * Useful if you call crypt_make_salt many times in a row: 1835 * Useful if you call crypt_make_rand64encoded many times in a row:
1810 * rnd = crypt_make_salt(buf1, 4, 0); 1836 * rnd = crypt_make_rand64encoded(buf1, 4, 0);
1811 * rnd = crypt_make_salt(buf2, 4, rnd); 1837 * rnd = crypt_make_rand64encoded(buf2, 4, rnd);
1812 * rnd = crypt_make_salt(buf3, 4, rnd); 1838 * rnd = crypt_make_rand64encoded(buf3, 4, rnd);
1813 * (otherwise we risk having same salt generated) 1839 * (otherwise we risk having same salt generated)
1814 */ 1840 */
1815extern int crypt_make_salt(char *p, int cnt /*, int rnd*/) FAST_FUNC; 1841extern int crypt_make_rand64encoded(char *p, int cnt /*, int rnd*/) FAST_FUNC;
1816/* "$N$" + sha_salt_16_bytes + NUL */ 1842/* Size of char salt[] to hold randomly-generated salt string
1817#define MAX_PW_SALT_LEN (3 + 16 + 1) 1843 * sha256/512:
1844 * "$5$" ["rounds=999999999$"] "<sha_salt_16_chars><NUL>"
1845 * "$6$" ["rounds=999999999$"] "<sha_salt_16_chars><NUL>"
1846 * #define MAX_PW_SALT_LEN (3 + sizeof("rounds=999999999$")-1 + 16 + 1)
1847 * yescrypt:
1848 * "$y$" <up to 8 params of up to 6 chars each> "$" <up to 86 chars salt><NUL>
1849 * (86 chars are ascii64-encoded 64 binary bytes)
1850 */
1851#define MAX_PW_SALT_LEN (3 + 8*6 + 1 + 86 + 1)
1818extern char* crypt_make_pw_salt(char p[MAX_PW_SALT_LEN], const char *algo) FAST_FUNC; 1852extern char* crypt_make_pw_salt(char p[MAX_PW_SALT_LEN], const char *algo) FAST_FUNC;
1819 1853
1820
1821/* Returns number of lines changed, or -1 on error */ 1854/* Returns number of lines changed, or -1 on error */
1822#if !(ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP) 1855#if !(ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP)
1823#define update_passwd(filename, username, data, member) \ 1856#define update_passwd(filename, username, data, member) \
@@ -1951,6 +1984,7 @@ int64_t read_key(int fd, char *buffer, int timeout) FAST_FUNC;
1951int64_t safe_read_key(int fd, char *buffer, int timeout) FAST_FUNC; 1984int64_t safe_read_key(int fd, char *buffer, int timeout) FAST_FUNC;
1952void read_key_ungets(char *buffer, const char *str, unsigned len) FAST_FUNC; 1985void read_key_ungets(char *buffer, const char *str, unsigned len) FAST_FUNC;
1953 1986
1987int check_got_signal_and_poll(struct pollfd pfd[1], int timeout) FAST_FUNC;
1954 1988
1955#if ENABLE_FEATURE_EDITING 1989#if ENABLE_FEATURE_EDITING
1956/* It's NOT just ENABLEd or disabled. It's a number: */ 1990/* It's NOT just ENABLEd or disabled. It's a number: */
@@ -1988,7 +2022,7 @@ typedef struct line_input_t {
1988# if MAX_HISTORY 2022# if MAX_HISTORY
1989 int cnt_history; 2023 int cnt_history;
1990 int cur_history; 2024 int cur_history;
1991 int max_history; /* must never be <= 0 */ 2025 int max_history; /* must never be < 0 */
1992# if ENABLE_FEATURE_EDITING_SAVEHISTORY 2026# if ENABLE_FEATURE_EDITING_SAVEHISTORY
1993 /* meaning of this field depends on FEATURE_EDITING_SAVE_ON_EXIT: 2027 /* meaning of this field depends on FEATURE_EDITING_SAVE_ON_EXIT:
1994 * if !FEATURE_EDITING_SAVE_ON_EXIT: "how many lines are 2028 * if !FEATURE_EDITING_SAVE_ON_EXIT: "how many lines are
@@ -2047,33 +2081,6 @@ enum { COMM_LEN = 16 };
2047# endif 2081# endif
2048#endif 2082#endif
2049 2083
2050struct smaprec {
2051 unsigned long mapped_rw;
2052 unsigned long mapped_ro;
2053 unsigned long shared_clean;
2054 unsigned long shared_dirty;
2055 unsigned long private_clean;
2056 unsigned long private_dirty;
2057 unsigned long stack;
2058 unsigned long smap_pss, smap_swap;
2059 unsigned long smap_size;
2060 // For mixed 32/64 userspace, 32-bit pmap still needs
2061 // 64-bit field here to correctly show 64-bit processes:
2062 unsigned long long smap_start;
2063 // (strictly speaking, other fields need to be wider too,
2064 // but they are in kbytes, not bytes, and they hold sizes,
2065 // not start addresses, sizes tend to be less than 4 terabytes)
2066 char smap_mode[5];
2067 char *smap_name;
2068};
2069
2070#if !ENABLE_PMAP
2071#define procps_read_smaps(pid, total, cb, data) \
2072 procps_read_smaps(pid, total)
2073#endif
2074int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total,
2075 void (*cb)(struct smaprec *, void *), void *data);
2076
2077typedef struct procps_status_t { 2084typedef struct procps_status_t {
2078 DIR *dir; 2085 DIR *dir;
2079 IF_FEATURE_SHOW_THREADS(DIR *task_dir;) 2086 IF_FEATURE_SHOW_THREADS(DIR *task_dir;)
@@ -2103,7 +2110,13 @@ typedef struct procps_status_t {
2103#endif 2110#endif
2104 unsigned tty_major,tty_minor; 2111 unsigned tty_major,tty_minor;
2105#if ENABLE_FEATURE_TOPMEM 2112#if ENABLE_FEATURE_TOPMEM
2106 struct smaprec smaps; 2113 unsigned long mapped_rw;
2114 unsigned long mapped_ro;
2115 unsigned long shared_clean;
2116 unsigned long shared_dirty;
2117 unsigned long private_clean;
2118 unsigned long private_dirty;
2119 unsigned long stack;
2107#endif 2120#endif
2108 char state[4]; 2121 char state[4];
2109 /* basename of executable in exec(2), read from /proc/N/stat 2122 /* basename of executable in exec(2), read from /proc/N/stat
@@ -2152,11 +2165,15 @@ void free_procps_scan(procps_status_t* sp) FAST_FUNC;
2152procps_status_t* procps_scan(procps_status_t* sp, int flags) FAST_FUNC; 2165procps_status_t* procps_scan(procps_status_t* sp, int flags) FAST_FUNC;
2153/* Format cmdline (up to col chars) into char buf[size] */ 2166/* Format cmdline (up to col chars) into char buf[size] */
2154/* Puts [comm] if cmdline is empty (-> process is a kernel thread) */ 2167/* Puts [comm] if cmdline is empty (-> process is a kernel thread) */
2155void read_cmdline(char *buf, int size, unsigned pid, const char *comm) FAST_FUNC; 2168int read_cmdline(char *buf, int size, unsigned pid, const char *comm) FAST_FUNC;
2156pid_t *find_pid_by_name(const char* procName) FAST_FUNC; 2169pid_t *find_pid_by_name(const char* procName) FAST_FUNC;
2157pid_t *pidlist_reverse(pid_t *pidList) FAST_FUNC; 2170pid_t *pidlist_reverse(pid_t *pidList) FAST_FUNC;
2158int starts_with_cpu(const char *str) FAST_FUNC; 2171int starts_with_cpu(const char *str) FAST_FUNC;
2159unsigned get_cpu_count(void) FAST_FUNC; 2172unsigned get_cpu_count(void) FAST_FUNC;
2173/* Some internals reused by pmap: */
2174unsigned long FAST_FUNC fast_strtoul_10(char **endptr);
2175unsigned long long FAST_FUNC fast_strtoull_16(char **endptr);
2176char* FAST_FUNC skip_fields(char *str, int count);
2160 2177
2161 2178
2162/* Use strict=1 if you process input from untrusted source: 2179/* Use strict=1 if you process input from untrusted source:
@@ -2182,6 +2199,21 @@ char *decode_base64(char *dst, const char **pp_src) FAST_FUNC;
2182char *decode_base32(char *dst, const char **pp_src) FAST_FUNC; 2199char *decode_base32(char *dst, const char **pp_src) FAST_FUNC;
2183void read_base64(FILE *src_stream, FILE *dst_stream, int flags) FAST_FUNC; 2200void read_base64(FILE *src_stream, FILE *dst_stream, int flags) FAST_FUNC;
2184 2201
2202int FAST_FUNC i2a64(int i);
2203int FAST_FUNC a2i64(char c);
2204char* FAST_FUNC num2str64_lsb_first(char *s, unsigned v, int n);
2205
2206enum {
2207 /* how many bytes XYZ_end() fills */
2208 MD5_OUTSIZE = 16,
2209 SHA1_OUTSIZE = 20,
2210 SHA256_OUTSIZE = 32,
2211 SHA384_OUTSIZE = 48,
2212 SHA512_OUTSIZE = 64,
2213 //SHA3-224_OUTSIZE = 28,
2214 /* size of input block */
2215 SHA2_INSIZE = 64,
2216};
2185typedef struct md5_ctx_t { 2217typedef struct md5_ctx_t {
2186 uint8_t wbuffer[64]; /* always correctly aligned for uint64_t */ 2218 uint8_t wbuffer[64]; /* always correctly aligned for uint64_t */
2187 void (*process_block)(struct md5_ctx_t*) FAST_FUNC; 2219 void (*process_block)(struct md5_ctx_t*) FAST_FUNC;
@@ -2195,6 +2227,7 @@ typedef struct sha512_ctx_t {
2195 uint64_t hash[8]; 2227 uint64_t hash[8];
2196 uint8_t wbuffer[128]; /* always correctly aligned for uint64_t */ 2228 uint8_t wbuffer[128]; /* always correctly aligned for uint64_t */
2197} sha512_ctx_t; 2229} sha512_ctx_t;
2230typedef struct sha512_ctx_t sha384_ctx_t;
2198typedef struct sha3_ctx_t { 2231typedef struct sha3_ctx_t {
2199 uint64_t state[25]; 2232 uint64_t state[25];
2200 unsigned bytes_queued; 2233 unsigned bytes_queued;
@@ -2212,20 +2245,46 @@ void sha256_begin(sha256_ctx_t *ctx) FAST_FUNC;
2212void sha512_begin(sha512_ctx_t *ctx) FAST_FUNC; 2245void sha512_begin(sha512_ctx_t *ctx) FAST_FUNC;
2213void sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC; 2246void sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC;
2214unsigned sha512_end(sha512_ctx_t *ctx, void *resbuf) FAST_FUNC; 2247unsigned sha512_end(sha512_ctx_t *ctx, void *resbuf) FAST_FUNC;
2248void sha384_begin(sha384_ctx_t *ctx) FAST_FUNC;
2249#define sha384_hash sha512_hash
2250unsigned sha384_end(sha384_ctx_t *ctx, void *resbuf) FAST_FUNC;
2215void sha3_begin(sha3_ctx_t *ctx) FAST_FUNC; 2251void sha3_begin(sha3_ctx_t *ctx) FAST_FUNC;
2216void sha3_hash(sha3_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC; 2252void sha3_hash(sha3_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC;
2217unsigned sha3_end(sha3_ctx_t *ctx, void *resbuf) FAST_FUNC; 2253unsigned sha3_end(sha3_ctx_t *ctx, void *resbuf) FAST_FUNC;
2254void FAST_FUNC sha256_block(const void *in, size_t len, uint8_t hash[32]);
2218/* TLS benefits from knowing that sha1 and sha256 share these. Give them "agnostic" names too */ 2255/* TLS benefits from knowing that sha1 and sha256 share these. Give them "agnostic" names too */
2219typedef struct md5_ctx_t md5sha_ctx_t; 2256typedef struct md5_ctx_t md5sha_ctx_t;
2220#define md5sha_hash md5_hash 2257#define md5sha_hash md5_hash
2221#define sha_end sha1_end 2258#define sha_end sha1_end
2222enum { 2259
2223 MD5_OUTSIZE = 16, 2260/* RFC 2104 HMAC (hash-based message authentication code) */
2224 SHA1_OUTSIZE = 20, 2261typedef struct hmac_ctx {
2225 SHA256_OUTSIZE = 32, 2262 md5sha_ctx_t hashed_key_xor_ipad;
2226 SHA512_OUTSIZE = 64, 2263 md5sha_ctx_t hashed_key_xor_opad;
2227 SHA3_OUTSIZE = 28, 2264} hmac_ctx_t;
2228}; 2265#define HMAC_ONLY_SHA256 (!ENABLE_FEATURE_TLS_SHA1)
2266typedef void md5sha_begin_func(md5sha_ctx_t *ctx) FAST_FUNC;
2267#if HMAC_ONLY_SHA256
2268#define hmac_begin(ctx,key,key_size,begin) \
2269 hmac_begin(ctx,key,key_size)
2270#endif
2271void FAST_FUNC hmac_begin(hmac_ctx_t *ctx, const uint8_t *key, unsigned key_size, md5sha_begin_func *begin);
2272static ALWAYS_INLINE void hmac_hash(hmac_ctx_t *ctx, const void *in, size_t len)
2273{
2274 md5sha_hash(&ctx->hashed_key_xor_ipad, in, len);
2275}
2276unsigned FAST_FUNC hmac_end(hmac_ctx_t *ctx, uint8_t *out);
2277#if HMAC_ONLY_SHA256
2278#define hmac_block(key,key_size,begin,in,sz,out) \
2279 hmac_block(key,key_size,in,sz,out)
2280#endif
2281unsigned FAST_FUNC hmac_block(const uint8_t *key, unsigned key_size,
2282 md5sha_begin_func *begin,
2283 const void *in, unsigned sz,
2284 uint8_t *out);
2285/* HMAC helpers for TLS: */
2286void FAST_FUNC hmac_hash_v(hmac_ctx_t *ctx, va_list va);
2287unsigned FAST_FUNC hmac_peek_hash(hmac_ctx_t *ctx, uint8_t *out, ...);
2229 2288
2230extern uint32_t *global_crc32_table; 2289extern uint32_t *global_crc32_table;
2231uint32_t *crc32_filltable(uint32_t *tbl256, int endian) FAST_FUNC; 2290uint32_t *crc32_filltable(uint32_t *tbl256, int endian) FAST_FUNC;
@@ -2337,26 +2396,10 @@ extern struct globals *BB_GLOBAL_CONST ptr_to_globals;
2337#define barrier() asm volatile ("":::"memory") 2396#define barrier() asm volatile ("":::"memory")
2338 2397
2339#if defined(__clang_major__) && __clang_major__ >= 9 2398#if defined(__clang_major__) && __clang_major__ >= 9
2340/* Clang/llvm drops assignment to "constant" storage. Silently. 2399/* {ASSIGN,XZALLOC}_CONST_PTR() are out-of-line functions
2341 * Needs serious convincing to not eliminate the store. 2400 * to prevent clang from reading pointer before it is assigned.
2342 */
2343static ALWAYS_INLINE void* not_const_pp(const void *p)
2344{
2345 void *pp;
2346 asm volatile (
2347 "# forget that p points to const"
2348 : /*outputs*/ "=r" (pp)
2349 : /*inputs*/ "0" (p)
2350 );
2351 return pp;
2352}
2353# define ASSIGN_CONST_PTR(pptr, v) do { \
2354 *(void**)not_const_pp(pptr) = (void*)(v); \
2355 barrier(); \
2356} while (0)
2357/* XZALLOC_CONST_PTR() is an out-of-line function to prevent
2358 * clang from reading pointer before it is assigned.
2359 */ 2401 */
2402void ASSIGN_CONST_PTR(const void *pptr, void *v) FAST_FUNC;
2360void XZALLOC_CONST_PTR(const void *pptr, size_t size) FAST_FUNC; 2403void XZALLOC_CONST_PTR(const void *pptr, size_t size) FAST_FUNC;
2361#else 2404#else
2362# define ASSIGN_CONST_PTR(pptr, v) do { \ 2405# define ASSIGN_CONST_PTR(pptr, v) do { \
diff --git a/include/platform.h b/include/platform.h
index ea0512f36..e10c5b558 100644
--- a/include/platform.h
+++ b/include/platform.h
@@ -187,7 +187,7 @@
187#elif defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN 187#elif defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN
188# define BB_BIG_ENDIAN 0 188# define BB_BIG_ENDIAN 0
189# define BB_LITTLE_ENDIAN 1 189# define BB_LITTLE_ENDIAN 1
190#elif defined(__386__) 190#elif defined(__i386__)
191# define BB_BIG_ENDIAN 0 191# define BB_BIG_ENDIAN 0
192# define BB_LITTLE_ENDIAN 1 192# define BB_LITTLE_ENDIAN 1
193#else 193#else
diff --git a/include/usage.src.h b/include/usage.src.h
index 5d2038834..0881337f8 100644
--- a/include/usage.src.h
+++ b/include/usage.src.h
@@ -17,11 +17,11 @@
17#define scripted_trivial_usage NOUSAGE_STR 17#define scripted_trivial_usage NOUSAGE_STR
18#define scripted_full_usage "" 18#define scripted_full_usage ""
19 19
20#if !ENABLE_USE_BB_CRYPT || ENABLE_USE_BB_CRYPT_SHA 20#if !ENABLE_USE_BB_CRYPT
21# define CRYPT_METHODS_HELP_STR "des,md5,sha256/512" \ 21# define CRYPT_METHODS_HELP_STR "des,md5,sha256/512,yescrypt" \
22 " (default "CONFIG_FEATURE_DEFAULT_PASSWD_ALGO")" 22 " (default "CONFIG_FEATURE_DEFAULT_PASSWD_ALGO")"
23#else 23#else
24# define CRYPT_METHODS_HELP_STR "des,md5" \ 24# define CRYPT_METHODS_HELP_STR "des,md5"IF_USE_BB_CRYPT_SHA(",sha256/512")IF_USE_BB_CRYPT_YES(",yescrypt") \
25 " (default "CONFIG_FEATURE_DEFAULT_PASSWD_ALGO")" 25 " (default "CONFIG_FEATURE_DEFAULT_PASSWD_ALGO")"
26#endif 26#endif
27 27