Commit Graph

1243 Commits

Author SHA1 Message Date
FRIGN
833c2aebb4 Remove mallocarray(...) and use reallocarray(NULL, ...)
After a short correspondence with Otto Moerbeek it turned out
mallocarray() is only in the OpenBSD-Kernel, because the kernel-
malloc doesn't have realloc.
Userspace applications should rather use reallocarray with an
explicit NULL-pointer.

Assuming reallocarray() will become available in c-stdlibs in the
next few years, we nip mallocarray() in the bud to allow an easy
transition to a system-provided version when the day comes.
2015-03-11 10:50:18 +01:00
FRIGN
d6818a3c5f Audit cksum(1)
1) Reorder local variables.
2) Cleanup error messages, use %zu for size_t.
3) combine putchar(' ') and fputs to substitute printf(" %s", s).
4) Fix usage().
5) argv-argc-usage-fix.
6) Add empty line before return.
2015-03-11 00:13:48 +01:00
FRIGN
3c33abc520 Implement mallocarray()
A function used only in the OpenBSD-Kernel as of now, but it surely
provides a helpful interface when you just don't want to make sure
the incoming pointer to erealloc() is really NULL so it behaves
like malloc, making it a bit more safer.

Talking about *allocarray(): It's definitely a major step in code-
hardening. Especially as a system administrator, you should be
able to trust your core tools without having to worry about segfaults
like this, which can easily lead to privilege escalation.

How do the GNU coreutils handle this?
$ strings -n 4611686018427387903
strings: invalid minimum string length -1
$ strings -n 4611686018427387904
strings: invalid minimum string length 0

They silently overflow...

In comparison, sbase:

$ strings -n 4611686018427387903
mallocarray: out of memory
$ strings -n 4611686018427387904
mallocarray: out of memory

The first out of memory is actually a true OOM returned by malloc,
whereas the second one is a detected overflow, which is not marked
in a special way.
Now tell me which diagnostic error-messages are easier to understand.
2015-03-10 22:19:19 +01:00
FRIGN
3b825735d8 Implement reallocarray()
Stateless and I stumbled upon this issue while discussing the
semantics of read, accepting a size_t but only being able to return
ssize_t, effectively lacking the ability to report successful
reads > SSIZE_MAX.
The discussion went along and we came to the topic of input-based
memory allocations. Basically, it was possible for the argument
to a memory-allocation-function to overflow, leading to a segfault
later.
The OpenBSD-guys came up with the ingenious reallocarray-function,
and I implemented it as ereallocarray, which automatically returns
on error.
Read more about it here[0].

A simple testcase is this (courtesy to stateless):
$ sbase-strings -n (2^(32|64) / 4)

This will segfault before this patch and properly return an OOM-
situation afterwards (thanks to the overflow-check in reallocarray).

[0]: http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/calloc.3
2015-03-10 21:23:36 +01:00
Hiltjo Posthuma
066a0306a1 fork: no need to _exit() on the error case 2015-03-10 20:05:18 +01:00
Quentin Rameau
82bebf8ce7 nl: add -l option 2015-03-10 12:55:06 +00:00
FRIGN
a8bd21c0ab Use switch with fork()
Allows dropping a local variable if the explicit PID is not needed
and it makes it clearer what happens.
Also, one should always strive for consistency for cases like these.
2015-03-09 15:01:29 +01:00
FRIGN
6f207dac5f Don't return but _exit after failed exec*() and fork()
Quoting POSIX[0]:
"Care should be taken, also, to call _exit() rather than exit() if exec cannot be used, since
exit() flushes and closes standard I/O channels, thereby damaging the parent process' standard
I/O data structures. (Even with fork(), it is wrong to call exit(), since buffered data would
then be flushed twice.)"

[0]: http://pubs.opengroup.org/onlinepubs/009695399/functions/vfork.html
2015-03-09 01:12:59 +01:00
FRIGN
4414a17e1b Small global-var change to chgrp(1) 2015-03-09 00:42:23 +01:00
FRIGN
3a04302c66 Audit chown(1)
Similar to the chgrp(1)-audit:
1) Refactor manpage so it's actually fun to read
2) BUGFIX: Call (l)chown properly when the H-flag is specified
   (only when depth > 0)
3) BUGFIX: Call (l)chown properly when the h-flag is specified
   (only when depth = 0).
4) BUGFIX: Only recurse() in chgrp() when the initial chownf()
   succeeds.
5) Style fixes, argv-basing.
6) Rename status to ret for consistency.
7) Add blank line before return.
2015-03-09 00:42:23 +01:00
Hiltjo Posthuma
8f436abde6 style: document intentional fallthrough 2015-03-09 00:33:13 +01:00
FRIGN
aa1a2391ba Use idiomatic return in setsid(1) 2015-03-09 00:25:54 +01:00
FRIGN
fba669da88 Audit chgrp(1)
1) Refactor manpage so it's actually fun to read.
2) BUGFIX: Call (l)chown properly when the H-flag is specified
   (only when depth > 0).
3) BUGFIX: Call (l)chown properly when the h-flag is specified
   (only when depth = 0).
4) BUGFIX: Only recurse() in chgrp() when the initial chownf()
   succeeds.
5) Style fixes, argv-basing.
6) Rename status to ret for consistency.
7) Add blank line before return.
2015-03-08 23:31:59 +01:00
FRIGN
8ffa06b3dc Add little estrtonum()-range-fix 2015-03-08 21:35:16 +01:00
FRIGN
76ad86b2a3 Audit cal(1)
1) Update manpage with the num-syntax.
2) Use size_t for years and derivatives.
3) Use putchar instead of printf wherever possible.
4) Update usage().
5) Style changes.
2015-03-08 21:28:32 +01:00
FRIGN
f140403fca Audit cols(1)
1) Refactor manpage.
2) De-globalize local values.
3) update usage().
4) sort local variable declarations.
5) fix wrong argument in strtonum (3 -> 1).
6) argc-argv style, boolean style.
7) check bytes > 0 before accessing b.lines[i][bytes - 1]
   relying on len only makes sense but let's not push it.
7) don't break on maxlen > (chars - 1) / 2. This didn't even
   make sense.
8) _correctly_ calculate cols and rows in a readable way.
9) Rewrite loop over rows and cols in a readable way and
   using putchar in a loop instead of printf-magic or fputs
   where not necessary.
2015-03-08 19:49:59 +01:00
Hiltjo Posthuma
78187474cf uniq: optimization: reuse the prevline buffer
don't strdup for each prevline, try to reuse the allocated buffer and if needed
increase it's size.
2015-03-08 14:50:46 +01:00
Hiltjo Posthuma
603499b674 time: show which signal terminated the program, exit status
The exit status when a program is signaled is not specified in POSIX afaik.
The GNU behaviour of 128 + signalno is used.
2015-03-08 12:51:42 +01:00
Hiltjo Posthuma
ad6776e9a1 grep, kill, renice, sort: style: put main at bottom 2015-03-08 12:51:33 +01:00
Hiltjo Posthuma
b2e73936bf uniq: dont use count format if empty 2015-03-07 14:04:04 +01:00
Hiltjo Posthuma
695940d50c uniq: style put main at bottom 2015-03-07 14:02:04 +01:00
Hiltjo Posthuma
160709734c col.1: convert to mandoc
- Also fix a few typos, style and section order.
- Changed the text "800 characters per line" to "800 bytes per line" as col
  doesn't seem to support UTF-8 right now.
2015-03-07 13:56:13 +01:00
Hiltjo Posthuma
37eaf92a7d seq, xargs: style: put main at bottom 2015-03-07 13:36:40 +01:00
Hiltjo Posthuma
0c17cd0fb3 staticize some functions 2015-03-07 13:33:39 +01:00
Hiltjo Posthuma
4092196fdc uniq: fix argv check
uniq inputfile failed with "extra argument"
2015-03-07 13:29:12 +01:00
sin
7d36a35649 Fix off-by-one in apathmax() as the path is relative to "/"
1) Use size_t * instead of long *
2) Fallback to PATH_MAX instead of BUFSIZ
3) Header cleanup
2015-03-06 23:50:39 +00:00
FRIGN
0c2f19c210 Audit logger(1)
1) Update manpage to current style
2) Line spacing
3) Local variable grouping
4) check for getline >= 0 instead of != -1
5) error message cleanup
2015-03-07 00:10:22 +01:00
Evan Gates
d21a958d88 bug and style fixes in find
1) don't mix declarations and code (leave recursion alone for now as I
plan on changing/using recurse)
2) change **argv to *argv[]
3) check for error on fork()
2015-03-06 18:31:14 +00:00
sin
e37e3e916a Remove file(1) from TODO
We'll probably develop this outside of sbase.  A simple script that
parses /etc/magic and generates magic.h would be sufficient.

The table can be huge and we do not want to bloat up binary size
only for file(1).
2015-03-06 11:26:28 +00:00
Evan Gates
07c4f784a6 don't mix declarations and code (and one ** to *[]) 2015-03-06 07:33:58 +00:00
FRIGN
90751ab4ba Small fix in mkdir.c
Don't initialize p twice.
2015-03-06 00:34:43 +01:00
FRIGN
edba90934d Small fix in head.c
Only deal with this newline-handling when many is true.
2015-03-06 00:32:22 +01:00
FRIGN
ab26b5583e Audit ln(1)
1) Clarify behaviour when the f-flag is given and a target is in its
   own way.
2) Fix usage()-style.
3) Group local variable declarations.
4) reorder args
5) argc style, other boolean style changes
6) improve error messages
7) set argv[argc - 1] to NULL to allow argv-centric loop later
8) BUGFIX: POSIX specifies that when with the f-flag there's a
   situation where a file stands in its own way for linking it
   should be ignored.
9) Add weprintf() where possible, so we don't pussy out when there's
   a small issue. This is sbase ffs!
2015-03-05 21:14:43 +01:00
FRIGN
aea256c288 Audit chmod(1)
1) Update manpage, refactor the HLP-section and other wordings.
2) BUGFIX: If chmod() fails, don't recurse.
3) Rewrite the arg-loop, fixing several issues:
   BUGFIX: Handle multi-flags (e.g. -RH)
   BUGFIX: Properly handle the termination flag --, error on e.g. --x
   BUGFIX: Error out on an empty flag -.
4) Refactor logic after the arg-loop, which is now simpler thanks
   to argv-incremention.
2015-03-05 19:08:28 +01:00
FRIGN
d9fa4b3ae7 Audit mkdir(1)
1) No need for strchr() in mkdirp or a while-loop. Rewrite it in
   a sane and readable way.
2) fix usage according to the manpage.
3) order includes, don't align local variables.
4) argc-style-fix.
5) BUGFIX: Don't try to chmod() *argv when mkdir() / mkdirp() failed.
6) Add newline before return in two places.
2015-03-05 18:03:08 +01:00
sin
cb04864692 fold: Fix usage and manpage for -width 2015-03-05 08:16:58 +00:00
sin
eca23e5308 tail: Add -num to usage and fix manpage to align with head.1 2015-03-05 08:16:58 +00:00
sin
68ccf5c0eb head: Fix usage and manpage
-N is not a flag but rather a number specification.  Be consistent
and use -num to be consistent with -n num.  num is properly highlighted
now as well.
2015-03-05 08:16:56 +00:00
Evan Gates
796b85a297 test: Minor cleanup
1) use arg.h
2) !strcmp
3) **argv to *argv[]
4) fix test to check if basename(argv0) == "[" but avoid
basename(3p) as it may change the contents of the string
passed to it and I didn't want to make a copy.
2015-03-05 08:04:52 +00:00
FRIGN
38066de8c2 Audit head(1)
1) Use (s)size_t in head().
2) BUGFIX: only check buf[len - 1] when len > 0, else there would
   be an overflow when getline returns 0 (which can happen) and a
   very potential segmentation fault.
3) fix error-messages.
4) update usage().
5) argv-argc-style.
6) clear up the main loop with if (newline).
7) add newline before return.
2015-03-05 01:01:52 +01:00
FRIGN
b7c199e050 Include sys/stat.h in nohup(1)
I accidentally removed it, but it's needed to compile on OpenBSD.
2015-03-05 00:57:14 +01:00
FRIGN
68aa324713 Mark uname(1) as audited in README
No changes needed.
2015-03-05 00:33:28 +01:00
FRIGN
6cdae700c9 Mark time(1) as finished and audited in README 2015-03-05 00:29:03 +01:00
FRIGN
9cb34502fd Add EXIT STATUS to nohup.1 2015-03-05 00:27:36 +01:00
FRIGN
1250a8962f Audit time(1)
1) fix usage().
2) sort includes and comment properly. rename rbeg and rend to r0 and r1.
3) argc style and usage fixes.
4) make error-messages clearer.
5) BUGFIX: It was ignored when fork() failed.
6) Don't call enprintf() after execvp and use _exit instead.
2015-03-05 00:23:09 +01:00
FRIGN
a443014e94 Add time(1) manpage 2015-03-05 00:22:36 +01:00
FRIGN
109f6b5a60 Audit mv(1)
1) Make argument-naming consistent with other tools (cp(1), ...)
2) style fixes
3) usage() fix
4) BUGFIX: Probably from the old non-arg.h days, the directory-
   check was only done when argc > 3, but with arg.h, this ignores
   the case when 3 arguments were given.
   This is actually a pretty serious issue and I'm glad it's fixed.
5) Moreover, be more verbose when stat() fails and make it clearer
   what the hell is going on at this checkpoint.
2015-03-04 23:22:43 +01:00
FRIGN
aaac1c8800 Audit tee(1)
1) "duplicate" implies that you can only specify two outputs,
   "multiply" is a better word describing the functionality.
2) fix other wording in the manpage
3) fix usage()
4) reorder local variables
5) fix sizeof() style
6) we need argv later, don't increment argv and rather iterate
   over argc.
7) Improve error messages, print the filename which the write
   failed to instead of printing the buffer itself (how much
   sense does that make, printing 1024 Bytes of garbage?).
   Also, give the name of the function which failed.
2015-03-04 23:05:11 +01:00
FRIGN
2fa6dc8159 Remove "make" from TODO
Make is part of the toolchain and not in the scope of sbase. There
are already numerous sane make-implementations around.
2015-03-04 22:41:21 +01:00
FRIGN
a6ee96af7c Audit nohup(1)
1) no need to include sys/stat.h
2) remove the enum which just added a layer too thick on this simple
   program
3) argc-style, other style
4) weprintf instead of enprintf, then save the error-message of
   execvp before and return the proper status.
5) write consistent "not reached" comment.
2015-03-04 22:39:12 +01:00