source: rtems-libbsd/rtemsbsd/rtems/syslog.c @ d5eac12

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since d5eac12 was d5eac12, checked in by Sebastian Huber <sebastian.huber@…>, on 10/09/14 at 12:34:13

SYSLOG(3): Replace implementation

Avoid potential buffer overflows on the stack. Expand the %m in the
format string.

  • Property mode set to 100644
File size: 4.1 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup rtems_bsd_rtems
5 *
6 * @brief TODO.
7 */
8
9/*
10 * Copyright (c) 2014 embedded brains GmbH.  All rights reserved.
11 *
12 *  embedded brains GmbH
13 *  Dornierstr. 4
14 *  82178 Puchheim
15 *  Germany
16 *  <rtems@embedded-brains.de>
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 *    notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 *    notice, this list of conditions and the following disclaimer in the
25 *    documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40#include <machine/rtems-bsd-user-space.h>
41#include <machine/rtems-bsd-program.h>
42
43#include <syslog.h>
44#include <errno.h>
45#include <stdio.h>
46#include <string.h>
47
48typedef struct {
49  int mask;
50} rtems_bsd_syslog_context;
51
52/* FIXME: This should be thread specific */
53static rtems_bsd_syslog_context rtems_bsd_syslog_instance = {
54        .mask = LOG_UPTO(LOG_DEBUG)
55};
56
57static const char * const rtems_bsd_syslog_priorities[] = {
58        [LOG_EMERG] = "emerg",
59        [LOG_ALERT] = "alert",
60        [LOG_CRIT] = "crit",
61        [LOG_ERR] = "err",
62        [LOG_WARNING] = "warning",
63        [LOG_NOTICE] = "notice",
64        [LOG_INFO] = "info",
65        [LOG_DEBUG] = "debug"
66};
67
68static rtems_bsd_syslog_context *
69rtems_bsd_syslog_get_context(void)
70{
71        return &rtems_bsd_syslog_instance;
72}
73
74static void
75rtems_bsd_syslog_format_buffer_overflow(void)
76{
77        fputs("err: syslog: format buffer overflow\n", stderr);
78}
79
80void
81syslog(int priority, const char *format, ...)
82{
83        va_list ap;
84
85        va_start(ap, format);
86        vsyslog(priority, format, ap);
87        va_end(ap);
88}
89
90void
91vsyslog(int priority, const char *format, va_list ap)
92{
93        rtems_bsd_syslog_context *ctx = rtems_bsd_syslog_get_context();
94        int pri = LOG_PRI(priority);
95        char fmt[128];
96        char buf[128];
97        const char *src;
98        char *dst;
99        size_t rem;
100        char *m;
101        int n;
102        size_t len;
103
104        if ((LOG_MASK(pri) & ctx->mask) == 0) {
105                return;
106        }
107
108        /* Expand the %m in the format string and add a newline character */
109
110        src = format;
111        dst = &fmt[0];
112        rem = sizeof(fmt) - 2;
113
114        while ((m = strstr(src, "%m")) != NULL) {
115                size_t c = m - src;
116
117                if (c > rem) {
118                        rtems_bsd_syslog_format_buffer_overflow();
119                        return;
120                }
121
122                memcpy(dst, src, c);
123                dst += c;
124                src += c + 2;
125                rem -= c;
126
127                n = sniprintf(dst, rem, "%s", strerror(errno));
128                if (n > rem || n < 0) {
129                        rtems_bsd_syslog_format_buffer_overflow();
130                        return;
131                }
132
133                dst += (size_t) n;
134                rem -= (size_t) n;
135        }
136
137        len = strlen(src);
138        if (len > rem) {
139                rtems_bsd_syslog_format_buffer_overflow();
140                return;
141        }
142
143        memcpy(dst, src, len);
144        dst += len;
145        dst[0] = '\n';
146        dst[1] = '\0';
147
148        /* Print into buffer */
149
150        dst = &buf[0];
151        rem = sizeof(buf) - 1;
152
153        n = sniprintf(dst, rem, "%s: ", rtems_bsd_syslog_priorities[pri]);
154        if (n <= rem) {
155                dst += (size_t) n;
156                rem -= (size_t) n;
157        }
158
159        n = sniprintf(dst, rem, "%s: ", rtems_bsd_program_get_name());
160        if (n <= rem) {
161                dst += (size_t) n;
162                rem -= (size_t) n;
163        }
164
165        vsniprintf(dst, rem, &fmt[0], ap);
166
167        /* Write in one rush */
168
169        fputs(&buf[0], stderr);
170}
171
172void
173openlog(const char *ident, int option, int facility)
174{
175        /* TODO */
176}
177
178void
179closelog(void)
180{
181        /* TODO */
182}
183
184int
185setlogmask(int mask)
186{
187        rtems_bsd_syslog_context *ctx = rtems_bsd_syslog_get_context();
188        int cur = ctx->mask;
189
190        ctx->mask = mask & LOG_UPTO(LOG_DEBUG);
191
192        return cur;
193}
Note: See TracBrowser for help on using the repository browser.