From 2fa8d6655ac37f9bdeb34420000052d921f4a543 Mon Sep 17 00:00:00 2001 From: Michal Zidek Date: Mon, 15 Apr 2013 16:00:46 +0200 Subject: Rename SAFEALIGN macros. https://fedorahosted.org/sssd/ticket/1772 SAFEALIGN macros have been renamed in this patch to make it easy to pick the right macro when data is copied from byte buffer to a variable or vice versa. The renamed macros are placed in new header file to avoid code duplication (the old ones were defined in two files, one for the client code and one for the rest of sssd). --- Makefile.am | 1 + src/sss_client/sss_cli.h | 36 +-------------- src/util/util.h | 62 +------------------------- src/util/util_safealign.h | 110 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+), 95 deletions(-) create mode 100644 src/util/util_safealign.h diff --git a/Makefile.am b/Makefile.am index b72384a7..94ad8c04 100644 --- a/Makefile.am +++ b/Makefile.am @@ -404,6 +404,7 @@ dist_noinst_HEADERS = \ src/util/atomic_io.h \ src/util/auth_utils.h \ src/util/authtok.h \ + src/util/util_safealign.h \ src/monitor/monitor.h \ src/monitor/monitor_interfaces.h \ src/responder/common/responder.h \ diff --git a/src/sss_client/sss_cli.h b/src/sss_client/sss_cli.h index a7d99b43..285a2979 100644 --- a/src/sss_client/sss_cli.h +++ b/src/sss_client/sss_cli.h @@ -32,6 +32,8 @@ #include #include +#include "util/util_safealign.h" + #ifndef HAVE_ERRNO_T #define HAVE_ERRNO_T typedef int errno_t; @@ -526,40 +528,6 @@ int sss_ssh_make_request(enum sss_cli_command cmd, uint8_t **repbuf, size_t *replen, int *errnop); -#ifndef SAFEALIGN_COPY_UINT32 -static inline void -safealign_memcpy(void *dest, const void *src, size_t n, size_t *counter) -{ - memcpy(dest, src, n); - if (counter) { - *counter += n; - } -} - -#define SAFEALIGN_SET_VALUE(dest, value, type, pctr) do { \ - type CV_MACRO_val = (type)(value); \ - safealign_memcpy(dest, &CV_MACRO_val, sizeof(type), pctr); \ -} while(0) - -#ifndef SAFEALIGN_SET_UINT32 -#define SAFEALIGN_SET_UINT32(dest, value, pctr) \ - SAFEALIGN_SET_VALUE(dest, value, uint32_t, pctr) -#endif - -#define SAFEALIGN_COPY_UINT32(dest, src, pctr) \ - safealign_memcpy(dest, src, sizeof(uint32_t), pctr) -#endif - -#ifndef SAFEALIGN_SET_UINT16 -#define SAFEALIGN_SET_UINT16(dest, value, pctr) \ - SAFEALIGN_SET_VALUE(dest, value, uint16_t, pctr) -#endif - -#ifndef SAFEALIGN_COPY_UINT16 -#define SAFEALIGN_COPY_UINT16(dest, src, pctr) \ - safealign_memcpy(dest, src, sizeof(uint16_t), pctr) -#endif - #if 0 /* GETSPNAM Request: diff --git a/src/util/util.h b/src/util/util.h index 49dc850c..fa45fdfd 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -45,6 +45,7 @@ #include "util/atomic_io.h" #include "util/util_errors.h" +#include "util/util_safealign.h" #define _(STRING) gettext (STRING) @@ -270,67 +271,6 @@ errno_t set_debug_file_from_fd(const int fd); #define OUT_OF_ID_RANGE(id, min, max) \ (id == 0 || (min && (id < min)) || (max && (id > max))) -#define SIZE_T_MAX ((size_t) -1) - -#define SIZE_T_OVERFLOW(current, add) \ - (((size_t)(add)) > (SIZE_T_MAX - ((size_t)(current)))) - -static inline void -safealign_memcpy(void *dest, const void *src, size_t n, size_t *counter) -{ - memcpy(dest, src, n); - if (counter) { - *counter += n; - } -} - -#define SAFEALIGN_SET_VALUE(dest, value, type, pctr) do { \ - type CV_MACRO_val = (type)(value); \ - safealign_memcpy(dest, &CV_MACRO_val, sizeof(type), pctr); \ -} while(0) - -#define SAFEALIGN_COPY_INT64(dest, src, pctr) \ - safealign_memcpy(dest, src, sizeof(int64_t), pctr) - -#define SAFEALIGN_SET_INT64(dest, value, pctr) \ - SAFEALIGN_SET_VALUE(dest, value, int64_t, pctr) - -#define SAFEALIGN_COPY_UINT32(dest, src, pctr) \ - safealign_memcpy(dest, src, sizeof(uint32_t), pctr) - -#define SAFEALIGN_SET_UINT32(dest, value, pctr) \ - SAFEALIGN_SET_VALUE(dest, value, uint32_t, pctr) - -#define SAFEALIGN_COPY_INT32(dest, src, pctr) \ - safealign_memcpy(dest, src, sizeof(int32_t), pctr) - -#define SAFEALIGN_SET_INT32(dest, value, pctr) \ - SAFEALIGN_SET_VALUE(dest, value, int32_t, pctr) - -#define SAFEALIGN_COPY_UINT16(dest, src, pctr) \ - safealign_memcpy(dest, src, sizeof(uint16_t), pctr) - -#define SAFEALIGN_SET_UINT16(dest, value, pctr) \ - SAFEALIGN_SET_VALUE(dest, value, uint16_t, pctr) - -#define SAFEALIGN_COPY_UINT32_CHECK(dest, src, len, pctr) do { \ - if ((*(pctr) + sizeof(uint32_t)) > (len) || \ - SIZE_T_OVERFLOW(*(pctr), sizeof(uint32_t))) return EINVAL; \ - safealign_memcpy(dest, src, sizeof(uint32_t), pctr); \ -} while(0) - -#define SAFEALIGN_COPY_INT32_CHECK(dest, src, len, pctr) do { \ - if ((*(pctr) + sizeof(int32_t)) > (len) || \ - SIZE_T_OVERFLOW(*(pctr), sizeof(int32_t))) return EINVAL; \ - safealign_memcpy(dest, src, sizeof(int32_t), pctr); \ -} while(0) - -#define SAFEALIGN_COPY_UINT16_CHECK(dest, src, len, pctr) do { \ - if ((*(pctr) + sizeof(uint16_t)) > (len) || \ - SIZE_T_OVERFLOW(*(pctr), sizeof(uint16_t))) return EINVAL; \ - safealign_memcpy(dest, src, sizeof(uint16_t), pctr); \ -} while(0) - #include "util/dlinklist.h" /* From debug.c */ diff --git a/src/util/util_safealign.h b/src/util/util_safealign.h new file mode 100644 index 00000000..d1d7a486 --- /dev/null +++ b/src/util/util_safealign.h @@ -0,0 +1,110 @@ +/* + SSSD + + Authors: + Simo Sorce + + Copyright (C) Red Hat, Inc 2007 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ + +/* CAUTION: + * This file is also used in sss_client (pam, nss). Therefore it has to be + * minimalist and cannot include DEBUG macros or header file util.h. + */ + + +#ifndef _UTIL_SAFEALIGN_H +#define _UTIL_SAFEALIGN_H + +#include + +#define SIZE_T_MAX ((size_t) -1) + +#define SIZE_T_OVERFLOW(current, add) \ + (((size_t)(add)) > (SIZE_T_MAX - ((size_t)(current)))) + +static inline void +safealign_memcpy(void *dest, const void *src, size_t n, size_t *counter) +{ + memcpy(dest, src, n); + if (counter) { + *counter += n; + } +} + +#define SAFEALIGN_VAR2BUF(dest, value, type, pctr) do { \ + type CV_MACRO_val = (type)(value); \ + safealign_memcpy(dest, &CV_MACRO_val, sizeof(type), pctr); \ +} while(0) + +#define SAFEALIGN_BUF2VAR_INT64(dest, src, pctr) \ + safealign_memcpy(dest, src, sizeof(int64_t), pctr) + +#define SAFEALIGN_VAR2BUF_INT64(dest, value, pctr) \ + SAFEALIGN_VAR2BUF(dest, value, int64_t, pctr) + +#define SAFEALIGN_BUF2VAR_UINT32(dest, src, pctr) \ + safealign_memcpy(dest, src, sizeof(uint32_t), pctr) + +#define SAFEALIGN_VAR2BUF_UINT32(dest, value, pctr) \ + SAFEALIGN_VAR2BUF(dest, value, uint32_t, pctr) + +#define SAFEALIGN_BUF2VAR_INT32(dest, src, pctr) \ + safealign_memcpy(dest, src, sizeof(int32_t), pctr) + +#define SAFEALIGN_VAR2BUF_INT32(dest, value, pctr) \ + SAFEALIGN_VAR2BUF(dest, value, int32_t, pctr) + +#define SAFEALIGN_BUF2VAR_UINT16(dest, src, pctr) \ + safealign_memcpy(dest, src, sizeof(uint16_t), pctr) + +#define SAFEALIGN_VAR2BUF_UINT16(dest, value, pctr) \ + SAFEALIGN_VAR2BUF(dest, value, uint16_t, pctr) + +#define SAFEALIGN_BUF2VAR_UINT32_CHECK(dest, src, len, pctr) do { \ + if ((*(pctr) + sizeof(uint32_t)) > (len) || \ + SIZE_T_OVERFLOW(*(pctr), sizeof(uint32_t))) return EINVAL; \ + safealign_memcpy(dest, src, sizeof(uint32_t), pctr); \ +} while(0) + +#define SAFEALIGN_BUF2VAR_INT32_CHECK(dest, src, len, pctr) do { \ + if ((*(pctr) + sizeof(int32_t)) > (len) || \ + SIZE_T_OVERFLOW(*(pctr), sizeof(int32_t))) return EINVAL; \ + safealign_memcpy(dest, src, sizeof(int32_t), pctr); \ +} while(0) + +#define SAFEALIGN_BUF2VAR_UINT16_CHECK(dest, src, len, pctr) do { \ + if ((*(pctr) + sizeof(uint16_t)) > (len) || \ + SIZE_T_OVERFLOW(*(pctr), sizeof(uint16_t))) return EINVAL; \ + safealign_memcpy(dest, src, sizeof(uint16_t), pctr); \ +} while(0) + + +/* Do not use these aliases in new code. Use the macros above instead. */ +#define SAFEALIGN_SET_VALUE SAFEALIGN_VAR2BUF +#define SAFEALIGN_COPY_INT64 SAFEALIGN_BUF2VAR_INT64 +#define SAFEALIGN_SET_INT64 SAFEALIGN_VAR2BUF_INT64 +#define SAFEALIGN_COPY_UINT32 SAFEALIGN_BUF2VAR_UINT32 +#define SAFEALIGN_SET_UINT32 SAFEALIGN_VAR2BUF_UINT32 +#define SAFEALIGN_COPY_INT32 SAFEALIGN_BUF2VAR_INT32 +#define SAFEALIGN_SET_INT32 SAFEALIGN_VAR2BUF_INT32 +#define SAFEALIGN_COPY_UINT16 SAFEALIGN_BUF2VAR_UINT16 +#define SAFEALIGN_SET_UINT16 SAFEALIGN_VAR2BUF_UINT16 +#define SAFEALIGN_COPY_UINT32_CHECK SAFEALIGN_BUF2VAR_UINT32_CHECK +#define SAFEALIGN_COPY_INT32_CHECK SAFEALIGN_BUF2VAR_INT32_CHECK +#define SAFEALIGN_COPY_UINT16_CHECK SAFEALIGN_BUF2VAR_UINT16_CHECK + +#endif /* _UTIL_SAFEALIGN_H */ -- cgit