libsemanage: direct_api: clamp write length in ERR_CHLD_STDERR()
Stephen Smalley
maintainerPATCH 1/1 libsemanage: direct_api: clamp write length in ERR_CHLD_STDERR()
snprintf() returns the number of bytes it would have written on truncation, which can exceed the buffer size, and can potentially return < 0 on other error conditions. Clamp the write length to the buffer size less the terminator and only write if snprintf() actually wrote bytes to the buffer.
Signed-off-by Stephen Smalley <[email protected]>
--- libsemanage/src/direct_api.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
libsemanage/src/direct_api.c +10 -2
@@ -618,11 +618,19 @@ static int read_from_pipe_to_data(semanage_handle_t *sh, size_t initial_len,"libsemanage.semanage_pipe_data: " , \ strlen("libsemanage.semanage_pipe_data: ")); \ n =snprintf (buf,sizeof (buf), __VA_ARGS__); \ - (void )!write_full (err_fd[PIPE_WRITE ], buf, n); \ +if (n >0 ) { \ +if ((size_t )n >=sizeof (buf)) \ + n =sizeof (buf) -1 ; \ + (void )!write_full (err_fd[PIPE_WRITE ], buf, (size_t )n); \ + } \if (errsv) { \ errno = errsv; \ n =snprintf (buf,sizeof (buf)," (%m)." ); \ - (void )!write_full (err_fd[PIPE_WRITE ], buf, n); \ +if (n >0 ) { \ +if ((size_t )n >=sizeof (buf)) \ + n =sizeof (buf) -1 ; \ + (void )!write_full (err_fd[PIPE_WRITE ], buf, n); \ + } \ } \ (void )!write_full (err_fd[PIPE_WRITE ],"\n" ,strlen ("\n" )); \ (void )!fsync (err_fd[PIPE_WRITE ]); \
Signature · 1 line
2.55.0
Stephen Smalley
maintainerQuoted 8 lines · Stephen Smalley
snprintf()returns the number of bytes it would have written on truncation, which can exceed the buffer size, and can potentially return < 0 on other error conditions. Clamp the write length to the buffer size less the terminator and only write ifsnprintf()actually wrote bytes to the buffer.Signed-off-by Stephen Smalley <[email protected]>
Merged.
Quoted 33 lines
--- libsemanage/src/direct_api.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)libsemanage/src/direct_api.c +10
-3@@ -618,11 +618,19 @@ static int read_from_pipe_to_data(semanage_handle_t *sh, size_t initial_len,"libsemanage.semanage_pipe_data: " , \ strlen("libsemanage.semanage_pipe_data: ")); \ n =snprintf (buf,sizeof (buf), __VA_ARGS__); \ - (void )!write_full (err_fd[PIPE_WRITE ], buf, n); \ +if (n >0 ) { \ +if ((size_t )n >=sizeof (buf)) \ + n =sizeof (buf) -1 ; \ + (void )!write_full (err_fd[PIPE_WRITE ], buf, (size_t )n); \ + } \if (errsv) { \ errno = errsv; \ n =snprintf (buf,sizeof (buf)," (%m)." ); \ - (void )!write_full (err_fd[PIPE_WRITE ], buf, n); \ +if (n >0 ) { \ +if ((size_t )n >=sizeof (buf)) \ + n =sizeof (buf) -1 ; \ + (void )!write_full (err_fd[PIPE_WRITE ], buf, n); \ + } \ } \ (void )!write_full (err_fd[PIPE_WRITE ],"\n" ,strlen ("\n" )); \ (void )!fsync (err_fd[PIPE_WRITE ]); \ --2.55.0