Hi,

On 16/08/2015 13:08, Sergiy Kibrik wrote:
> Race is possible in ulog_kmsg: if no /dev/kmsg exists
> (e.g. while /dev gets re-mounted) regular file created instead.
> From this point system goes without kernel logger:
> special character file can't be created anymore, all clients keep
> overwriting single message in regular file.
> 
> To avoid this we check if file we're going to write to is really
> character special and ensure it doesn't go away while we check.
> 
> Signed-off-by: Sergiy Kibrik <[email protected]>


the patch is quite invasive. how about just using "r+"

from the man page -->  "r+     Open for reading and writing.  The stream
is positioned at the beginning of the file."

could you try the following and not apply any other changes ?

        John

-       if ((kmsg = fopen("/dev/kmsg", "w")) != NULL) {
+       if ((kmsg = fopen("/dev/kmsg", "r+")) != NULL) {






> ---
>  ulog.c | 17 ++++++++++++++++-
>  1 file changed, 16 insertions(+), 1 deletion(-)
> 
> diff --git a/ulog.c b/ulog.c
> index 776a0c4..10d61da 100644
> --- a/ulog.c
> +++ b/ulog.c
> @@ -23,6 +23,9 @@
>  #include <stdlib.h>
>  #include <unistd.h>
>  #include <string.h>
> +#include <sys/types.h>
> +#include <sys/stat.h>
> +#include <fcntl.h>
>  
>  static int _ulog_channels = -1;
>  static int _ulog_facility = -1;
> @@ -89,8 +92,18 @@ static void ulog_defaults(void)
>  static void ulog_kmsg(int priority, const char *fmt, va_list ap)
>  {
>       FILE *kmsg;
> +     int fd;
> +     struct stat s;
> +
> +     /* extra checks not to accidentally create regular file */
> +     fd = open("/dev/kmsg", O_WRONLY);
> +     if (fd < 0)
> +             return;
> +
> +     if (fstat(fd, &s) == 0 &&
> +         s.st_mode & S_IFCHR &&
> +         (kmsg = fdopen(fd, "w")) != NULL) {
>  
> -     if ((kmsg = fopen("/dev/kmsg", "w")) != NULL) {
>               fprintf(kmsg, "<%u>", priority);
>  
>               if (_ulog_ident)
> @@ -99,6 +112,8 @@ static void ulog_kmsg(int priority, const char *fmt, 
> va_list ap)
>               vfprintf(kmsg, fmt, ap);
>               fclose(kmsg);
>       }
> +
> +     close(fd);
>  }
>  
>  static void ulog_stdio(int priority, const char *fmt, va_list ap)
> 
_______________________________________________
openwrt-devel mailing list
[email protected]
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel

Reply via email to