linux-fscrypt.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [fsverity-utils PATCH v2 1/2] Remove unneeded includes
@ 2020-12-17 14:47 luca.boccassi
  2020-12-17 14:47 ` [fsverity-utils PATCH v2 2/2] Allow to build and run sign/digest on Windows luca.boccassi
                   ` (2 more replies)
  0 siblings, 3 replies; 34+ messages in thread
From: luca.boccassi @ 2020-12-17 14:47 UTC (permalink / raw)
  To: linux-fscrypt; +Cc: ebiggers

From: Luca Boccassi <luca.boccassi@microsoft.com>

Signed-off-by: Luca Boccassi <luca.boccassi@microsoft.com>
---
v2: do not remove includes from fsverity_uapi.h, actually needed

 programs/cmd_enable.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/programs/cmd_enable.c b/programs/cmd_enable.c
index fdf26c7..14c3c17 100644
--- a/programs/cmd_enable.c
+++ b/programs/cmd_enable.c
@@ -14,7 +14,6 @@
 #include <fcntl.h>
 #include <getopt.h>
 #include <limits.h>
-#include <sys/ioctl.h>
 
 static bool read_signature(const char *filename, u8 **sig_ret,
 			   u32 *sig_size_ret)
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [fsverity-utils PATCH v2 2/2] Allow to build and run sign/digest on Windows
  2020-12-17 14:47 [fsverity-utils PATCH v2 1/2] Remove unneeded includes luca.boccassi
@ 2020-12-17 14:47 ` luca.boccassi
  2020-12-17 18:32   ` Eric Biggers
  2020-12-17 19:16 ` [fsverity-utils PATCH v3 1/2] Remove unneeded includes luca.boccassi
  2020-12-17 19:25 ` [fsverity-utils PATCH v4 1/2] Remove unneeded includes luca.boccassi
  2 siblings, 1 reply; 34+ messages in thread
From: luca.boccassi @ 2020-12-17 14:47 UTC (permalink / raw)
  To: linux-fscrypt; +Cc: ebiggers

From: Luca Boccassi <luca.boccassi@microsoft.com>

Add some minimal compat type defs, and stub out the enable/measure
sources. Also add a way to handle the fact that mingw adds a
.exe extension automatically in the Makefile install rules, and
that there is not pkg-config and the libcrypto linker flag is
different.

Signed-off-by: Luca Boccassi <luca.boccassi@microsoft.com>
---
v2: rework the stubbing out to detect mingw in the Makefile and remove
    sources from compilation, instead of ifdefs.
    add a new common/win32_defs.h for the compat definitions.
    define strerror_r using strerror_s.

    To compile with mingw:
      make CC=x86_64-w64-mingw32-gcc-8.3-win32
    note that the openssl headers and a win32 libcrypto.dll need
    to be available in the default search paths, and otherwise have
    to be specified as expected via CPPFLAGS/LDFLAGS

 Makefile               | 36 ++++++++++++++++++-------
 common/common_defs.h   |  2 ++
 common/fsverity_uapi.h |  2 ++
 common/win32_defs.h    | 61 ++++++++++++++++++++++++++++++++++++++++++
 lib/utils.c            | 11 +++++++-
 programs/fsverity.c    |  2 ++
 programs/utils.c       |  2 +-
 7 files changed, 105 insertions(+), 11 deletions(-)
 create mode 100644 common/win32_defs.h

diff --git a/Makefile b/Makefile
index bfe83c4..cc62818 100644
--- a/Makefile
+++ b/Makefile
@@ -35,6 +35,11 @@
 cc-option = $(shell if $(CC) $(1) -c -x c /dev/null -o /dev/null > /dev/null 2>&1; \
 	      then echo $(1); fi)
 
+# Support building with MinGW for minimal Windows fsverity.exe
+ifneq ($(findstring -mingw,$(shell $(CC) -dumpmachine 2>/dev/null)),)
+MINGW = 1
+endif
+
 CFLAGS ?= -O2
 
 override CFLAGS := -Wall -Wundef				\
@@ -62,7 +67,14 @@ BINDIR          ?= $(PREFIX)/bin
 INCDIR          ?= $(PREFIX)/include
 LIBDIR          ?= $(PREFIX)/lib
 DESTDIR         ?=
+ifneq ($(MINGW),1)
 PKGCONF         ?= pkg-config
+LIBCRYPTO       := -lcrypto
+else
+PKGCONF         := false
+LIBCRYPTO       := -l:libcrypto.dll
+EXEEXT          := .exe
+endif
 
 # Rebuild if a user-specified setting that affects the build changed.
 .build-config: FORCE
@@ -81,15 +93,15 @@ PKGCONF         ?= pkg-config
 
 DEFAULT_TARGETS :=
 COMMON_HEADERS  := $(wildcard common/*.h)
-LDLIBS          := $(shell "$(PKGCONF)" libcrypto --libs 2>/dev/null || echo -lcrypto)
+LDLIBS          := $(shell "$(PKGCONF)" libcrypto --libs 2>/dev/null || echo $(LIBCRYPTO))
 CFLAGS          += $(shell "$(PKGCONF)" libcrypto --cflags 2>/dev/null || echo)
 
 # If we are dynamically linking, when running tests we need to override
 # LD_LIBRARY_PATH as no RPATH is set
 ifdef USE_SHARED_LIB
-RUN_FSVERITY    = LD_LIBRARY_PATH=./ ./fsverity
+RUN_FSVERITY    = LD_LIBRARY_PATH=./ ./fsverity$(EXEEXT)
 else
-RUN_FSVERITY    = ./fsverity
+RUN_FSVERITY    = ./fsverity$(EXEEXT)
 endif
 
 ##############################################################################
@@ -99,6 +111,9 @@ endif
 SOVERSION       := 0
 LIB_CFLAGS      := $(CFLAGS) -fvisibility=hidden
 LIB_SRC         := $(wildcard lib/*.c)
+ifeq ($(MINGW),1)
+LIB_SRC         := $(filter-out lib/enable.c,${LIB_SRC})
+endif
 LIB_HEADERS     := $(wildcard lib/*.h) $(COMMON_HEADERS)
 STATIC_LIB_OBJ  := $(LIB_SRC:.c=.o)
 SHARED_LIB_OBJ  := $(LIB_SRC:.c=.shlib.o)
@@ -141,10 +156,13 @@ PROG_COMMON_SRC   := programs/utils.c
 PROG_COMMON_OBJ   := $(PROG_COMMON_SRC:.c=.o)
 FSVERITY_PROG_OBJ := $(PROG_COMMON_OBJ)		\
 		     programs/cmd_digest.o	\
-		     programs/cmd_enable.o	\
-		     programs/cmd_measure.o	\
 		     programs/cmd_sign.o	\
 		     programs/fsverity.o
+ifneq ($(MINGW),1)
+FSVERITY_PROG_OBJ += \
+		     programs/cmd_enable.o	\
+		     programs/cmd_measure.o
+endif
 TEST_PROG_SRC     := $(wildcard programs/test_*.c)
 TEST_PROGRAMS     := $(TEST_PROG_SRC:programs/%.c=%)
 
@@ -186,7 +204,7 @@ test_programs:$(TEST_PROGRAMS)
 # want to run the full tests.
 check:fsverity test_programs
 	for prog in $(TEST_PROGRAMS); do \
-		$(TEST_WRAPPER_PROG) ./$$prog || exit 1; \
+		$(TEST_WRAPPER_PROG) ./$$prog$(EXEEXT) || exit 1; \
 	done
 	$(RUN_FSVERITY) --help > /dev/null
 	$(RUN_FSVERITY) --version > /dev/null
@@ -202,7 +220,7 @@ check:fsverity test_programs
 
 install:all
 	install -d $(DESTDIR)$(LIBDIR)/pkgconfig $(DESTDIR)$(INCDIR) $(DESTDIR)$(BINDIR)
-	install -m755 fsverity $(DESTDIR)$(BINDIR)
+	install -m755 fsverity$(EXEEXT) $(DESTDIR)$(BINDIR)
 	install -m644 libfsverity.a $(DESTDIR)$(LIBDIR)
 	install -m755 libfsverity.so.$(SOVERSION) $(DESTDIR)$(LIBDIR)
 	ln -sf libfsverity.so.$(SOVERSION) $(DESTDIR)$(LIBDIR)/libfsverity.so
@@ -215,7 +233,7 @@ install:all
 	chmod 644 $(DESTDIR)$(LIBDIR)/pkgconfig/libfsverity.pc
 
 uninstall:
-	rm -f $(DESTDIR)$(BINDIR)/fsverity
+	rm -f $(DESTDIR)$(BINDIR)/fsverity$(EXEEXT)
 	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.a
 	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.so.$(SOVERSION)
 	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.so
@@ -232,4 +250,4 @@ help:
 
 clean:
 	rm -f $(DEFAULT_TARGETS) $(TEST_PROGRAMS) \
-		lib/*.o programs/*.o .build-config fsverity.sig
+		fsverity$(EXEEXT) lib/*.o programs/*.o .build-config fsverity.sig
diff --git a/common/common_defs.h b/common/common_defs.h
index 279385a..3ae5561 100644
--- a/common/common_defs.h
+++ b/common/common_defs.h
@@ -15,6 +15,8 @@
 #include <stddef.h>
 #include <stdint.h>
 
+#include "win32_defs.h"
+
 typedef uint8_t u8;
 typedef uint16_t u16;
 typedef uint32_t u32;
diff --git a/common/fsverity_uapi.h b/common/fsverity_uapi.h
index 33f4415..be1d3f6 100644
--- a/common/fsverity_uapi.h
+++ b/common/fsverity_uapi.h
@@ -10,8 +10,10 @@
 #ifndef _UAPI_LINUX_FSVERITY_H
 #define _UAPI_LINUX_FSVERITY_H
 
+#ifndef _WIN32
 #include <linux/ioctl.h>
 #include <linux/types.h>
+#endif /* _WIN32 */
 
 #define FS_VERITY_HASH_ALG_SHA256	1
 #define FS_VERITY_HASH_ALG_SHA512	2
diff --git a/common/win32_defs.h b/common/win32_defs.h
new file mode 100644
index 0000000..e13938a
--- /dev/null
+++ b/common/win32_defs.h
@@ -0,0 +1,61 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * WIN32 compat definitions for libfsverity and the 'fsverity' program
+ *
+ * Copyright 2020 Microsoft
+ *
+ * Use of this source code is governed by an MIT-style
+ * license that can be found in the LICENSE file or at
+ * https://opensource.org/licenses/MIT.
+ */
+#ifndef COMMON_WIN32_DEFS_H
+#define COMMON_WIN32_DEFS_H
+
+/* Some minimal definitions to allow the digest/sign commands to run under Windows */
+
+/* All file reads we do need this flag on _WIN32 */
+#ifndef O_BINARY
+#  define O_BINARY 0
+#endif
+
+#ifdef _WIN32
+
+#include <stdint.h>
+#include <inttypes.h>
+
+#ifdef _WIN64
+#  define SIZET_PF PRIu64
+#else
+#  define SIZET_PF PRIu32
+#endif
+
+#ifndef ENOPKG
+#   define ENOPKG 65
+#endif
+
+#ifndef __cold
+#  define __cold
+#endif
+
+typedef __signed__ char __s8;
+typedef unsigned char __u8;
+typedef __signed__ short __s16;
+typedef unsigned short __u16;
+typedef __signed__ int __s32;
+typedef unsigned int __u32;
+typedef __signed__ long long  __s64;
+typedef unsigned long long  __u64;
+typedef __u16 __le16;
+typedef __u16 __be16;
+typedef __u32 __le32;
+typedef __u32 __be32;
+typedef __u64 __le64;
+typedef __u64 __be64;
+
+#else
+
+#define SIZET_PF "zu"
+
+#endif /* _WIN32 */
+
+#endif /* COMMON_WIN32_DEFS_H */
diff --git a/lib/utils.c b/lib/utils.c
index 8b5d6cb..8bb4413 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -22,7 +22,7 @@ static void *xmalloc(size_t size)
 	void *p = malloc(size);
 
 	if (!p)
-		libfsverity_error_msg("out of memory (tried to allocate %zu bytes)",
+		libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
 				      size);
 	return p;
 }
@@ -53,6 +53,15 @@ libfsverity_set_error_callback(void (*cb)(const char *msg))
 	libfsverity_error_cb = cb;
 }
 
+#ifdef _WIN32
+static char *strerror_r(int errnum, char *buf, size_t buflen)
+{
+	strerror_s(buf, buflen, errnum);
+
+	return buf;
+}
+#endif
+
 void libfsverity_do_error_msg(const char *format, va_list va, int err)
 {
 	int saved_errno = errno;
diff --git a/programs/fsverity.c b/programs/fsverity.c
index 5d5fbe2..f68e034 100644
--- a/programs/fsverity.c
+++ b/programs/fsverity.c
@@ -28,6 +28,7 @@ static const struct fsverity_command {
 "    fsverity digest FILE...\n"
 "               [--hash-alg=HASH_ALG] [--block-size=BLOCK_SIZE] [--salt=SALT]\n"
 "               [--compact] [--for-builtin-sig]\n"
+#ifndef _WIN32
 	}, {
 		.name = "enable",
 		.func = fsverity_cmd_enable,
@@ -43,6 +44,7 @@ static const struct fsverity_command {
 "Display the fs-verity digest of the given verity file(s)",
 		.usage_str =
 "    fsverity measure FILE...\n"
+#endif /* _WIN32 */
 	}, {
 		.name = "sign",
 		.func = fsverity_cmd_sign,
diff --git a/programs/utils.c b/programs/utils.c
index facccda..ce19b57 100644
--- a/programs/utils.c
+++ b/programs/utils.c
@@ -102,7 +102,7 @@ void install_libfsverity_error_handler(void)
 
 bool open_file(struct filedes *file, const char *filename, int flags, int mode)
 {
-	file->fd = open(filename, flags, mode);
+	file->fd = open(filename, flags | O_BINARY, mode);
 	if (file->fd < 0) {
 		error_msg_errno("can't open '%s' for %s", filename,
 				(flags & O_ACCMODE) == O_RDONLY ? "reading" :
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* Re: [fsverity-utils PATCH v2 2/2] Allow to build and run sign/digest on Windows
  2020-12-17 14:47 ` [fsverity-utils PATCH v2 2/2] Allow to build and run sign/digest on Windows luca.boccassi
@ 2020-12-17 18:32   ` Eric Biggers
  2020-12-17 18:44     ` Luca Boccassi
  0 siblings, 1 reply; 34+ messages in thread
From: Eric Biggers @ 2020-12-17 18:32 UTC (permalink / raw)
  To: luca.boccassi; +Cc: linux-fscrypt

On Thu, Dec 17, 2020 at 02:47:49PM +0000, luca.boccassi@gmail.com wrote:
> From: Luca Boccassi <luca.boccassi@microsoft.com>
> 
> Add some minimal compat type defs, and stub out the enable/measure
> sources. Also add a way to handle the fact that mingw adds a
> .exe extension automatically in the Makefile install rules, and
> that there is not pkg-config and the libcrypto linker flag is
> different.
> 
> Signed-off-by: Luca Boccassi <luca.boccassi@microsoft.com>
> ---
> v2: rework the stubbing out to detect mingw in the Makefile and remove
>     sources from compilation, instead of ifdefs.
>     add a new common/win32_defs.h for the compat definitions.
>     define strerror_r using strerror_s.
> 
>     To compile with mingw:
>       make CC=x86_64-w64-mingw32-gcc-8.3-win32
>     note that the openssl headers and a win32 libcrypto.dll need
>     to be available in the default search paths, and otherwise have
>     to be specified as expected via CPPFLAGS/LDFLAGS
> 

I got some warnings and an error when compiling:

$ make CC=x86_64-w64-mingw32-gcc
  CC       lib/compute_digest.o
  CC       lib/hash_algs.o
  CC       lib/sign_digest.o
  CC       lib/utils.o
lib/utils.c: In function ‘xmalloc’:
lib/utils.c:25:25: warning: unknown conversion type character ‘l’ in format [-Wformat=]
   25 |   libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from lib/../common/win32_defs.h:24,
                 from lib/../common/common_defs.h:18,
                 from lib/lib_private.h:15,
                 from lib/utils.c:14:
/usr/x86_64-w64-mingw32/include/inttypes.h:36:18: note: format string is defined here
   36 | #define PRIu64 "llu"
      |                  ^
lib/utils.c:25:25: warning: too many arguments for format [-Wformat-extra-args]
   25 |   libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  AR       libfsverity.a
  CC       lib/compute_digest.shlib.o
  CC       lib/hash_algs.shlib.o
  CC       lib/sign_digest.shlib.o
  CC       lib/utils.shlib.o
lib/utils.c: In function ‘xmalloc’:
lib/utils.c:25:25: warning: unknown conversion type character ‘l’ in format [-Wformat=]
   25 |   libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from lib/../common/win32_defs.h:24,
                 from lib/../common/common_defs.h:18,
                 from lib/lib_private.h:15,
                 from lib/utils.c:14:
/usr/x86_64-w64-mingw32/include/inttypes.h:36:18: note: format string is defined here
   36 | #define PRIu64 "llu"
      |                  ^
lib/utils.c:25:25: warning: too many arguments for format [-Wformat-extra-args]
   25 |   libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  CCLD     libfsverity.so.0
/usr/lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -l:libcrypto.dll
collect2: error: ld returned 1 exit status
make: *** [Makefile:137: libfsverity.so.0] Error 1



This is on Arch Linux with mingw-w64-gcc and mingw-w64-openssl installed.

So there's something wrong with the SIZET_PF format string, and also
-l:libcrypto.dll isn't correct; it should be just -lcrypto like it is on Linux.
(MinGW knows to look for a .dll file.)

- Eric

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [fsverity-utils PATCH v2 2/2] Allow to build and run sign/digest on Windows
  2020-12-17 18:32   ` Eric Biggers
@ 2020-12-17 18:44     ` Luca Boccassi
  2020-12-17 19:05       ` Eric Biggers
  0 siblings, 1 reply; 34+ messages in thread
From: Luca Boccassi @ 2020-12-17 18:44 UTC (permalink / raw)
  To: Eric Biggers; +Cc: linux-fscrypt

[-- Attachment #1: Type: text/plain, Size: 4137 bytes --]

On Thu, 2020-12-17 at 10:32 -0800, Eric Biggers wrote:
> On Thu, Dec 17, 2020 at 02:47:49PM +0000, luca.boccassi@gmail.com wrote:
> > From: Luca Boccassi <luca.boccassi@microsoft.com>
> > 
> > Add some minimal compat type defs, and stub out the enable/measure
> > sources. Also add a way to handle the fact that mingw adds a
> > .exe extension automatically in the Makefile install rules, and
> > that there is not pkg-config and the libcrypto linker flag is
> > different.
> > 
> > Signed-off-by: Luca Boccassi <luca.boccassi@microsoft.com>
> > ---
> > v2: rework the stubbing out to detect mingw in the Makefile and remove
> >     sources from compilation, instead of ifdefs.
> >     add a new common/win32_defs.h for the compat definitions.
> >     define strerror_r using strerror_s.
> > 
> >     To compile with mingw:
> >       make CC=x86_64-w64-mingw32-gcc-8.3-win32
> >     note that the openssl headers and a win32 libcrypto.dll need
> >     to be available in the default search paths, and otherwise have
> >     to be specified as expected via CPPFLAGS/LDFLAGS
> > 
> 
> I got some warnings and an error when compiling:
> 
> $ make CC=x86_64-w64-mingw32-gcc
>   CC       lib/compute_digest.o
>   CC       lib/hash_algs.o
>   CC       lib/sign_digest.o
>   CC       lib/utils.o
> lib/utils.c: In function ‘xmalloc’:
> lib/utils.c:25:25: warning: unknown conversion type character ‘l’ in format [-Wformat=]
>    25 |   libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
>       |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> In file included from lib/../common/win32_defs.h:24,
>                  from lib/../common/common_defs.h:18,
>                  from lib/lib_private.h:15,
>                  from lib/utils.c:14:
> /usr/x86_64-w64-mingw32/include/inttypes.h:36:18: note: format string is defined here
>    36 | #define PRIu64 "llu"
>       |                  ^
> lib/utils.c:25:25: warning: too many arguments for format [-Wformat-extra-args]
>    25 |   libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
>       |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>   AR       libfsverity.a
>   CC       lib/compute_digest.shlib.o
>   CC       lib/hash_algs.shlib.o
>   CC       lib/sign_digest.shlib.o
>   CC       lib/utils.shlib.o
> lib/utils.c: In function ‘xmalloc’:
> lib/utils.c:25:25: warning: unknown conversion type character ‘l’ in format [-Wformat=]
>    25 |   libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
>       |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> In file included from lib/../common/win32_defs.h:24,
>                  from lib/../common/common_defs.h:18,
>                  from lib/lib_private.h:15,
>                  from lib/utils.c:14:
> /usr/x86_64-w64-mingw32/include/inttypes.h:36:18: note: format string is defined here
>    36 | #define PRIu64 "llu"
>       |                  ^
> lib/utils.c:25:25: warning: too many arguments for format [-Wformat-extra-args]
>    25 |   libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
>       |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>   CCLD     libfsverity.so.0
> /usr/lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -l:libcrypto.dll
> collect2: error: ld returned 1 exit status
> make: *** [Makefile:137: libfsverity.so.0] Error 1
> 
> 
> 
> This is on Arch Linux with mingw-w64-gcc and mingw-w64-openssl installed.
> 
> So there's something wrong with the SIZET_PF format string, and also
> -l:libcrypto.dll isn't correct; it should be just -lcrypto like it is on Linux.
> (MinGW knows to look for a .dll file.)
> 
> - Eric

Mmh I don't get any warnings on Debian - gcc-mingw-w64-x86-64 8.3.0-
6+21.3~deb10u1 - any idea how to fix it?

And on -lcrypto, it didn't use to work before the refactor - but now it
does. I have no clue what was happening. Will change it back in v3.

-- 
Kind regards,
Luca Boccassi

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [fsverity-utils PATCH v2 2/2] Allow to build and run sign/digest on Windows
  2020-12-17 18:44     ` Luca Boccassi
@ 2020-12-17 19:05       ` Eric Biggers
  2020-12-17 19:12         ` Luca Boccassi
  0 siblings, 1 reply; 34+ messages in thread
From: Eric Biggers @ 2020-12-17 19:05 UTC (permalink / raw)
  To: Luca Boccassi; +Cc: linux-fscrypt

On Thu, Dec 17, 2020 at 06:44:38PM +0000, Luca Boccassi wrote:
> On Thu, 2020-12-17 at 10:32 -0800, Eric Biggers wrote:
> > On Thu, Dec 17, 2020 at 02:47:49PM +0000, luca.boccassi@gmail.com wrote:
> > > From: Luca Boccassi <luca.boccassi@microsoft.com>
> > > 
> > > Add some minimal compat type defs, and stub out the enable/measure
> > > sources. Also add a way to handle the fact that mingw adds a
> > > .exe extension automatically in the Makefile install rules, and
> > > that there is not pkg-config and the libcrypto linker flag is
> > > different.
> > > 
> > > Signed-off-by: Luca Boccassi <luca.boccassi@microsoft.com>
> > > ---
> > > v2: rework the stubbing out to detect mingw in the Makefile and remove
> > >     sources from compilation, instead of ifdefs.
> > >     add a new common/win32_defs.h for the compat definitions.
> > >     define strerror_r using strerror_s.
> > > 
> > >     To compile with mingw:
> > >       make CC=x86_64-w64-mingw32-gcc-8.3-win32
> > >     note that the openssl headers and a win32 libcrypto.dll need
> > >     to be available in the default search paths, and otherwise have
> > >     to be specified as expected via CPPFLAGS/LDFLAGS
> > > 
> > 
> > I got some warnings and an error when compiling:
> > 
> > $ make CC=x86_64-w64-mingw32-gcc
> >   CC       lib/compute_digest.o
> >   CC       lib/hash_algs.o
> >   CC       lib/sign_digest.o
> >   CC       lib/utils.o
> > lib/utils.c: In function ‘xmalloc’:
> > lib/utils.c:25:25: warning: unknown conversion type character ‘l’ in format [-Wformat=]
> >    25 |   libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
> >       |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > In file included from lib/../common/win32_defs.h:24,
> >                  from lib/../common/common_defs.h:18,
> >                  from lib/lib_private.h:15,
> >                  from lib/utils.c:14:
> > /usr/x86_64-w64-mingw32/include/inttypes.h:36:18: note: format string is defined here
> >    36 | #define PRIu64 "llu"
> >       |                  ^
> > lib/utils.c:25:25: warning: too many arguments for format [-Wformat-extra-args]
> >    25 |   libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
> >       |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >   AR       libfsverity.a
> >   CC       lib/compute_digest.shlib.o
> >   CC       lib/hash_algs.shlib.o
> >   CC       lib/sign_digest.shlib.o
> >   CC       lib/utils.shlib.o
> > lib/utils.c: In function ‘xmalloc’:
> > lib/utils.c:25:25: warning: unknown conversion type character ‘l’ in format [-Wformat=]
> >    25 |   libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
> >       |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > In file included from lib/../common/win32_defs.h:24,
> >                  from lib/../common/common_defs.h:18,
> >                  from lib/lib_private.h:15,
> >                  from lib/utils.c:14:
> > /usr/x86_64-w64-mingw32/include/inttypes.h:36:18: note: format string is defined here
> >    36 | #define PRIu64 "llu"
> >       |                  ^
> > lib/utils.c:25:25: warning: too many arguments for format [-Wformat-extra-args]
> >    25 |   libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
> >       |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >   CCLD     libfsverity.so.0
> > /usr/lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -l:libcrypto.dll
> > collect2: error: ld returned 1 exit status
> > make: *** [Makefile:137: libfsverity.so.0] Error 1
> > 
> > 
> > 
> > This is on Arch Linux with mingw-w64-gcc and mingw-w64-openssl installed.
> > 
> > So there's something wrong with the SIZET_PF format string, and also
> > -l:libcrypto.dll isn't correct; it should be just -lcrypto like it is on Linux.
> > (MinGW knows to look for a .dll file.)
> > 
> > - Eric
> 
> Mmh I don't get any warnings on Debian - gcc-mingw-w64-x86-64 8.3.0-
> 6+21.3~deb10u1 - any idea how to fix it?
> 
> And on -lcrypto, it didn't use to work before the refactor - but now it
> does. I have no clue what was happening. Will change it back in v3.
> 

Apparently the definition of _GNU_SOURCE in lib/utils.c changes the printf
implementation that is used from Microsoft's to MinGW's, but the use of
__attribute__((format(printf))) is generating warnings assuming that Microsoft's
printf implementation is used.  Always defining _GNU_SOURCE and then using
__attribute__((format(gnu_printf))) might be the way to go:

diff --git a/Makefile b/Makefile
index cc62818..44aee92 100644
--- a/Makefile
+++ b/Makefile
@@ -52,7 +52,7 @@ override CFLAGS := -Wall -Wundef				\
 	$(call cc-option,-Wvla)					\
 	$(CFLAGS)
 
-override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 $(CPPFLAGS)
+override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(CPPFLAGS)
 
 ifneq ($(V),1)
 QUIET_CC        = @echo '  CC      ' $@;
diff --git a/common/win32_defs.h b/common/win32_defs.h
index e13938a..4edb17f 100644
--- a/common/win32_defs.h
+++ b/common/win32_defs.h
@@ -37,6 +37,11 @@
 #  define __cold
 #endif
 
+#ifndef __printf
+#  define __printf(fmt_idx, vargs_idx) \
+	__attribute__((format(gnu_printf, fmt_idx, vargs_idx)))
+#endif
+
 typedef __signed__ char __s8;
 typedef unsigned char __u8;
 typedef __signed__ short __s16;
diff --git a/lib/utils.c b/lib/utils.c
index 8bb4413..55a4045 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -9,8 +9,6 @@
  * https://opensource.org/licenses/MIT.
  */
 
-#define _GNU_SOURCE /* for asprintf() and strerror_r() */
-
 #include "lib_private.h"
 
 #include <stdio.h>

^ permalink raw reply related	[flat|nested] 34+ messages in thread

* Re: [fsverity-utils PATCH v2 2/2] Allow to build and run sign/digest on Windows
  2020-12-17 19:05       ` Eric Biggers
@ 2020-12-17 19:12         ` Luca Boccassi
  2020-12-17 19:20           ` Eric Biggers
  0 siblings, 1 reply; 34+ messages in thread
From: Luca Boccassi @ 2020-12-17 19:12 UTC (permalink / raw)
  To: Eric Biggers; +Cc: linux-fscrypt

[-- Attachment #1: Type: text/plain, Size: 8201 bytes --]

On Thu, 2020-12-17 at 11:05 -0800, Eric Biggers wrote:
> On Thu, Dec 17, 2020 at 06:44:38PM +0000, Luca Boccassi wrote:
> > On Thu, 2020-12-17 at 10:32 -0800, Eric Biggers wrote:
> > > On Thu, Dec 17, 2020 at 02:47:49PM +0000, luca.boccassi@gmail.com wrote:
> > > > From: Luca Boccassi <luca.boccassi@microsoft.com>
> > > > 
> > > > Add some minimal compat type defs, and stub out the enable/measure
> > > > sources. Also add a way to handle the fact that mingw adds a
> > > > .exe extension automatically in the Makefile install rules, and
> > > > that there is not pkg-config and the libcrypto linker flag is
> > > > different.
> > > > 
> > > > Signed-off-by: Luca Boccassi <luca.boccassi@microsoft.com>
> > > > ---
> > > > v2: rework the stubbing out to detect mingw in the Makefile and remove
> > > >     sources from compilation, instead of ifdefs.
> > > >     add a new common/win32_defs.h for the compat definitions.
> > > >     define strerror_r using strerror_s.
> > > > 
> > > >     To compile with mingw:
> > > >       make CC=x86_64-w64-mingw32-gcc-8.3-win32
> > > >     note that the openssl headers and a win32 libcrypto.dll need
> > > >     to be available in the default search paths, and otherwise have
> > > >     to be specified as expected via CPPFLAGS/LDFLAGS
> > > > 
> > > 
> > > I got some warnings and an error when compiling:
> > > 
> > > $ make CC=x86_64-w64-mingw32-gcc
> > >   CC       lib/compute_digest.o
> > >   CC       lib/hash_algs.o
> > >   CC       lib/sign_digest.o
> > >   CC       lib/utils.o
> > > lib/utils.c: In function ‘xmalloc’:
> > > lib/utils.c:25:25: warning: unknown conversion type character ‘l’ in format [-Wformat=]
> > >    25 |   libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
> > >       |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > > In file included from lib/../common/win32_defs.h:24,
> > >                  from lib/../common/common_defs.h:18,
> > >                  from lib/lib_private.h:15,
> > >                  from lib/utils.c:14:
> > > /usr/x86_64-w64-mingw32/include/inttypes.h:36:18: note: format string is defined here
> > >    36 | #define PRIu64 "llu"
> > >       |                  ^
> > > lib/utils.c:25:25: warning: too many arguments for format [-Wformat-extra-args]
> > >    25 |   libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
> > >       |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > >   AR       libfsverity.a
> > >   CC       lib/compute_digest.shlib.o
> > >   CC       lib/hash_algs.shlib.o
> > >   CC       lib/sign_digest.shlib.o
> > >   CC       lib/utils.shlib.o
> > > lib/utils.c: In function ‘xmalloc’:
> > > lib/utils.c:25:25: warning: unknown conversion type character ‘l’ in format [-Wformat=]
> > >    25 |   libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
> > >       |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > > In file included from lib/../common/win32_defs.h:24,
> > >                  from lib/../common/common_defs.h:18,
> > >                  from lib/lib_private.h:15,
> > >                  from lib/utils.c:14:
> > > /usr/x86_64-w64-mingw32/include/inttypes.h:36:18: note: format string is defined here
> > >    36 | #define PRIu64 "llu"
> > >       |                  ^
> > > lib/utils.c:25:25: warning: too many arguments for format [-Wformat-extra-args]
> > >    25 |   libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
> > >       |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > >   CCLD     libfsverity.so.0
> > > /usr/lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -l:libcrypto.dll
> > > collect2: error: ld returned 1 exit status
> > > make: *** [Makefile:137: libfsverity.so.0] Error 1
> > > 
> > > 
> > > 
> > > This is on Arch Linux with mingw-w64-gcc and mingw-w64-openssl installed.
> > > 
> > > So there's something wrong with the SIZET_PF format string, and also
> > > -l:libcrypto.dll isn't correct; it should be just -lcrypto like it is on Linux.
> > > (MinGW knows to look for a .dll file.)
> > > 
> > > - Eric
> > 
> > Mmh I don't get any warnings on Debian - gcc-mingw-w64-x86-64 8.3.0-
> > 6+21.3~deb10u1 - any idea how to fix it?
> > 
> > And on -lcrypto, it didn't use to work before the refactor - but now it
> > does. I have no clue what was happening. Will change it back in v3.
> > 
> 
> Apparently the definition of _GNU_SOURCE in lib/utils.c changes the printf
> implementation that is used from Microsoft's to MinGW's, but the use of
> __attribute__((format(printf))) is generating warnings assuming that Microsoft's
> printf implementation is used.  Always defining _GNU_SOURCE and then using
> __attribute__((format(gnu_printf))) might be the way to go:
> 
> diff --git a/Makefile b/Makefile
> index cc62818..44aee92 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -52,7 +52,7 @@ override CFLAGS := -Wall -Wundef				\
>  	$(call cc-option,-Wvla)					\
>  	$(CFLAGS)
>  
> -override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 $(CPPFLAGS)
> +override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(CPPFLAGS)
>  
>  ifneq ($(V),1)
>  QUIET_CC        = @echo '  CC      ' $@;
> diff --git a/common/win32_defs.h b/common/win32_defs.h
> index e13938a..4edb17f 100644
> --- a/common/win32_defs.h
> +++ b/common/win32_defs.h
> @@ -37,6 +37,11 @@
>  #  define __cold
>  #endif
>  
> +#ifndef __printf
> +#  define __printf(fmt_idx, vargs_idx) \
> +	__attribute__((format(gnu_printf, fmt_idx, vargs_idx)))
> +#endif
> +
>  typedef __signed__ char __s8;
>  typedef unsigned char __u8;
>  typedef __signed__ short __s16;
> diff --git a/lib/utils.c b/lib/utils.c
> index 8bb4413..55a4045 100644
> --- a/lib/utils.c
> +++ b/lib/utils.c
> @@ -9,8 +9,6 @@
>   * https://opensource.org/licenses/MIT.
>   */
>  
> -#define _GNU_SOURCE /* for asprintf() and strerror_r() */
> -
>  #include "lib_private.h"
>  
>  #include <stdio.h>

I get the following warning with the mingw build now:

lib/utils.c: In function ‘xmalloc’:
lib/utils.c:23:25: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘size_t’ {aka ‘long long unsigned int’} [-Wformat=]
   libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           size);
           ~~~~           
In file included from lib/../common/win32_defs.h:24,
                 from lib/../common/common_defs.h:18,
                 from lib/lib_private.h:15,
                 from lib/utils.c:12:
/usr/share/mingw-w64/include/inttypes.h:94:20: note: format string is defined here
 #define PRIu64 "I64u"
  AR       libfsverity.a
  CC       lib/sign_digest.shlib.o
  CC       lib/compute_digest.shlib.o
  CC       lib/hash_algs.shlib.o
  CC       lib/utils.shlib.o
lib/utils.c: In function ‘xmalloc’:
lib/utils.c:23:25: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘size_t’ {aka ‘long long unsigned int’} [-Wformat=]
   libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           size);
           ~~~~           
In file included from lib/../common/win32_defs.h:24,
                 from lib/../common/common_defs.h:18,
                 from lib/lib_private.h:15,
                 from lib/utils.c:12:
/usr/share/mingw-w64/include/inttypes.h:94:20: note: format string is defined here
 #define PRIu64 "I64u"


But, honestly, it seems harmless to me. If someone on Windows is trying
to get a digest and don't have memory to do it, they'll have bigger
problems to worry about than knowing how much it was requested. I'll
send a v3 with your suggested changes. As far as I can read online,
handling %zu in a cross compatible way is like the number one
annoyance.

-- 
Kind regards,
Luca Boccassi

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [fsverity-utils PATCH v3 1/2] Remove unneeded includes
  2020-12-17 14:47 [fsverity-utils PATCH v2 1/2] Remove unneeded includes luca.boccassi
  2020-12-17 14:47 ` [fsverity-utils PATCH v2 2/2] Allow to build and run sign/digest on Windows luca.boccassi
@ 2020-12-17 19:16 ` luca.boccassi
  2020-12-17 19:16   ` [fsverity-utils PATCH v3 2/2] Allow to build and run sign/digest on Windows luca.boccassi
  2020-12-17 19:25 ` [fsverity-utils PATCH v4 1/2] Remove unneeded includes luca.boccassi
  2 siblings, 1 reply; 34+ messages in thread
From: luca.boccassi @ 2020-12-17 19:16 UTC (permalink / raw)
  To: linux-fscrypt; +Cc: ebiggers

From: Luca Boccassi <luca.boccassi@microsoft.com>

Signed-off-by: Luca Boccassi <luca.boccassi@microsoft.com>
---
v2: do not remove includes from fsverity_uapi.h, actually needed

 programs/cmd_enable.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/programs/cmd_enable.c b/programs/cmd_enable.c
index fdf26c7..14c3c17 100644
--- a/programs/cmd_enable.c
+++ b/programs/cmd_enable.c
@@ -14,7 +14,6 @@
 #include <fcntl.h>
 #include <getopt.h>
 #include <limits.h>
-#include <sys/ioctl.h>
 
 static bool read_signature(const char *filename, u8 **sig_ret,
 			   u32 *sig_size_ret)
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [fsverity-utils PATCH v3 2/2] Allow to build and run sign/digest on Windows
  2020-12-17 19:16 ` [fsverity-utils PATCH v3 1/2] Remove unneeded includes luca.boccassi
@ 2020-12-17 19:16   ` luca.boccassi
  0 siblings, 0 replies; 34+ messages in thread
From: luca.boccassi @ 2020-12-17 19:16 UTC (permalink / raw)
  To: linux-fscrypt; +Cc: ebiggers

From: Luca Boccassi <luca.boccassi@microsoft.com>

Add some minimal compat type defs, and stub out the enable/measure
sources. Also add a way to handle the fact that mingw adds a
.exe extension automatically in the Makefile install rules, and
that there is not pkg-config and the libcrypto linker flag is
different.

Signed-off-by: Luca Boccassi <luca.boccassi@microsoft.com>
---
v2: rework the stubbing out to detect mingw in the Makefile and remove
    sources from compilation, instead of ifdefs.
    add a new common/win32_defs.h for the compat definitions.
    define strerror_r using strerror_s.

    To compile with mingw:
      make CC=x86_64-w64-mingw32-gcc-8.3-win32
    note that the openssl headers and a win32 libcrypto.dll need
    to be available in the default search paths, and otherwise have
    to be specified as expected via CPPFLAGS/LDFLAGS
v3: apply suggestion to remove -D_GNU_SOURCE from the header and define
    it as a CPPFLAGS, and to add  a definition of __printf for _WIN32
    to fix compiler warnings
    removed override of -lcrypto, not needed

 Makefile               | 34 ++++++++++++++++------
 common/common_defs.h   |  2 ++
 common/fsverity_uapi.h |  2 ++
 common/win32_defs.h    | 66 ++++++++++++++++++++++++++++++++++++++++++
 lib/utils.c            | 13 +++++++--
 programs/fsverity.c    |  2 ++
 programs/utils.c       |  2 +-
 7 files changed, 108 insertions(+), 13 deletions(-)
 create mode 100644 common/win32_defs.h

diff --git a/Makefile b/Makefile
index bfe83c4..a5aa900 100644
--- a/Makefile
+++ b/Makefile
@@ -35,6 +35,11 @@
 cc-option = $(shell if $(CC) $(1) -c -x c /dev/null -o /dev/null > /dev/null 2>&1; \
 	      then echo $(1); fi)
 
+# Support building with MinGW for minimal Windows fsverity.exe
+ifneq ($(findstring -mingw,$(shell $(CC) -dumpmachine 2>/dev/null)),)
+MINGW = 1
+endif
+
 CFLAGS ?= -O2
 
 override CFLAGS := -Wall -Wundef				\
@@ -47,7 +52,7 @@ override CFLAGS := -Wall -Wundef				\
 	$(call cc-option,-Wvla)					\
 	$(CFLAGS)
 
-override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 $(CPPFLAGS)
+override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(CPPFLAGS)
 
 ifneq ($(V),1)
 QUIET_CC        = @echo '  CC      ' $@;
@@ -62,7 +67,12 @@ BINDIR          ?= $(PREFIX)/bin
 INCDIR          ?= $(PREFIX)/include
 LIBDIR          ?= $(PREFIX)/lib
 DESTDIR         ?=
+ifneq ($(MINGW),1)
 PKGCONF         ?= pkg-config
+else
+PKGCONF         := false
+EXEEXT          := .exe
+endif
 
 # Rebuild if a user-specified setting that affects the build changed.
 .build-config: FORCE
@@ -87,9 +97,9 @@ CFLAGS          += $(shell "$(PKGCONF)" libcrypto --cflags 2>/dev/null || echo)
 # If we are dynamically linking, when running tests we need to override
 # LD_LIBRARY_PATH as no RPATH is set
 ifdef USE_SHARED_LIB
-RUN_FSVERITY    = LD_LIBRARY_PATH=./ ./fsverity
+RUN_FSVERITY    = LD_LIBRARY_PATH=./ ./fsverity$(EXEEXT)
 else
-RUN_FSVERITY    = ./fsverity
+RUN_FSVERITY    = ./fsverity$(EXEEXT)
 endif
 
 ##############################################################################
@@ -99,6 +109,9 @@ endif
 SOVERSION       := 0
 LIB_CFLAGS      := $(CFLAGS) -fvisibility=hidden
 LIB_SRC         := $(wildcard lib/*.c)
+ifeq ($(MINGW),1)
+LIB_SRC         := $(filter-out lib/enable.c,${LIB_SRC})
+endif
 LIB_HEADERS     := $(wildcard lib/*.h) $(COMMON_HEADERS)
 STATIC_LIB_OBJ  := $(LIB_SRC:.c=.o)
 SHARED_LIB_OBJ  := $(LIB_SRC:.c=.shlib.o)
@@ -141,10 +154,13 @@ PROG_COMMON_SRC   := programs/utils.c
 PROG_COMMON_OBJ   := $(PROG_COMMON_SRC:.c=.o)
 FSVERITY_PROG_OBJ := $(PROG_COMMON_OBJ)		\
 		     programs/cmd_digest.o	\
-		     programs/cmd_enable.o	\
-		     programs/cmd_measure.o	\
 		     programs/cmd_sign.o	\
 		     programs/fsverity.o
+ifneq ($(MINGW),1)
+FSVERITY_PROG_OBJ += \
+		     programs/cmd_enable.o	\
+		     programs/cmd_measure.o
+endif
 TEST_PROG_SRC     := $(wildcard programs/test_*.c)
 TEST_PROGRAMS     := $(TEST_PROG_SRC:programs/%.c=%)
 
@@ -186,7 +202,7 @@ test_programs:$(TEST_PROGRAMS)
 # want to run the full tests.
 check:fsverity test_programs
 	for prog in $(TEST_PROGRAMS); do \
-		$(TEST_WRAPPER_PROG) ./$$prog || exit 1; \
+		$(TEST_WRAPPER_PROG) ./$$prog$(EXEEXT) || exit 1; \
 	done
 	$(RUN_FSVERITY) --help > /dev/null
 	$(RUN_FSVERITY) --version > /dev/null
@@ -202,7 +218,7 @@ check:fsverity test_programs
 
 install:all
 	install -d $(DESTDIR)$(LIBDIR)/pkgconfig $(DESTDIR)$(INCDIR) $(DESTDIR)$(BINDIR)
-	install -m755 fsverity $(DESTDIR)$(BINDIR)
+	install -m755 fsverity$(EXEEXT) $(DESTDIR)$(BINDIR)
 	install -m644 libfsverity.a $(DESTDIR)$(LIBDIR)
 	install -m755 libfsverity.so.$(SOVERSION) $(DESTDIR)$(LIBDIR)
 	ln -sf libfsverity.so.$(SOVERSION) $(DESTDIR)$(LIBDIR)/libfsverity.so
@@ -215,7 +231,7 @@ install:all
 	chmod 644 $(DESTDIR)$(LIBDIR)/pkgconfig/libfsverity.pc
 
 uninstall:
-	rm -f $(DESTDIR)$(BINDIR)/fsverity
+	rm -f $(DESTDIR)$(BINDIR)/fsverity$(EXEEXT)
 	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.a
 	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.so.$(SOVERSION)
 	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.so
@@ -232,4 +248,4 @@ help:
 
 clean:
 	rm -f $(DEFAULT_TARGETS) $(TEST_PROGRAMS) \
-		lib/*.o programs/*.o .build-config fsverity.sig
+		fsverity$(EXEEXT) lib/*.o programs/*.o .build-config fsverity.sig
diff --git a/common/common_defs.h b/common/common_defs.h
index 279385a..3ae5561 100644
--- a/common/common_defs.h
+++ b/common/common_defs.h
@@ -15,6 +15,8 @@
 #include <stddef.h>
 #include <stdint.h>
 
+#include "win32_defs.h"
+
 typedef uint8_t u8;
 typedef uint16_t u16;
 typedef uint32_t u32;
diff --git a/common/fsverity_uapi.h b/common/fsverity_uapi.h
index 33f4415..be1d3f6 100644
--- a/common/fsverity_uapi.h
+++ b/common/fsverity_uapi.h
@@ -10,8 +10,10 @@
 #ifndef _UAPI_LINUX_FSVERITY_H
 #define _UAPI_LINUX_FSVERITY_H
 
+#ifndef _WIN32
 #include <linux/ioctl.h>
 #include <linux/types.h>
+#endif /* _WIN32 */
 
 #define FS_VERITY_HASH_ALG_SHA256	1
 #define FS_VERITY_HASH_ALG_SHA512	2
diff --git a/common/win32_defs.h b/common/win32_defs.h
new file mode 100644
index 0000000..2fd4887
--- /dev/null
+++ b/common/win32_defs.h
@@ -0,0 +1,66 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * WIN32 compat definitions for libfsverity and the 'fsverity' program
+ *
+ * Copyright 2020 Microsoft
+ *
+ * Use of this source code is governed by an MIT-style
+ * license that can be found in the LICENSE file or at
+ * https://opensource.org/licenses/MIT.
+ */
+#ifndef COMMON_WIN32_DEFS_H
+#define COMMON_WIN32_DEFS_H
+
+/* Some minimal definitions to allow the digest/sign commands to run under Windows */
+
+/* All file reads we do need this flag on _WIN32 */
+#ifndef O_BINARY
+#  define O_BINARY 0
+#endif
+
+#ifdef _WIN32
+
+#include <stdint.h>
+#include <inttypes.h>
+
+#ifdef _WIN64
+#  define SIZET_PF PRIu64
+#else
+#  define SIZET_PF PRIu32
+#endif
+
+#ifndef ENOPKG
+#   define ENOPKG 65
+#endif
+
+#ifndef __cold
+#  define __cold
+#endif
+
+#ifndef __printf
+#  define __printf(fmt_idx, vargs_idx) \
+       __attribute__((format(gnu_printf, fmt_idx, vargs_idx)))
+#endif
+
+typedef __signed__ char __s8;
+typedef unsigned char __u8;
+typedef __signed__ short __s16;
+typedef unsigned short __u16;
+typedef __signed__ int __s32;
+typedef unsigned int __u32;
+typedef __signed__ long long  __s64;
+typedef unsigned long long  __u64;
+typedef __u16 __le16;
+typedef __u16 __be16;
+typedef __u32 __le32;
+typedef __u32 __be32;
+typedef __u64 __le64;
+typedef __u64 __be64;
+
+#else
+
+#define SIZET_PF "zu"
+
+#endif /* _WIN32 */
+
+#endif /* COMMON_WIN32_DEFS_H */
diff --git a/lib/utils.c b/lib/utils.c
index 8b5d6cb..55a4045 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -9,8 +9,6 @@
  * https://opensource.org/licenses/MIT.
  */
 
-#define _GNU_SOURCE /* for asprintf() and strerror_r() */
-
 #include "lib_private.h"
 
 #include <stdio.h>
@@ -22,7 +20,7 @@ static void *xmalloc(size_t size)
 	void *p = malloc(size);
 
 	if (!p)
-		libfsverity_error_msg("out of memory (tried to allocate %zu bytes)",
+		libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
 				      size);
 	return p;
 }
@@ -53,6 +51,15 @@ libfsverity_set_error_callback(void (*cb)(const char *msg))
 	libfsverity_error_cb = cb;
 }
 
+#ifdef _WIN32
+static char *strerror_r(int errnum, char *buf, size_t buflen)
+{
+	strerror_s(buf, buflen, errnum);
+
+	return buf;
+}
+#endif
+
 void libfsverity_do_error_msg(const char *format, va_list va, int err)
 {
 	int saved_errno = errno;
diff --git a/programs/fsverity.c b/programs/fsverity.c
index 5d5fbe2..f68e034 100644
--- a/programs/fsverity.c
+++ b/programs/fsverity.c
@@ -28,6 +28,7 @@ static const struct fsverity_command {
 "    fsverity digest FILE...\n"
 "               [--hash-alg=HASH_ALG] [--block-size=BLOCK_SIZE] [--salt=SALT]\n"
 "               [--compact] [--for-builtin-sig]\n"
+#ifndef _WIN32
 	}, {
 		.name = "enable",
 		.func = fsverity_cmd_enable,
@@ -43,6 +44,7 @@ static const struct fsverity_command {
 "Display the fs-verity digest of the given verity file(s)",
 		.usage_str =
 "    fsverity measure FILE...\n"
+#endif /* _WIN32 */
 	}, {
 		.name = "sign",
 		.func = fsverity_cmd_sign,
diff --git a/programs/utils.c b/programs/utils.c
index facccda..ce19b57 100644
--- a/programs/utils.c
+++ b/programs/utils.c
@@ -102,7 +102,7 @@ void install_libfsverity_error_handler(void)
 
 bool open_file(struct filedes *file, const char *filename, int flags, int mode)
 {
-	file->fd = open(filename, flags, mode);
+	file->fd = open(filename, flags | O_BINARY, mode);
 	if (file->fd < 0) {
 		error_msg_errno("can't open '%s' for %s", filename,
 				(flags & O_ACCMODE) == O_RDONLY ? "reading" :
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* Re: [fsverity-utils PATCH v2 2/2] Allow to build and run sign/digest on Windows
  2020-12-17 19:12         ` Luca Boccassi
@ 2020-12-17 19:20           ` Eric Biggers
  2020-12-17 19:26             ` Luca Boccassi
  0 siblings, 1 reply; 34+ messages in thread
From: Eric Biggers @ 2020-12-17 19:20 UTC (permalink / raw)
  To: Luca Boccassi; +Cc: linux-fscrypt

On Thu, Dec 17, 2020 at 07:12:06PM +0000, Luca Boccassi wrote:
> 
> I get the following warning with the mingw build now:
> 
> lib/utils.c: In function ‘xmalloc’:
> lib/utils.c:23:25: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘size_t’ {aka ‘long long unsigned int’} [-Wformat=]
>    libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
>                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>            size);
>            ~~~~           
> In file included from lib/../common/win32_defs.h:24,
>                  from lib/../common/common_defs.h:18,
>                  from lib/lib_private.h:15,
>                  from lib/utils.c:12:
> /usr/share/mingw-w64/include/inttypes.h:94:20: note: format string is defined here
>  #define PRIu64 "I64u"
>   AR       libfsverity.a
>   CC       lib/sign_digest.shlib.o
>   CC       lib/compute_digest.shlib.o
>   CC       lib/hash_algs.shlib.o
>   CC       lib/utils.shlib.o
> lib/utils.c: In function ‘xmalloc’:
> lib/utils.c:23:25: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘size_t’ {aka ‘long long unsigned int’} [-Wformat=]
>    libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
>                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>            size);
>            ~~~~           
> In file included from lib/../common/win32_defs.h:24,
>                  from lib/../common/common_defs.h:18,
>                  from lib/lib_private.h:15,
>                  from lib/utils.c:12:
> /usr/share/mingw-w64/include/inttypes.h:94:20: note: format string is defined here
>  #define PRIu64 "I64u"
> 
> 
> But, honestly, it seems harmless to me. If someone on Windows is trying
> to get a digest and don't have memory to do it, they'll have bigger
> problems to worry about than knowing how much it was requested. I'll
> send a v3 with your suggested changes. As far as I can read online,
> handling %zu in a cross compatible way is like the number one
> annoyance.
> 

It needs to compile without warnings, otherwise new warnings won't be noticed.

I think that if the MinGW printf is used (by defining _GNU_SOURCE), then %zu
would just work as-is.  That's what I do in another project.  Try:

diff --git a/Makefile b/Makefile
index cc62818..44aee92 100644
--- a/Makefile
+++ b/Makefile
@@ -52,7 +52,7 @@ override CFLAGS := -Wall -Wundef				\
 	$(call cc-option,-Wvla)					\
 	$(CFLAGS)
 
-override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 $(CPPFLAGS)
+override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(CPPFLAGS)
 
 ifneq ($(V),1)
 QUIET_CC        = @echo '  CC      ' $@;
diff --git a/common/win32_defs.h b/common/win32_defs.h
index e13938a..3b0d908 100644
--- a/common/win32_defs.h
+++ b/common/win32_defs.h
@@ -23,12 +23,6 @@
 #include <stdint.h>
 #include <inttypes.h>
 
-#ifdef _WIN64
-#  define SIZET_PF PRIu64
-#else
-#  define SIZET_PF PRIu32
-#endif
-
 #ifndef ENOPKG
 #   define ENOPKG 65
 #endif
@@ -37,6 +31,11 @@
 #  define __cold
 #endif
 
+#ifndef __printf
+#  define __printf(fmt_idx, vargs_idx) \
+	__attribute__((format(gnu_printf, fmt_idx, vargs_idx)))
+#endif
+
 typedef __signed__ char __s8;
 typedef unsigned char __u8;
 typedef __signed__ short __s16;
@@ -52,10 +51,6 @@ typedef __u32 __be32;
 typedef __u64 __le64;
 typedef __u64 __be64;
 
-#else
-
-#define SIZET_PF "zu"
-
 #endif /* _WIN32 */
 
 #endif /* COMMON_WIN32_DEFS_H */
diff --git a/lib/utils.c b/lib/utils.c
index 8bb4413..036dd60 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -9,8 +9,6 @@
  * https://opensource.org/licenses/MIT.
  */
 
-#define _GNU_SOURCE /* for asprintf() and strerror_r() */
-
 #include "lib_private.h"
 
 #include <stdio.h>
@@ -22,7 +20,7 @@ static void *xmalloc(size_t size)
 	void *p = malloc(size);
 
 	if (!p)
-		libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
+		libfsverity_error_msg("out of memory (tried to allocate %zu bytes)",
 				      size);
 	return p;
 }

- Eric

^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [fsverity-utils PATCH v4 1/2] Remove unneeded includes
  2020-12-17 14:47 [fsverity-utils PATCH v2 1/2] Remove unneeded includes luca.boccassi
  2020-12-17 14:47 ` [fsverity-utils PATCH v2 2/2] Allow to build and run sign/digest on Windows luca.boccassi
  2020-12-17 19:16 ` [fsverity-utils PATCH v3 1/2] Remove unneeded includes luca.boccassi
@ 2020-12-17 19:25 ` luca.boccassi
  2020-12-17 19:25   ` [fsverity-utils PATCH v4 2/2] Allow to build and run sign/digest on Windows luca.boccassi
  2020-12-21 21:32   ` [fsverity-utils PATCH v4 1/2] Remove unneeded includes Eric Biggers
  2 siblings, 2 replies; 34+ messages in thread
From: luca.boccassi @ 2020-12-17 19:25 UTC (permalink / raw)
  To: linux-fscrypt; +Cc: ebiggers

From: Luca Boccassi <luca.boccassi@microsoft.com>

Signed-off-by: Luca Boccassi <luca.boccassi@microsoft.com>
---
v2: do not remove includes from fsverity_uapi.h, actually needed

 programs/cmd_enable.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/programs/cmd_enable.c b/programs/cmd_enable.c
index fdf26c7..14c3c17 100644
--- a/programs/cmd_enable.c
+++ b/programs/cmd_enable.c
@@ -14,7 +14,6 @@
 #include <fcntl.h>
 #include <getopt.h>
 #include <limits.h>
-#include <sys/ioctl.h>
 
 static bool read_signature(const char *filename, u8 **sig_ret,
 			   u32 *sig_size_ret)
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [fsverity-utils PATCH v4 2/2] Allow to build and run sign/digest on Windows
  2020-12-17 19:25 ` [fsverity-utils PATCH v4 1/2] Remove unneeded includes luca.boccassi
@ 2020-12-17 19:25   ` luca.boccassi
  2020-12-21 21:40     ` Eric Biggers
  2020-12-21 22:19     ` [PATCH v5] " Luca Boccassi
  2020-12-21 21:32   ` [fsverity-utils PATCH v4 1/2] Remove unneeded includes Eric Biggers
  1 sibling, 2 replies; 34+ messages in thread
From: luca.boccassi @ 2020-12-17 19:25 UTC (permalink / raw)
  To: linux-fscrypt; +Cc: ebiggers

From: Luca Boccassi <luca.boccassi@microsoft.com>

Add some minimal compat type defs, and stub out the enable/measure
sources. Also add a way to handle the fact that mingw adds a
.exe extension automatically in the Makefile install rules, and
that there is not pkg-config and the libcrypto linker flag is
different.

Signed-off-by: Luca Boccassi <luca.boccassi@microsoft.com>
---
v2: rework the stubbing out to detect mingw in the Makefile and remove
    sources from compilation, instead of ifdefs.
    add a new common/win32_defs.h for the compat definitions.
    define strerror_r using strerror_s.

    To compile with mingw:
      make CC=x86_64-w64-mingw32-gcc-8.3-win32
    note that the openssl headers and a win32 libcrypto.dll need
    to be available in the default search paths, and otherwise have
    to be specified as expected via CPPFLAGS/LDFLAGS
v3: apply suggestion to remove -D_GNU_SOURCE from the header and define
    it as a CPPFLAGS, and to add  a definition of __printf for _WIN32
    to fix compiler warnings
    removed override of -lcrypto, not needed
v4: apply suggestion to remove overrides of %zu, as it now "just works".
    no more compilation warnings.

 Makefile               | 34 ++++++++++++++++++-------
 common/common_defs.h   |  2 ++
 common/fsverity_uapi.h |  2 ++
 common/win32_defs.h    | 57 ++++++++++++++++++++++++++++++++++++++++++
 lib/utils.c            | 11 ++++++--
 programs/fsverity.c    |  2 ++
 programs/utils.c       |  2 +-
 7 files changed, 98 insertions(+), 12 deletions(-)
 create mode 100644 common/win32_defs.h

diff --git a/Makefile b/Makefile
index bfe83c4..a5aa900 100644
--- a/Makefile
+++ b/Makefile
@@ -35,6 +35,11 @@
 cc-option = $(shell if $(CC) $(1) -c -x c /dev/null -o /dev/null > /dev/null 2>&1; \
 	      then echo $(1); fi)
 
+# Support building with MinGW for minimal Windows fsverity.exe
+ifneq ($(findstring -mingw,$(shell $(CC) -dumpmachine 2>/dev/null)),)
+MINGW = 1
+endif
+
 CFLAGS ?= -O2
 
 override CFLAGS := -Wall -Wundef				\
@@ -47,7 +52,7 @@ override CFLAGS := -Wall -Wundef				\
 	$(call cc-option,-Wvla)					\
 	$(CFLAGS)
 
-override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 $(CPPFLAGS)
+override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(CPPFLAGS)
 
 ifneq ($(V),1)
 QUIET_CC        = @echo '  CC      ' $@;
@@ -62,7 +67,12 @@ BINDIR          ?= $(PREFIX)/bin
 INCDIR          ?= $(PREFIX)/include
 LIBDIR          ?= $(PREFIX)/lib
 DESTDIR         ?=
+ifneq ($(MINGW),1)
 PKGCONF         ?= pkg-config
+else
+PKGCONF         := false
+EXEEXT          := .exe
+endif
 
 # Rebuild if a user-specified setting that affects the build changed.
 .build-config: FORCE
@@ -87,9 +97,9 @@ CFLAGS          += $(shell "$(PKGCONF)" libcrypto --cflags 2>/dev/null || echo)
 # If we are dynamically linking, when running tests we need to override
 # LD_LIBRARY_PATH as no RPATH is set
 ifdef USE_SHARED_LIB
-RUN_FSVERITY    = LD_LIBRARY_PATH=./ ./fsverity
+RUN_FSVERITY    = LD_LIBRARY_PATH=./ ./fsverity$(EXEEXT)
 else
-RUN_FSVERITY    = ./fsverity
+RUN_FSVERITY    = ./fsverity$(EXEEXT)
 endif
 
 ##############################################################################
@@ -99,6 +109,9 @@ endif
 SOVERSION       := 0
 LIB_CFLAGS      := $(CFLAGS) -fvisibility=hidden
 LIB_SRC         := $(wildcard lib/*.c)
+ifeq ($(MINGW),1)
+LIB_SRC         := $(filter-out lib/enable.c,${LIB_SRC})
+endif
 LIB_HEADERS     := $(wildcard lib/*.h) $(COMMON_HEADERS)
 STATIC_LIB_OBJ  := $(LIB_SRC:.c=.o)
 SHARED_LIB_OBJ  := $(LIB_SRC:.c=.shlib.o)
@@ -141,10 +154,13 @@ PROG_COMMON_SRC   := programs/utils.c
 PROG_COMMON_OBJ   := $(PROG_COMMON_SRC:.c=.o)
 FSVERITY_PROG_OBJ := $(PROG_COMMON_OBJ)		\
 		     programs/cmd_digest.o	\
-		     programs/cmd_enable.o	\
-		     programs/cmd_measure.o	\
 		     programs/cmd_sign.o	\
 		     programs/fsverity.o
+ifneq ($(MINGW),1)
+FSVERITY_PROG_OBJ += \
+		     programs/cmd_enable.o	\
+		     programs/cmd_measure.o
+endif
 TEST_PROG_SRC     := $(wildcard programs/test_*.c)
 TEST_PROGRAMS     := $(TEST_PROG_SRC:programs/%.c=%)
 
@@ -186,7 +202,7 @@ test_programs:$(TEST_PROGRAMS)
 # want to run the full tests.
 check:fsverity test_programs
 	for prog in $(TEST_PROGRAMS); do \
-		$(TEST_WRAPPER_PROG) ./$$prog || exit 1; \
+		$(TEST_WRAPPER_PROG) ./$$prog$(EXEEXT) || exit 1; \
 	done
 	$(RUN_FSVERITY) --help > /dev/null
 	$(RUN_FSVERITY) --version > /dev/null
@@ -202,7 +218,7 @@ check:fsverity test_programs
 
 install:all
 	install -d $(DESTDIR)$(LIBDIR)/pkgconfig $(DESTDIR)$(INCDIR) $(DESTDIR)$(BINDIR)
-	install -m755 fsverity $(DESTDIR)$(BINDIR)
+	install -m755 fsverity$(EXEEXT) $(DESTDIR)$(BINDIR)
 	install -m644 libfsverity.a $(DESTDIR)$(LIBDIR)
 	install -m755 libfsverity.so.$(SOVERSION) $(DESTDIR)$(LIBDIR)
 	ln -sf libfsverity.so.$(SOVERSION) $(DESTDIR)$(LIBDIR)/libfsverity.so
@@ -215,7 +231,7 @@ install:all
 	chmod 644 $(DESTDIR)$(LIBDIR)/pkgconfig/libfsverity.pc
 
 uninstall:
-	rm -f $(DESTDIR)$(BINDIR)/fsverity
+	rm -f $(DESTDIR)$(BINDIR)/fsverity$(EXEEXT)
 	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.a
 	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.so.$(SOVERSION)
 	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.so
@@ -232,4 +248,4 @@ help:
 
 clean:
 	rm -f $(DEFAULT_TARGETS) $(TEST_PROGRAMS) \
-		lib/*.o programs/*.o .build-config fsverity.sig
+		fsverity$(EXEEXT) lib/*.o programs/*.o .build-config fsverity.sig
diff --git a/common/common_defs.h b/common/common_defs.h
index 279385a..3ae5561 100644
--- a/common/common_defs.h
+++ b/common/common_defs.h
@@ -15,6 +15,8 @@
 #include <stddef.h>
 #include <stdint.h>
 
+#include "win32_defs.h"
+
 typedef uint8_t u8;
 typedef uint16_t u16;
 typedef uint32_t u32;
diff --git a/common/fsverity_uapi.h b/common/fsverity_uapi.h
index 33f4415..be1d3f6 100644
--- a/common/fsverity_uapi.h
+++ b/common/fsverity_uapi.h
@@ -10,8 +10,10 @@
 #ifndef _UAPI_LINUX_FSVERITY_H
 #define _UAPI_LINUX_FSVERITY_H
 
+#ifndef _WIN32
 #include <linux/ioctl.h>
 #include <linux/types.h>
+#endif /* _WIN32 */
 
 #define FS_VERITY_HASH_ALG_SHA256	1
 #define FS_VERITY_HASH_ALG_SHA512	2
diff --git a/common/win32_defs.h b/common/win32_defs.h
new file mode 100644
index 0000000..29ef9b2
--- /dev/null
+++ b/common/win32_defs.h
@@ -0,0 +1,57 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * WIN32 compat definitions for libfsverity and the 'fsverity' program
+ *
+ * Copyright 2020 Microsoft
+ *
+ * Use of this source code is governed by an MIT-style
+ * license that can be found in the LICENSE file or at
+ * https://opensource.org/licenses/MIT.
+ */
+#ifndef COMMON_WIN32_DEFS_H
+#define COMMON_WIN32_DEFS_H
+
+/* Some minimal definitions to allow the digest/sign commands to run under Windows */
+
+/* All file reads we do need this flag on _WIN32 */
+#ifndef O_BINARY
+#  define O_BINARY 0
+#endif
+
+#ifdef _WIN32
+
+#include <stdint.h>
+#include <inttypes.h>
+
+#ifndef ENOPKG
+#   define ENOPKG 65
+#endif
+
+#ifndef __cold
+#  define __cold
+#endif
+
+/* For %zu in printf() */
+#ifndef __printf
+#  define __printf(fmt_idx, vargs_idx) \
+       __attribute__((format(gnu_printf, fmt_idx, vargs_idx)))
+#endif
+
+typedef __signed__ char __s8;
+typedef unsigned char __u8;
+typedef __signed__ short __s16;
+typedef unsigned short __u16;
+typedef __signed__ int __s32;
+typedef unsigned int __u32;
+typedef __signed__ long long  __s64;
+typedef unsigned long long  __u64;
+typedef __u16 __le16;
+typedef __u16 __be16;
+typedef __u32 __le32;
+typedef __u32 __be32;
+typedef __u64 __le64;
+typedef __u64 __be64;
+
+#endif /* _WIN32 */
+
+#endif /* COMMON_WIN32_DEFS_H */
diff --git a/lib/utils.c b/lib/utils.c
index 8b5d6cb..036dd60 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -9,8 +9,6 @@
  * https://opensource.org/licenses/MIT.
  */
 
-#define _GNU_SOURCE /* for asprintf() and strerror_r() */
-
 #include "lib_private.h"
 
 #include <stdio.h>
@@ -53,6 +51,15 @@ libfsverity_set_error_callback(void (*cb)(const char *msg))
 	libfsverity_error_cb = cb;
 }
 
+#ifdef _WIN32
+static char *strerror_r(int errnum, char *buf, size_t buflen)
+{
+	strerror_s(buf, buflen, errnum);
+
+	return buf;
+}
+#endif
+
 void libfsverity_do_error_msg(const char *format, va_list va, int err)
 {
 	int saved_errno = errno;
diff --git a/programs/fsverity.c b/programs/fsverity.c
index 5d5fbe2..f68e034 100644
--- a/programs/fsverity.c
+++ b/programs/fsverity.c
@@ -28,6 +28,7 @@ static const struct fsverity_command {
 "    fsverity digest FILE...\n"
 "               [--hash-alg=HASH_ALG] [--block-size=BLOCK_SIZE] [--salt=SALT]\n"
 "               [--compact] [--for-builtin-sig]\n"
+#ifndef _WIN32
 	}, {
 		.name = "enable",
 		.func = fsverity_cmd_enable,
@@ -43,6 +44,7 @@ static const struct fsverity_command {
 "Display the fs-verity digest of the given verity file(s)",
 		.usage_str =
 "    fsverity measure FILE...\n"
+#endif /* _WIN32 */
 	}, {
 		.name = "sign",
 		.func = fsverity_cmd_sign,
diff --git a/programs/utils.c b/programs/utils.c
index facccda..ce19b57 100644
--- a/programs/utils.c
+++ b/programs/utils.c
@@ -102,7 +102,7 @@ void install_libfsverity_error_handler(void)
 
 bool open_file(struct filedes *file, const char *filename, int flags, int mode)
 {
-	file->fd = open(filename, flags, mode);
+	file->fd = open(filename, flags | O_BINARY, mode);
 	if (file->fd < 0) {
 		error_msg_errno("can't open '%s' for %s", filename,
 				(flags & O_ACCMODE) == O_RDONLY ? "reading" :
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* Re: [fsverity-utils PATCH v2 2/2] Allow to build and run sign/digest on Windows
  2020-12-17 19:20           ` Eric Biggers
@ 2020-12-17 19:26             ` Luca Boccassi
  0 siblings, 0 replies; 34+ messages in thread
From: Luca Boccassi @ 2020-12-17 19:26 UTC (permalink / raw)
  To: Eric Biggers; +Cc: linux-fscrypt

[-- Attachment #1: Type: text/plain, Size: 4589 bytes --]

On Thu, 2020-12-17 at 11:20 -0800, Eric Biggers wrote:
> On Thu, Dec 17, 2020 at 07:12:06PM +0000, Luca Boccassi wrote:
> > I get the following warning with the mingw build now:
> > 
> > lib/utils.c: In function ‘xmalloc’:
> > lib/utils.c:23:25: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘size_t’ {aka ‘long long unsigned int’} [-Wformat=]
> >    libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
> >                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >            size);
> >            ~~~~           
> > In file included from lib/../common/win32_defs.h:24,
> >                  from lib/../common/common_defs.h:18,
> >                  from lib/lib_private.h:15,
> >                  from lib/utils.c:12:
> > /usr/share/mingw-w64/include/inttypes.h:94:20: note: format string is defined here
> >  #define PRIu64 "I64u"
> >   AR       libfsverity.a
> >   CC       lib/sign_digest.shlib.o
> >   CC       lib/compute_digest.shlib.o
> >   CC       lib/hash_algs.shlib.o
> >   CC       lib/utils.shlib.o
> > lib/utils.c: In function ‘xmalloc’:
> > lib/utils.c:23:25: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘size_t’ {aka ‘long long unsigned int’} [-Wformat=]
> >    libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
> >                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >            size);
> >            ~~~~           
> > In file included from lib/../common/win32_defs.h:24,
> >                  from lib/../common/common_defs.h:18,
> >                  from lib/lib_private.h:15,
> >                  from lib/utils.c:12:
> > /usr/share/mingw-w64/include/inttypes.h:94:20: note: format string is defined here
> >  #define PRIu64 "I64u"
> > 
> > 
> > But, honestly, it seems harmless to me. If someone on Windows is trying
> > to get a digest and don't have memory to do it, they'll have bigger
> > problems to worry about than knowing how much it was requested. I'll
> > send a v3 with your suggested changes. As far as I can read online,
> > handling %zu in a cross compatible way is like the number one
> > annoyance.
> > 
> 
> It needs to compile without warnings, otherwise new warnings won't be noticed.
> 
> I think that if the MinGW printf is used (by defining _GNU_SOURCE), then %zu
> would just work as-is.  That's what I do in another project.  Try:
> 
> diff --git a/Makefile b/Makefile
> index cc62818..44aee92 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -52,7 +52,7 @@ override CFLAGS := -Wall -Wundef				\
>  	$(call cc-option,-Wvla)					\
>  	$(CFLAGS)
>  
> -override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 $(CPPFLAGS)
> +override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(CPPFLAGS)
>  
>  ifneq ($(V),1)
>  QUIET_CC        = @echo '  CC      ' $@;
> diff --git a/common/win32_defs.h b/common/win32_defs.h
> index e13938a..3b0d908 100644
> --- a/common/win32_defs.h
> +++ b/common/win32_defs.h
> @@ -23,12 +23,6 @@
>  #include <stdint.h>
>  #include <inttypes.h>
>  
> -#ifdef _WIN64
> -#  define SIZET_PF PRIu64
> -#else
> -#  define SIZET_PF PRIu32
> -#endif
> -
>  #ifndef ENOPKG
>  #   define ENOPKG 65
>  #endif
> @@ -37,6 +31,11 @@
>  #  define __cold
>  #endif
>  
> +#ifndef __printf
> +#  define __printf(fmt_idx, vargs_idx) \
> +	__attribute__((format(gnu_printf, fmt_idx, vargs_idx)))
> +#endif
> +
>  typedef __signed__ char __s8;
>  typedef unsigned char __u8;
>  typedef __signed__ short __s16;
> @@ -52,10 +51,6 @@ typedef __u32 __be32;
>  typedef __u64 __le64;
>  typedef __u64 __be64;
>  
> -#else
> -
> -#define SIZET_PF "zu"
> -
>  #endif /* _WIN32 */
>  
>  #endif /* COMMON_WIN32_DEFS_H */
> diff --git a/lib/utils.c b/lib/utils.c
> index 8bb4413..036dd60 100644
> --- a/lib/utils.c
> +++ b/lib/utils.c
> @@ -9,8 +9,6 @@
>   * https://opensource.org/licenses/MIT.
>   */
>  
> -#define _GNU_SOURCE /* for asprintf() and strerror_r() */
> -
>  #include "lib_private.h"
>  
>  #include <stdio.h>
> @@ -22,7 +20,7 @@ static void *xmalloc(size_t size)
>  	void *p = malloc(size);
>  
>  	if (!p)
> -		libfsverity_error_msg("out of memory (tried to allocate %" SIZET_PF " bytes)",
> +		libfsverity_error_msg("out of memory (tried to allocate %zu bytes)",
>  				      size);
>  	return p;
>  }

That works, no more warnings, nice! Thank you! Sent v4.

-- 
Kind regards,
Luca Boccassi

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [fsverity-utils PATCH v4 1/2] Remove unneeded includes
  2020-12-17 19:25 ` [fsverity-utils PATCH v4 1/2] Remove unneeded includes luca.boccassi
  2020-12-17 19:25   ` [fsverity-utils PATCH v4 2/2] Allow to build and run sign/digest on Windows luca.boccassi
@ 2020-12-21 21:32   ` Eric Biggers
  1 sibling, 0 replies; 34+ messages in thread
From: Eric Biggers @ 2020-12-21 21:32 UTC (permalink / raw)
  To: luca.boccassi; +Cc: linux-fscrypt

On Thu, Dec 17, 2020 at 07:25:15PM +0000, luca.boccassi@gmail.com wrote:
> From: Luca Boccassi <luca.boccassi@microsoft.com>
> 
> Signed-off-by: Luca Boccassi <luca.boccassi@microsoft.com>
> ---
> v2: do not remove includes from fsverity_uapi.h, actually needed
> 
>  programs/cmd_enable.c | 1 -
>  1 file changed, 1 deletion(-)
> 
> diff --git a/programs/cmd_enable.c b/programs/cmd_enable.c
> index fdf26c7..14c3c17 100644
> --- a/programs/cmd_enable.c
> +++ b/programs/cmd_enable.c
> @@ -14,7 +14,6 @@
>  #include <fcntl.h>
>  #include <getopt.h>
>  #include <limits.h>
> -#include <sys/ioctl.h>
>  
>  static bool read_signature(const char *filename, u8 **sig_ret,
>  			   u32 *sig_size_ret)

Applied with s/includes/include/.

- Eric

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [fsverity-utils PATCH v4 2/2] Allow to build and run sign/digest on Windows
  2020-12-17 19:25   ` [fsverity-utils PATCH v4 2/2] Allow to build and run sign/digest on Windows luca.boccassi
@ 2020-12-21 21:40     ` Eric Biggers
  2020-12-21 22:23       ` Luca Boccassi
  2020-12-21 22:19     ` [PATCH v5] " Luca Boccassi
  1 sibling, 1 reply; 34+ messages in thread
From: Eric Biggers @ 2020-12-21 21:40 UTC (permalink / raw)
  To: luca.boccassi; +Cc: linux-fscrypt

On Thu, Dec 17, 2020 at 07:25:16PM +0000, luca.boccassi@gmail.com wrote:
> From: Luca Boccassi <luca.boccassi@microsoft.com>
> 
> Add some minimal compat type defs, and stub out the enable/measure
> sources. Also add a way to handle the fact that mingw adds a
> .exe extension automatically in the Makefile install rules, and
> that there is not pkg-config and the libcrypto linker flag is
> different.
> 
> Signed-off-by: Luca Boccassi <luca.boccassi@microsoft.com>

This commit message is outdated; can you update it?

> diff --git a/Makefile b/Makefile
> index bfe83c4..a5aa900 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -35,6 +35,11 @@
>  cc-option = $(shell if $(CC) $(1) -c -x c /dev/null -o /dev/null > /dev/null 2>&1; \
>  	      then echo $(1); fi)
>  
> +# Support building with MinGW for minimal Windows fsverity.exe
> +ifneq ($(findstring -mingw,$(shell $(CC) -dumpmachine 2>/dev/null)),)
> +MINGW = 1
> +endif
> +
>  CFLAGS ?= -O2
>  
>  override CFLAGS := -Wall -Wundef				\
> @@ -47,7 +52,7 @@ override CFLAGS := -Wall -Wundef				\
>  	$(call cc-option,-Wvla)					\
>  	$(CFLAGS)
>  
> -override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 $(CPPFLAGS)
> +override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(CPPFLAGS)
>  
>  ifneq ($(V),1)
>  QUIET_CC        = @echo '  CC      ' $@;
> @@ -62,7 +67,12 @@ BINDIR          ?= $(PREFIX)/bin
>  INCDIR          ?= $(PREFIX)/include
>  LIBDIR          ?= $(PREFIX)/lib
>  DESTDIR         ?=
> +ifneq ($(MINGW),1)
>  PKGCONF         ?= pkg-config
> +else
> +PKGCONF         := false
> +EXEEXT          := .exe
> +endif
>  
>  # Rebuild if a user-specified setting that affects the build changed.
>  .build-config: FORCE
> @@ -87,9 +97,9 @@ CFLAGS          += $(shell "$(PKGCONF)" libcrypto --cflags 2>/dev/null || echo)
>  # If we are dynamically linking, when running tests we need to override
>  # LD_LIBRARY_PATH as no RPATH is set
>  ifdef USE_SHARED_LIB
> -RUN_FSVERITY    = LD_LIBRARY_PATH=./ ./fsverity
> +RUN_FSVERITY    = LD_LIBRARY_PATH=./ ./fsverity$(EXEEXT)
>  else
> -RUN_FSVERITY    = ./fsverity
> +RUN_FSVERITY    = ./fsverity$(EXEEXT)
>  endif
>  
>  ##############################################################################
> @@ -99,6 +109,9 @@ endif
>  SOVERSION       := 0
>  LIB_CFLAGS      := $(CFLAGS) -fvisibility=hidden
>  LIB_SRC         := $(wildcard lib/*.c)
> +ifeq ($(MINGW),1)
> +LIB_SRC         := $(filter-out lib/enable.c,${LIB_SRC})
> +endif
>  LIB_HEADERS     := $(wildcard lib/*.h) $(COMMON_HEADERS)
>  STATIC_LIB_OBJ  := $(LIB_SRC:.c=.o)
>  SHARED_LIB_OBJ  := $(LIB_SRC:.c=.shlib.o)
> @@ -141,10 +154,13 @@ PROG_COMMON_SRC   := programs/utils.c
>  PROG_COMMON_OBJ   := $(PROG_COMMON_SRC:.c=.o)
>  FSVERITY_PROG_OBJ := $(PROG_COMMON_OBJ)		\
>  		     programs/cmd_digest.o	\
> -		     programs/cmd_enable.o	\
> -		     programs/cmd_measure.o	\
>  		     programs/cmd_sign.o	\
>  		     programs/fsverity.o
> +ifneq ($(MINGW),1)
> +FSVERITY_PROG_OBJ += \
> +		     programs/cmd_enable.o	\
> +		     programs/cmd_measure.o
> +endif
>  TEST_PROG_SRC     := $(wildcard programs/test_*.c)
>  TEST_PROGRAMS     := $(TEST_PROG_SRC:programs/%.c=%)
>  

The Makefile target to build the binary is still "fsverity", but for Windows it
actually builds "fsverity.exe".  I think that when the .exe extension is added,
the name of the Makefile target should change too.  Makefile targets should be
either a filename target *or* a special target, not conditionally either one.

> @@ -186,7 +202,7 @@ test_programs:$(TEST_PROGRAMS)
>  # want to run the full tests.
>  check:fsverity test_programs
>  	for prog in $(TEST_PROGRAMS); do \
> -		$(TEST_WRAPPER_PROG) ./$$prog || exit 1; \
> +		$(TEST_WRAPPER_PROG) ./$$prog$(EXEEXT) || exit 1; \
>  	done

The .exe extension isn't being added to the test programs when they are built.
Did you intend for building the test programs for Windows (and running them on
Windows) to be supported?

- Eric

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [PATCH v5] Allow to build and run sign/digest on Windows
  2020-12-17 19:25   ` [fsverity-utils PATCH v4 2/2] Allow to build and run sign/digest on Windows luca.boccassi
  2020-12-21 21:40     ` Eric Biggers
@ 2020-12-21 22:19     ` Luca Boccassi
  2020-12-21 23:03       ` Eric Biggers
  2020-12-21 23:24       ` [PATCH v6 1/3] Move -D_GNU_SOURCE to CPPFLAGS Luca Boccassi
  1 sibling, 2 replies; 34+ messages in thread
From: Luca Boccassi @ 2020-12-21 22:19 UTC (permalink / raw)
  To: linux-fscrypt; +Cc: ebiggers, Luca Boccassi

From: Luca Boccassi <luca.boccassi@microsoft.com>

Add some minimal compat type defs, and stub out the enable/measure
sources. Also add a way to handle the fact that mingw adds a
.exe extension automatically in the Makefile install rules.

Signed-off-by: Luca Boccassi <luca.boccassi@microsoft.com>
---
v2: rework the stubbing out to detect mingw in the Makefile and remove
    sources from compilation, instead of ifdefs.
    add a new common/win32_defs.h for the compat definitions.
    define strerror_r using strerror_s.

    To compile with mingw:
      make CC=x86_64-w64-mingw32-gcc-8.3-win32
    note that the openssl headers and a win32 libcrypto.dll need
    to be available in the default search paths, and otherwise have
    to be specified as expected via CPPFLAGS/LDFLAGS
v3: apply suggestion to remove -D_GNU_SOURCE from the header and define
    it as a CPPFLAGS, and to add  a definition of __printf for _WIN32
    to fix compiler warnings
    removed override of -lcrypto, not needed
v4: apply suggestion to remove overrides of %zu, as it now "just works".
    no more compilation warnings.
v5: change makefile fsverity target to use $(EXEEXT), and ensure make check
    can run under Windows (tested with wine, using TEST_WRAPPER_PROG=wine)

 Makefile               | 48 +++++++++++++++++++++++------------
 common/common_defs.h   |  2 ++
 common/fsverity_uapi.h |  2 ++
 common/win32_defs.h    | 57 ++++++++++++++++++++++++++++++++++++++++++
 lib/utils.c            | 11 ++++++--
 programs/fsverity.c    |  2 ++
 programs/utils.c       |  2 +-
 7 files changed, 105 insertions(+), 19 deletions(-)
 create mode 100644 common/win32_defs.h

diff --git a/Makefile b/Makefile
index bfe83c4..d850ae3 100644
--- a/Makefile
+++ b/Makefile
@@ -35,6 +35,11 @@
 cc-option = $(shell if $(CC) $(1) -c -x c /dev/null -o /dev/null > /dev/null 2>&1; \
 	      then echo $(1); fi)
 
+# Support building with MinGW for minimal Windows fsverity.exe
+ifneq ($(findstring -mingw,$(shell $(CC) -dumpmachine 2>/dev/null)),)
+MINGW = 1
+endif
+
 CFLAGS ?= -O2
 
 override CFLAGS := -Wall -Wundef				\
@@ -47,7 +52,7 @@ override CFLAGS := -Wall -Wundef				\
 	$(call cc-option,-Wvla)					\
 	$(CFLAGS)
 
-override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 $(CPPFLAGS)
+override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(CPPFLAGS)
 
 ifneq ($(V),1)
 QUIET_CC        = @echo '  CC      ' $@;
@@ -62,7 +67,12 @@ BINDIR          ?= $(PREFIX)/bin
 INCDIR          ?= $(PREFIX)/include
 LIBDIR          ?= $(PREFIX)/lib
 DESTDIR         ?=
+ifneq ($(MINGW),1)
 PKGCONF         ?= pkg-config
+else
+PKGCONF         := false
+EXEEXT          := .exe
+endif
 
 # Rebuild if a user-specified setting that affects the build changed.
 .build-config: FORCE
@@ -87,9 +97,9 @@ CFLAGS          += $(shell "$(PKGCONF)" libcrypto --cflags 2>/dev/null || echo)
 # If we are dynamically linking, when running tests we need to override
 # LD_LIBRARY_PATH as no RPATH is set
 ifdef USE_SHARED_LIB
-RUN_FSVERITY    = LD_LIBRARY_PATH=./ ./fsverity
+RUN_FSVERITY    = LD_LIBRARY_PATH=./ $(TEST_WRAPPER_PROG) ./fsverity$(EXEEXT)
 else
-RUN_FSVERITY    = ./fsverity
+RUN_FSVERITY    = $(TEST_WRAPPER_PROG) ./fsverity$(EXEEXT)
 endif
 
 ##############################################################################
@@ -99,6 +109,9 @@ endif
 SOVERSION       := 0
 LIB_CFLAGS      := $(CFLAGS) -fvisibility=hidden
 LIB_SRC         := $(wildcard lib/*.c)
+ifeq ($(MINGW),1)
+LIB_SRC         := $(filter-out lib/enable.c,${LIB_SRC})
+endif
 LIB_HEADERS     := $(wildcard lib/*.h) $(COMMON_HEADERS)
 STATIC_LIB_OBJ  := $(LIB_SRC:.c=.o)
 SHARED_LIB_OBJ  := $(LIB_SRC:.c=.shlib.o)
@@ -141,10 +154,13 @@ PROG_COMMON_SRC   := programs/utils.c
 PROG_COMMON_OBJ   := $(PROG_COMMON_SRC:.c=.o)
 FSVERITY_PROG_OBJ := $(PROG_COMMON_OBJ)		\
 		     programs/cmd_digest.o	\
-		     programs/cmd_enable.o	\
-		     programs/cmd_measure.o	\
 		     programs/cmd_sign.o	\
 		     programs/fsverity.o
+ifneq ($(MINGW),1)
+FSVERITY_PROG_OBJ += \
+		     programs/cmd_enable.o	\
+		     programs/cmd_measure.o
+endif
 TEST_PROG_SRC     := $(wildcard programs/test_*.c)
 TEST_PROGRAMS     := $(TEST_PROG_SRC:programs/%.c=%)
 
@@ -154,15 +170,15 @@ $(ALL_PROG_OBJ): %.o: %.c $(ALL_PROG_HEADERS) .build-config
 
 # Link the fsverity program
 ifdef USE_SHARED_LIB
-fsverity: $(FSVERITY_PROG_OBJ) libfsverity.so
+fsverity$(EXEEXT): $(FSVERITY_PROG_OBJ) libfsverity.so
 	$(QUIET_CCLD) $(CC) -o $@ $(FSVERITY_PROG_OBJ) \
 		$(CFLAGS) $(LDFLAGS) -L. -lfsverity
 else
-fsverity: $(FSVERITY_PROG_OBJ) libfsverity.a
+fsverity$(EXEEXT): $(FSVERITY_PROG_OBJ) libfsverity.a
 	$(QUIET_CCLD) $(CC) -o $@ $+ $(CFLAGS) $(LDFLAGS) $(LDLIBS)
 endif
 
-DEFAULT_TARGETS += fsverity
+DEFAULT_TARGETS += fsverity$(EXEEXT)
 
 # Link the test programs
 $(TEST_PROGRAMS): %: programs/%.o $(PROG_COMMON_OBJ) libfsverity.a
@@ -184,25 +200,25 @@ test_programs:$(TEST_PROGRAMS)
 
 # This just runs some quick, portable tests.  Use scripts/run-tests.sh if you
 # want to run the full tests.
-check:fsverity test_programs
+check:fsverity$(EXEEXT) test_programs
 	for prog in $(TEST_PROGRAMS); do \
-		$(TEST_WRAPPER_PROG) ./$$prog || exit 1; \
+		$(TEST_WRAPPER_PROG) ./$$prog$(EXEEXT) || exit 1; \
 	done
 	$(RUN_FSVERITY) --help > /dev/null
 	$(RUN_FSVERITY) --version > /dev/null
-	$(RUN_FSVERITY) sign fsverity fsverity.sig \
+	$(RUN_FSVERITY) sign fsverity$(EXEEXT) fsverity.sig \
 		--key=testdata/key.pem --cert=testdata/cert.pem > /dev/null
-	$(RUN_FSVERITY) sign fsverity fsverity.sig --hash=sha512 \
+	$(RUN_FSVERITY) sign fsverity$(EXEEXT) fsverity.sig --hash=sha512 \
 		--block-size=512 --salt=12345678 \
 		--key=testdata/key.pem --cert=testdata/cert.pem > /dev/null
-	$(RUN_FSVERITY) digest fsverity --hash=sha512 \
+	$(RUN_FSVERITY) digest fsverity$(EXEEXT) --hash=sha512 \
 		--block-size=512 --salt=12345678 > /dev/null
 	rm -f fsverity.sig
 	@echo "All tests passed!"
 
 install:all
 	install -d $(DESTDIR)$(LIBDIR)/pkgconfig $(DESTDIR)$(INCDIR) $(DESTDIR)$(BINDIR)
-	install -m755 fsverity $(DESTDIR)$(BINDIR)
+	install -m755 fsverity$(EXEEXT) $(DESTDIR)$(BINDIR)
 	install -m644 libfsverity.a $(DESTDIR)$(LIBDIR)
 	install -m755 libfsverity.so.$(SOVERSION) $(DESTDIR)$(LIBDIR)
 	ln -sf libfsverity.so.$(SOVERSION) $(DESTDIR)$(LIBDIR)/libfsverity.so
@@ -215,7 +231,7 @@ install:all
 	chmod 644 $(DESTDIR)$(LIBDIR)/pkgconfig/libfsverity.pc
 
 uninstall:
-	rm -f $(DESTDIR)$(BINDIR)/fsverity
+	rm -f $(DESTDIR)$(BINDIR)/fsverity$(EXEEXT)
 	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.a
 	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.so.$(SOVERSION)
 	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.so
@@ -232,4 +248,4 @@ help:
 
 clean:
 	rm -f $(DEFAULT_TARGETS) $(TEST_PROGRAMS) \
-		lib/*.o programs/*.o .build-config fsverity.sig
+		lib/*.o programs/*.o .build-config fsverity.sig *.exe
diff --git a/common/common_defs.h b/common/common_defs.h
index 279385a..3ae5561 100644
--- a/common/common_defs.h
+++ b/common/common_defs.h
@@ -15,6 +15,8 @@
 #include <stddef.h>
 #include <stdint.h>
 
+#include "win32_defs.h"
+
 typedef uint8_t u8;
 typedef uint16_t u16;
 typedef uint32_t u32;
diff --git a/common/fsverity_uapi.h b/common/fsverity_uapi.h
index 33f4415..be1d3f6 100644
--- a/common/fsverity_uapi.h
+++ b/common/fsverity_uapi.h
@@ -10,8 +10,10 @@
 #ifndef _UAPI_LINUX_FSVERITY_H
 #define _UAPI_LINUX_FSVERITY_H
 
+#ifndef _WIN32
 #include <linux/ioctl.h>
 #include <linux/types.h>
+#endif /* _WIN32 */
 
 #define FS_VERITY_HASH_ALG_SHA256	1
 #define FS_VERITY_HASH_ALG_SHA512	2
diff --git a/common/win32_defs.h b/common/win32_defs.h
new file mode 100644
index 0000000..29ef9b2
--- /dev/null
+++ b/common/win32_defs.h
@@ -0,0 +1,57 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * WIN32 compat definitions for libfsverity and the 'fsverity' program
+ *
+ * Copyright 2020 Microsoft
+ *
+ * Use of this source code is governed by an MIT-style
+ * license that can be found in the LICENSE file or at
+ * https://opensource.org/licenses/MIT.
+ */
+#ifndef COMMON_WIN32_DEFS_H
+#define COMMON_WIN32_DEFS_H
+
+/* Some minimal definitions to allow the digest/sign commands to run under Windows */
+
+/* All file reads we do need this flag on _WIN32 */
+#ifndef O_BINARY
+#  define O_BINARY 0
+#endif
+
+#ifdef _WIN32
+
+#include <stdint.h>
+#include <inttypes.h>
+
+#ifndef ENOPKG
+#   define ENOPKG 65
+#endif
+
+#ifndef __cold
+#  define __cold
+#endif
+
+/* For %zu in printf() */
+#ifndef __printf
+#  define __printf(fmt_idx, vargs_idx) \
+       __attribute__((format(gnu_printf, fmt_idx, vargs_idx)))
+#endif
+
+typedef __signed__ char __s8;
+typedef unsigned char __u8;
+typedef __signed__ short __s16;
+typedef unsigned short __u16;
+typedef __signed__ int __s32;
+typedef unsigned int __u32;
+typedef __signed__ long long  __s64;
+typedef unsigned long long  __u64;
+typedef __u16 __le16;
+typedef __u16 __be16;
+typedef __u32 __le32;
+typedef __u32 __be32;
+typedef __u64 __le64;
+typedef __u64 __be64;
+
+#endif /* _WIN32 */
+
+#endif /* COMMON_WIN32_DEFS_H */
diff --git a/lib/utils.c b/lib/utils.c
index 8b5d6cb..036dd60 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -9,8 +9,6 @@
  * https://opensource.org/licenses/MIT.
  */
 
-#define _GNU_SOURCE /* for asprintf() and strerror_r() */
-
 #include "lib_private.h"
 
 #include <stdio.h>
@@ -53,6 +51,15 @@ libfsverity_set_error_callback(void (*cb)(const char *msg))
 	libfsverity_error_cb = cb;
 }
 
+#ifdef _WIN32
+static char *strerror_r(int errnum, char *buf, size_t buflen)
+{
+	strerror_s(buf, buflen, errnum);
+
+	return buf;
+}
+#endif
+
 void libfsverity_do_error_msg(const char *format, va_list va, int err)
 {
 	int saved_errno = errno;
diff --git a/programs/fsverity.c b/programs/fsverity.c
index 5d5fbe2..f68e034 100644
--- a/programs/fsverity.c
+++ b/programs/fsverity.c
@@ -28,6 +28,7 @@ static const struct fsverity_command {
 "    fsverity digest FILE...\n"
 "               [--hash-alg=HASH_ALG] [--block-size=BLOCK_SIZE] [--salt=SALT]\n"
 "               [--compact] [--for-builtin-sig]\n"
+#ifndef _WIN32
 	}, {
 		.name = "enable",
 		.func = fsverity_cmd_enable,
@@ -43,6 +44,7 @@ static const struct fsverity_command {
 "Display the fs-verity digest of the given verity file(s)",
 		.usage_str =
 "    fsverity measure FILE...\n"
+#endif /* _WIN32 */
 	}, {
 		.name = "sign",
 		.func = fsverity_cmd_sign,
diff --git a/programs/utils.c b/programs/utils.c
index facccda..ce19b57 100644
--- a/programs/utils.c
+++ b/programs/utils.c
@@ -102,7 +102,7 @@ void install_libfsverity_error_handler(void)
 
 bool open_file(struct filedes *file, const char *filename, int flags, int mode)
 {
-	file->fd = open(filename, flags, mode);
+	file->fd = open(filename, flags | O_BINARY, mode);
 	if (file->fd < 0) {
 		error_msg_errno("can't open '%s' for %s", filename,
 				(flags & O_ACCMODE) == O_RDONLY ? "reading" :
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* Re: [fsverity-utils PATCH v4 2/2] Allow to build and run sign/digest on Windows
  2020-12-21 21:40     ` Eric Biggers
@ 2020-12-21 22:23       ` Luca Boccassi
  0 siblings, 0 replies; 34+ messages in thread
From: Luca Boccassi @ 2020-12-21 22:23 UTC (permalink / raw)
  To: Eric Biggers; +Cc: linux-fscrypt

On Mon, 21 Dec 2020 at 21:40, Eric Biggers <ebiggers@kernel.org> wrote:
>
> On Thu, Dec 17, 2020 at 07:25:16PM +0000, luca.boccassi@gmail.com wrote:
> > From: Luca Boccassi <luca.boccassi@microsoft.com>
> >
> > Add some minimal compat type defs, and stub out the enable/measure
> > sources. Also add a way to handle the fact that mingw adds a
> > .exe extension automatically in the Makefile install rules, and
> > that there is not pkg-config and the libcrypto linker flag is
> > different.
> >
> > Signed-off-by: Luca Boccassi <luca.boccassi@microsoft.com>
>
> This commit message is outdated; can you update it?

Fixed in v5.

> > diff --git a/Makefile b/Makefile
> > index bfe83c4..a5aa900 100644
> > --- a/Makefile
> > +++ b/Makefile
> > @@ -35,6 +35,11 @@
> >  cc-option = $(shell if $(CC) $(1) -c -x c /dev/null -o /dev/null > /dev/null 2>&1; \
> >             then echo $(1); fi)
> >
> > +# Support building with MinGW for minimal Windows fsverity.exe
> > +ifneq ($(findstring -mingw,$(shell $(CC) -dumpmachine 2>/dev/null)),)
> > +MINGW = 1
> > +endif
> > +
> >  CFLAGS ?= -O2
> >
> >  override CFLAGS := -Wall -Wundef                             \
> > @@ -47,7 +52,7 @@ override CFLAGS := -Wall -Wundef                            \
> >       $(call cc-option,-Wvla)                                 \
> >       $(CFLAGS)
> >
> > -override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 $(CPPFLAGS)
> > +override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(CPPFLAGS)
> >
> >  ifneq ($(V),1)
> >  QUIET_CC        = @echo '  CC      ' $@;
> > @@ -62,7 +67,12 @@ BINDIR          ?= $(PREFIX)/bin
> >  INCDIR          ?= $(PREFIX)/include
> >  LIBDIR          ?= $(PREFIX)/lib
> >  DESTDIR         ?=
> > +ifneq ($(MINGW),1)
> >  PKGCONF         ?= pkg-config
> > +else
> > +PKGCONF         := false
> > +EXEEXT          := .exe
> > +endif
> >
> >  # Rebuild if a user-specified setting that affects the build changed.
> >  .build-config: FORCE
> > @@ -87,9 +97,9 @@ CFLAGS          += $(shell "$(PKGCONF)" libcrypto --cflags 2>/dev/null || echo)
> >  # If we are dynamically linking, when running tests we need to override
> >  # LD_LIBRARY_PATH as no RPATH is set
> >  ifdef USE_SHARED_LIB
> > -RUN_FSVERITY    = LD_LIBRARY_PATH=./ ./fsverity
> > +RUN_FSVERITY    = LD_LIBRARY_PATH=./ ./fsverity$(EXEEXT)
> >  else
> > -RUN_FSVERITY    = ./fsverity
> > +RUN_FSVERITY    = ./fsverity$(EXEEXT)
> >  endif
> >
> >  ##############################################################################
> > @@ -99,6 +109,9 @@ endif
> >  SOVERSION       := 0
> >  LIB_CFLAGS      := $(CFLAGS) -fvisibility=hidden
> >  LIB_SRC         := $(wildcard lib/*.c)
> > +ifeq ($(MINGW),1)
> > +LIB_SRC         := $(filter-out lib/enable.c,${LIB_SRC})
> > +endif
> >  LIB_HEADERS     := $(wildcard lib/*.h) $(COMMON_HEADERS)
> >  STATIC_LIB_OBJ  := $(LIB_SRC:.c=.o)
> >  SHARED_LIB_OBJ  := $(LIB_SRC:.c=.shlib.o)
> > @@ -141,10 +154,13 @@ PROG_COMMON_SRC   := programs/utils.c
> >  PROG_COMMON_OBJ   := $(PROG_COMMON_SRC:.c=.o)
> >  FSVERITY_PROG_OBJ := $(PROG_COMMON_OBJ)              \
> >                    programs/cmd_digest.o      \
> > -                  programs/cmd_enable.o      \
> > -                  programs/cmd_measure.o     \
> >                    programs/cmd_sign.o        \
> >                    programs/fsverity.o
> > +ifneq ($(MINGW),1)
> > +FSVERITY_PROG_OBJ += \
> > +                  programs/cmd_enable.o      \
> > +                  programs/cmd_measure.o
> > +endif
> >  TEST_PROG_SRC     := $(wildcard programs/test_*.c)
> >  TEST_PROGRAMS     := $(TEST_PROG_SRC:programs/%.c=%)
> >
>
> The Makefile target to build the binary is still "fsverity", but for Windows it
> actually builds "fsverity.exe".  I think that when the .exe extension is added,
> the name of the Makefile target should change too.  Makefile targets should be
> either a filename target *or* a special target, not conditionally either one.

Ok, updated in v5.

> > @@ -186,7 +202,7 @@ test_programs:$(TEST_PROGRAMS)
> >  # want to run the full tests.
> >  check:fsverity test_programs
> >       for prog in $(TEST_PROGRAMS); do \
> > -             $(TEST_WRAPPER_PROG) ./$$prog || exit 1; \
> > +             $(TEST_WRAPPER_PROG) ./$$prog$(EXEEXT) || exit 1; \
> >       done
>
> The .exe extension isn't being added to the test programs when they are built.
> Did you intend for building the test programs for Windows (and running them on
> Windows) to be supported?

Yes it probably should. Made further updates, and at least with
mingw/wine make check works.

Kind regards,
Luca Boccassi

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH v5] Allow to build and run sign/digest on Windows
  2020-12-21 22:19     ` [PATCH v5] " Luca Boccassi
@ 2020-12-21 23:03       ` Eric Biggers
  2020-12-21 23:26         ` Luca Boccassi
  2020-12-21 23:24       ` [PATCH v6 1/3] Move -D_GNU_SOURCE to CPPFLAGS Luca Boccassi
  1 sibling, 1 reply; 34+ messages in thread
From: Eric Biggers @ 2020-12-21 23:03 UTC (permalink / raw)
  To: Luca Boccassi; +Cc: linux-fscrypt, Luca Boccassi

On Mon, Dec 21, 2020 at 10:19:53PM +0000, Luca Boccassi wrote:
> From: Luca Boccassi <luca.boccassi@microsoft.com>
> 
> stub out the enable/measure sources.

That's not the case anymore, right?  Now those files are just omitted.

> diff --git a/Makefile b/Makefile
> index bfe83c4..d850ae3 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -35,6 +35,11 @@
>  cc-option = $(shell if $(CC) $(1) -c -x c /dev/null -o /dev/null > /dev/null 2>&1; \
>  	      then echo $(1); fi)
>  
> +# Support building with MinGW for minimal Windows fsverity.exe
> +ifneq ($(findstring -mingw,$(shell $(CC) -dumpmachine 2>/dev/null)),)
> +MINGW = 1
> +endif

It would be helpful if this comment mentioned that libfsverity isn't built as a
proper Windows library.  At the moment it is not very clear.

Also, a note in the README about the Windows support would be helpful.

> -override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 $(CPPFLAGS)
> +override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(CPPFLAGS)

The _GNU_SOURCE change should go in a separate patch.

>  # Rebuild if a user-specified setting that affects the build changed.
>  .build-config: FORCE
> @@ -87,9 +97,9 @@ CFLAGS          += $(shell "$(PKGCONF)" libcrypto --cflags 2>/dev/null || echo)
>  # If we are dynamically linking, when running tests we need to override
>  # LD_LIBRARY_PATH as no RPATH is set
>  ifdef USE_SHARED_LIB
> -RUN_FSVERITY    = LD_LIBRARY_PATH=./ ./fsverity
> +RUN_FSVERITY    = LD_LIBRARY_PATH=./ $(TEST_WRAPPER_PROG) ./fsverity$(EXEEXT)
>  else
> -RUN_FSVERITY    = ./fsverity
> +RUN_FSVERITY    = $(TEST_WRAPPER_PROG) ./fsverity$(EXEEXT)
>  endif

Adding $(TEST_WRAPPER_PROG) here should go in a separate patch too.  It also
affects valgrind testing on Linux.

>  # Link the fsverity program
>  ifdef USE_SHARED_LIB
> -fsverity: $(FSVERITY_PROG_OBJ) libfsverity.so
> +fsverity$(EXEEXT): $(FSVERITY_PROG_OBJ) libfsverity.so

It would be easier to read if there was a variable $(FSVERITY) that had the
value of fsverity$(EXEEXT).

>  # Link the test programs
>  $(TEST_PROGRAMS): %: programs/%.o $(PROG_COMMON_OBJ) libfsverity.a
> @@ -184,25 +200,25 @@ test_programs:$(TEST_PROGRAMS)

This still doesn't actually add the .exe extension to the test programs.
Try 'make CC=x86_64-w64-mingw32-gcc help'; the targets to build the test
programs don't have the .exe extension.

- Eric

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [PATCH v6 1/3] Move -D_GNU_SOURCE to CPPFLAGS
  2020-12-21 22:19     ` [PATCH v5] " Luca Boccassi
  2020-12-21 23:03       ` Eric Biggers
@ 2020-12-21 23:24       ` Luca Boccassi
  2020-12-21 23:24         ` [PATCH v6 2/3] Wrap ./fsverity in TEST_WRAPPER_PROG too Luca Boccassi
                           ` (3 more replies)
  1 sibling, 4 replies; 34+ messages in thread
From: Luca Boccassi @ 2020-12-21 23:24 UTC (permalink / raw)
  To: linux-fscrypt; +Cc: ebiggers, Luca Boccassi

Ensures it is actually defined before any include is preprocessed.

Signed-off-by: Luca Boccassi <bluca@debian.org>
---
v6: split from mingw patch

 Makefile    | 2 +-
 lib/utils.c | 2 --
 2 files changed, 1 insertion(+), 3 deletions(-)

diff --git a/Makefile b/Makefile
index bfe83c4..f1ba956 100644
--- a/Makefile
+++ b/Makefile
@@ -47,7 +47,7 @@ override CFLAGS := -Wall -Wundef				\
 	$(call cc-option,-Wvla)					\
 	$(CFLAGS)
 
-override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 $(CPPFLAGS)
+override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(CPPFLAGS)
 
 ifneq ($(V),1)
 QUIET_CC        = @echo '  CC      ' $@;
diff --git a/lib/utils.c b/lib/utils.c
index 8b5d6cb..13e3b35 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -9,8 +9,6 @@
  * https://opensource.org/licenses/MIT.
  */
 
-#define _GNU_SOURCE /* for asprintf() and strerror_r() */
-
 #include "lib_private.h"
 
 #include <stdio.h>
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v6 2/3] Wrap ./fsverity in TEST_WRAPPER_PROG too
  2020-12-21 23:24       ` [PATCH v6 1/3] Move -D_GNU_SOURCE to CPPFLAGS Luca Boccassi
@ 2020-12-21 23:24         ` Luca Boccassi
  2020-12-21 23:24         ` [PATCH v6 3/3] Allow to build and run sign/digest on Windows Luca Boccassi
                           ` (2 subsequent siblings)
  3 siblings, 0 replies; 34+ messages in thread
From: Luca Boccassi @ 2020-12-21 23:24 UTC (permalink / raw)
  To: linux-fscrypt; +Cc: ebiggers, Luca Boccassi

Allows make check to run fsverity under the desired tool

Signed-off-by: Luca Boccassi <bluca@debian.org>
---
v6: split from mingw patch

 Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index f1ba956..583db8e 100644
--- a/Makefile
+++ b/Makefile
@@ -87,9 +87,9 @@ CFLAGS          += $(shell "$(PKGCONF)" libcrypto --cflags 2>/dev/null || echo)
 # If we are dynamically linking, when running tests we need to override
 # LD_LIBRARY_PATH as no RPATH is set
 ifdef USE_SHARED_LIB
-RUN_FSVERITY    = LD_LIBRARY_PATH=./ ./fsverity
+RUN_FSVERITY    = LD_LIBRARY_PATH=./ $(TEST_WRAPPER_PROG) ./fsverity
 else
-RUN_FSVERITY    = ./fsverity
+RUN_FSVERITY    = $(TEST_WRAPPER_PROG) ./fsverity
 endif
 
 ##############################################################################
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v6 3/3] Allow to build and run sign/digest on Windows
  2020-12-21 23:24       ` [PATCH v6 1/3] Move -D_GNU_SOURCE to CPPFLAGS Luca Boccassi
  2020-12-21 23:24         ` [PATCH v6 2/3] Wrap ./fsverity in TEST_WRAPPER_PROG too Luca Boccassi
@ 2020-12-21 23:24         ` Luca Boccassi
  2020-12-21 23:53           ` Eric Biggers
  2020-12-22  0:00         ` [PATCH v6 1/3] Move -D_GNU_SOURCE to CPPFLAGS Eric Biggers
  2020-12-22  0:10         ` [PATCH v7 " Luca Boccassi
  3 siblings, 1 reply; 34+ messages in thread
From: Luca Boccassi @ 2020-12-21 23:24 UTC (permalink / raw)
  To: linux-fscrypt; +Cc: ebiggers, Luca Boccassi, Luca Boccassi

Add some minimal compat type defs, and omit the enable/measure
sources. Also add a way to handle the fact that mingw adds a
.exe extension automatically in the Makefile install rules.

Signed-off-by: Luca Boccassi <luca.boccassi@microsoft.com>
---
v2: rework the stubbing out to detect mingw in the Makefile and remove
    sources from compilation, instead of ifdefs.
    add a new common/win32_defs.h for the compat definitions.
    define strerror_r using strerror_s.

    To compile with mingw:
      make CC=x86_64-w64-mingw32-gcc-8.3-win32
    note that the openssl headers and a win32 libcrypto.dll need
    to be available in the default search paths, and otherwise have
    to be specified as expected via CPPFLAGS/LDFLAGS
v3: apply suggestion to remove -D_GNU_SOURCE from the header and define
    it as a CPPFLAGS, and to add  a definition of __printf for _WIN32
    to fix compiler warnings
    removed override of -lcrypto, not needed
v4: apply suggestion to remove overrides of %zu, as it now "just works".
    no more compilation warnings.
v5: change makefile fsverity target to use $(EXEEXT), and ensure make check
    can run under Windows (tested with wine, using TEST_WRAPPER_PROG=wine)
v6: split GNU_SOURCES and TEST_WRAPPER_PROG out in separate patches.
    add note about mingw to README.md.
    note in Makefile that we don't build libfsverity.dll yet.
    ensure the TEST_PROGS targets get the .exe suffix if needed.

 Makefile               | 48 ++++++++++++++++++++++++-----------
 README.md              | 11 ++++++++
 common/common_defs.h   |  2 ++
 common/fsverity_uapi.h |  2 ++
 common/win32_defs.h    | 57 ++++++++++++++++++++++++++++++++++++++++++
 lib/utils.c            |  9 +++++++
 programs/fsverity.c    |  2 ++
 programs/utils.c       |  2 +-
 8 files changed, 117 insertions(+), 16 deletions(-)
 create mode 100644 common/win32_defs.h

diff --git a/Makefile b/Makefile
index 583db8e..0354f62 100644
--- a/Makefile
+++ b/Makefile
@@ -35,6 +35,12 @@
 cc-option = $(shell if $(CC) $(1) -c -x c /dev/null -o /dev/null > /dev/null 2>&1; \
 	      then echo $(1); fi)
 
+# Support building with MinGW for minimal Windows fsverity.exe, but not for
+# libfsverity. fsverity.exe will be statically linked.
+ifneq ($(findstring -mingw,$(shell $(CC) -dumpmachine 2>/dev/null)),)
+MINGW = 1
+endif
+
 CFLAGS ?= -O2
 
 override CFLAGS := -Wall -Wundef				\
@@ -62,7 +68,13 @@ BINDIR          ?= $(PREFIX)/bin
 INCDIR          ?= $(PREFIX)/include
 LIBDIR          ?= $(PREFIX)/lib
 DESTDIR         ?=
+ifneq ($(MINGW),1)
 PKGCONF         ?= pkg-config
+else
+PKGCONF         := false
+EXEEXT          := .exe
+endif
+FSVERITY        := fsverity$(EXEEXT)
 
 # Rebuild if a user-specified setting that affects the build changed.
 .build-config: FORCE
@@ -87,9 +99,9 @@ CFLAGS          += $(shell "$(PKGCONF)" libcrypto --cflags 2>/dev/null || echo)
 # If we are dynamically linking, when running tests we need to override
 # LD_LIBRARY_PATH as no RPATH is set
 ifdef USE_SHARED_LIB
-RUN_FSVERITY    = LD_LIBRARY_PATH=./ $(TEST_WRAPPER_PROG) ./fsverity
+RUN_FSVERITY    = LD_LIBRARY_PATH=./ $(TEST_WRAPPER_PROG) ./$(FSVERITY)
 else
-RUN_FSVERITY    = $(TEST_WRAPPER_PROG) ./fsverity
+RUN_FSVERITY    = $(TEST_WRAPPER_PROG) ./$(FSVERITY)
 endif
 
 ##############################################################################
@@ -99,6 +111,9 @@ endif
 SOVERSION       := 0
 LIB_CFLAGS      := $(CFLAGS) -fvisibility=hidden
 LIB_SRC         := $(wildcard lib/*.c)
+ifeq ($(MINGW),1)
+LIB_SRC         := $(filter-out lib/enable.c,${LIB_SRC})
+endif
 LIB_HEADERS     := $(wildcard lib/*.h) $(COMMON_HEADERS)
 STATIC_LIB_OBJ  := $(LIB_SRC:.c=.o)
 SHARED_LIB_OBJ  := $(LIB_SRC:.c=.shlib.o)
@@ -141,12 +156,15 @@ PROG_COMMON_SRC   := programs/utils.c
 PROG_COMMON_OBJ   := $(PROG_COMMON_SRC:.c=.o)
 FSVERITY_PROG_OBJ := $(PROG_COMMON_OBJ)		\
 		     programs/cmd_digest.o	\
-		     programs/cmd_enable.o	\
-		     programs/cmd_measure.o	\
 		     programs/cmd_sign.o	\
 		     programs/fsverity.o
+ifneq ($(MINGW),1)
+FSVERITY_PROG_OBJ += \
+		     programs/cmd_enable.o	\
+		     programs/cmd_measure.o
+endif
 TEST_PROG_SRC     := $(wildcard programs/test_*.c)
-TEST_PROGRAMS     := $(TEST_PROG_SRC:programs/%.c=%)
+TEST_PROGRAMS     := $(TEST_PROG_SRC:programs/%.c=%$(EXEEXT))
 
 # Compile program object files
 $(ALL_PROG_OBJ): %.o: %.c $(ALL_PROG_HEADERS) .build-config
@@ -154,18 +172,18 @@ $(ALL_PROG_OBJ): %.o: %.c $(ALL_PROG_HEADERS) .build-config
 
 # Link the fsverity program
 ifdef USE_SHARED_LIB
-fsverity: $(FSVERITY_PROG_OBJ) libfsverity.so
+$(FSVERITY): $(FSVERITY_PROG_OBJ) libfsverity.so
 	$(QUIET_CCLD) $(CC) -o $@ $(FSVERITY_PROG_OBJ) \
 		$(CFLAGS) $(LDFLAGS) -L. -lfsverity
 else
-fsverity: $(FSVERITY_PROG_OBJ) libfsverity.a
+$(FSVERITY): $(FSVERITY_PROG_OBJ) libfsverity.a
 	$(QUIET_CCLD) $(CC) -o $@ $+ $(CFLAGS) $(LDFLAGS) $(LDLIBS)
 endif
 
-DEFAULT_TARGETS += fsverity
+DEFAULT_TARGETS += $(FSVERITY)
 
 # Link the test programs
-$(TEST_PROGRAMS): %: programs/%.o $(PROG_COMMON_OBJ) libfsverity.a
+$(TEST_PROGRAMS): %$(EXEEXT): programs/%.o $(PROG_COMMON_OBJ) libfsverity.a
 	$(QUIET_CCLD) $(CC) -o $@ $+ $(CFLAGS) $(LDFLAGS) $(LDLIBS)
 
 ##############################################################################
@@ -184,25 +202,25 @@ test_programs:$(TEST_PROGRAMS)
 
 # This just runs some quick, portable tests.  Use scripts/run-tests.sh if you
 # want to run the full tests.
-check:fsverity test_programs
+check:$(FSVERITY) test_programs
 	for prog in $(TEST_PROGRAMS); do \
 		$(TEST_WRAPPER_PROG) ./$$prog || exit 1; \
 	done
 	$(RUN_FSVERITY) --help > /dev/null
 	$(RUN_FSVERITY) --version > /dev/null
-	$(RUN_FSVERITY) sign fsverity fsverity.sig \
+	$(RUN_FSVERITY) sign $(FSVERITY) fsverity.sig \
 		--key=testdata/key.pem --cert=testdata/cert.pem > /dev/null
-	$(RUN_FSVERITY) sign fsverity fsverity.sig --hash=sha512 \
+	$(RUN_FSVERITY) sign $(FSVERITY) fsverity.sig --hash=sha512 \
 		--block-size=512 --salt=12345678 \
 		--key=testdata/key.pem --cert=testdata/cert.pem > /dev/null
-	$(RUN_FSVERITY) digest fsverity --hash=sha512 \
+	$(RUN_FSVERITY) digest $(FSVERITY) --hash=sha512 \
 		--block-size=512 --salt=12345678 > /dev/null
 	rm -f fsverity.sig
 	@echo "All tests passed!"
 
 install:all
 	install -d $(DESTDIR)$(LIBDIR)/pkgconfig $(DESTDIR)$(INCDIR) $(DESTDIR)$(BINDIR)
-	install -m755 fsverity $(DESTDIR)$(BINDIR)
+	install -m755 $(FSVERITY) $(DESTDIR)$(BINDIR)
 	install -m644 libfsverity.a $(DESTDIR)$(LIBDIR)
 	install -m755 libfsverity.so.$(SOVERSION) $(DESTDIR)$(LIBDIR)
 	ln -sf libfsverity.so.$(SOVERSION) $(DESTDIR)$(LIBDIR)/libfsverity.so
@@ -215,7 +233,7 @@ install:all
 	chmod 644 $(DESTDIR)$(LIBDIR)/pkgconfig/libfsverity.pc
 
 uninstall:
-	rm -f $(DESTDIR)$(BINDIR)/fsverity
+	rm -f $(DESTDIR)$(BINDIR)/$(FSVERITY)
 	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.a
 	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.so.$(SOVERSION)
 	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.so
diff --git a/README.md b/README.md
index 6045c75..c630f7d 100644
--- a/README.md
+++ b/README.md
@@ -50,6 +50,17 @@ use `make USE_SHARED_LIB=1` to use dynamic linking instead.
 
 See the `Makefile` for other supported build and installation options.
 
+### Building on Windows
+
+There is minimal support for building Windows executables using MinGW.
+```bash
+    make CC=x86_64-w64-mingw32-gcc-win32
+```
+
+`fsverity.exe` will be built, and it supports the `digest` and `sign` commands.
+
+A Windows build of OpenSSL/libcrypto needs to be available.
+
 ## Examples
 
 ### Basic use
diff --git a/common/common_defs.h b/common/common_defs.h
index 279385a..3ae5561 100644
--- a/common/common_defs.h
+++ b/common/common_defs.h
@@ -15,6 +15,8 @@
 #include <stddef.h>
 #include <stdint.h>
 
+#include "win32_defs.h"
+
 typedef uint8_t u8;
 typedef uint16_t u16;
 typedef uint32_t u32;
diff --git a/common/fsverity_uapi.h b/common/fsverity_uapi.h
index 33f4415..be1d3f6 100644
--- a/common/fsverity_uapi.h
+++ b/common/fsverity_uapi.h
@@ -10,8 +10,10 @@
 #ifndef _UAPI_LINUX_FSVERITY_H
 #define _UAPI_LINUX_FSVERITY_H
 
+#ifndef _WIN32
 #include <linux/ioctl.h>
 #include <linux/types.h>
+#endif /* _WIN32 */
 
 #define FS_VERITY_HASH_ALG_SHA256	1
 #define FS_VERITY_HASH_ALG_SHA512	2
diff --git a/common/win32_defs.h b/common/win32_defs.h
new file mode 100644
index 0000000..29ef9b2
--- /dev/null
+++ b/common/win32_defs.h
@@ -0,0 +1,57 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * WIN32 compat definitions for libfsverity and the 'fsverity' program
+ *
+ * Copyright 2020 Microsoft
+ *
+ * Use of this source code is governed by an MIT-style
+ * license that can be found in the LICENSE file or at
+ * https://opensource.org/licenses/MIT.
+ */
+#ifndef COMMON_WIN32_DEFS_H
+#define COMMON_WIN32_DEFS_H
+
+/* Some minimal definitions to allow the digest/sign commands to run under Windows */
+
+/* All file reads we do need this flag on _WIN32 */
+#ifndef O_BINARY
+#  define O_BINARY 0
+#endif
+
+#ifdef _WIN32
+
+#include <stdint.h>
+#include <inttypes.h>
+
+#ifndef ENOPKG
+#   define ENOPKG 65
+#endif
+
+#ifndef __cold
+#  define __cold
+#endif
+
+/* For %zu in printf() */
+#ifndef __printf
+#  define __printf(fmt_idx, vargs_idx) \
+       __attribute__((format(gnu_printf, fmt_idx, vargs_idx)))
+#endif
+
+typedef __signed__ char __s8;
+typedef unsigned char __u8;
+typedef __signed__ short __s16;
+typedef unsigned short __u16;
+typedef __signed__ int __s32;
+typedef unsigned int __u32;
+typedef __signed__ long long  __s64;
+typedef unsigned long long  __u64;
+typedef __u16 __le16;
+typedef __u16 __be16;
+typedef __u32 __le32;
+typedef __u32 __be32;
+typedef __u64 __le64;
+typedef __u64 __be64;
+
+#endif /* _WIN32 */
+
+#endif /* COMMON_WIN32_DEFS_H */
diff --git a/lib/utils.c b/lib/utils.c
index 13e3b35..036dd60 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -51,6 +51,15 @@ libfsverity_set_error_callback(void (*cb)(const char *msg))
 	libfsverity_error_cb = cb;
 }
 
+#ifdef _WIN32
+static char *strerror_r(int errnum, char *buf, size_t buflen)
+{
+	strerror_s(buf, buflen, errnum);
+
+	return buf;
+}
+#endif
+
 void libfsverity_do_error_msg(const char *format, va_list va, int err)
 {
 	int saved_errno = errno;
diff --git a/programs/fsverity.c b/programs/fsverity.c
index 5d5fbe2..f68e034 100644
--- a/programs/fsverity.c
+++ b/programs/fsverity.c
@@ -28,6 +28,7 @@ static const struct fsverity_command {
 "    fsverity digest FILE...\n"
 "               [--hash-alg=HASH_ALG] [--block-size=BLOCK_SIZE] [--salt=SALT]\n"
 "               [--compact] [--for-builtin-sig]\n"
+#ifndef _WIN32
 	}, {
 		.name = "enable",
 		.func = fsverity_cmd_enable,
@@ -43,6 +44,7 @@ static const struct fsverity_command {
 "Display the fs-verity digest of the given verity file(s)",
 		.usage_str =
 "    fsverity measure FILE...\n"
+#endif /* _WIN32 */
 	}, {
 		.name = "sign",
 		.func = fsverity_cmd_sign,
diff --git a/programs/utils.c b/programs/utils.c
index facccda..ce19b57 100644
--- a/programs/utils.c
+++ b/programs/utils.c
@@ -102,7 +102,7 @@ void install_libfsverity_error_handler(void)
 
 bool open_file(struct filedes *file, const char *filename, int flags, int mode)
 {
-	file->fd = open(filename, flags, mode);
+	file->fd = open(filename, flags | O_BINARY, mode);
 	if (file->fd < 0) {
 		error_msg_errno("can't open '%s' for %s", filename,
 				(flags & O_ACCMODE) == O_RDONLY ? "reading" :
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* Re: [PATCH v5] Allow to build and run sign/digest on Windows
  2020-12-21 23:03       ` Eric Biggers
@ 2020-12-21 23:26         ` Luca Boccassi
  0 siblings, 0 replies; 34+ messages in thread
From: Luca Boccassi @ 2020-12-21 23:26 UTC (permalink / raw)
  To: Eric Biggers; +Cc: linux-fscrypt, Luca Boccassi

On Mon, 21 Dec 2020 at 23:03, Eric Biggers <ebiggers@kernel.org> wrote:
>
> On Mon, Dec 21, 2020 at 10:19:53PM +0000, Luca Boccassi wrote:
> > From: Luca Boccassi <luca.boccassi@microsoft.com>
> >
> > stub out the enable/measure sources.
>
> That's not the case anymore, right?  Now those files are just omitted.

Clarified in v6

> > diff --git a/Makefile b/Makefile
> > index bfe83c4..d850ae3 100644
> > --- a/Makefile
> > +++ b/Makefile
> > @@ -35,6 +35,11 @@
> >  cc-option = $(shell if $(CC) $(1) -c -x c /dev/null -o /dev/null > /dev/null 2>&1; \
> >             then echo $(1); fi)
> >
> > +# Support building with MinGW for minimal Windows fsverity.exe
> > +ifneq ($(findstring -mingw,$(shell $(CC) -dumpmachine 2>/dev/null)),)
> > +MINGW = 1
> > +endif
>
> It would be helpful if this comment mentioned that libfsverity isn't built as a
> proper Windows library.  At the moment it is not very clear.
>
> Also, a note in the README about the Windows support would be helpful.

Done both in v6

> > -override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 $(CPPFLAGS)
> > +override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(CPPFLAGS)
>
> The _GNU_SOURCE change should go in a separate patch.

Split out in v6

> >  # Rebuild if a user-specified setting that affects the build changed.
> >  .build-config: FORCE
> > @@ -87,9 +97,9 @@ CFLAGS          += $(shell "$(PKGCONF)" libcrypto --cflags 2>/dev/null || echo)
> >  # If we are dynamically linking, when running tests we need to override
> >  # LD_LIBRARY_PATH as no RPATH is set
> >  ifdef USE_SHARED_LIB
> > -RUN_FSVERITY    = LD_LIBRARY_PATH=./ ./fsverity
> > +RUN_FSVERITY    = LD_LIBRARY_PATH=./ $(TEST_WRAPPER_PROG) ./fsverity$(EXEEXT)
> >  else
> > -RUN_FSVERITY    = ./fsverity
> > +RUN_FSVERITY    = $(TEST_WRAPPER_PROG) ./fsverity$(EXEEXT)
> >  endif
>
> Adding $(TEST_WRAPPER_PROG) here should go in a separate patch too.  It also
> affects valgrind testing on Linux.

Split out in v6

> >  # Link the fsverity program
> >  ifdef USE_SHARED_LIB
> > -fsverity: $(FSVERITY_PROG_OBJ) libfsverity.so
> > +fsverity$(EXEEXT): $(FSVERITY_PROG_OBJ) libfsverity.so
>
> It would be easier to read if there was a variable $(FSVERITY) that had the
> value of fsverity$(EXEEXT).

Done in v6

> >  # Link the test programs
> >  $(TEST_PROGRAMS): %: programs/%.o $(PROG_COMMON_OBJ) libfsverity.a
> > @@ -184,25 +200,25 @@ test_programs:$(TEST_PROGRAMS)
>
> This still doesn't actually add the .exe extension to the test programs.
> Try 'make CC=x86_64-w64-mingw32-gcc help'; the targets to build the test
> programs don't have the .exe extension.

Yeah it didn't show in the target - although the binaries do have the
extension. Should be all fixed now in v6.

Kind regards,
Luca Boccassi

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH v6 3/3] Allow to build and run sign/digest on Windows
  2020-12-21 23:24         ` [PATCH v6 3/3] Allow to build and run sign/digest on Windows Luca Boccassi
@ 2020-12-21 23:53           ` Eric Biggers
  2020-12-21 23:57             ` Luca Boccassi
  0 siblings, 1 reply; 34+ messages in thread
From: Eric Biggers @ 2020-12-21 23:53 UTC (permalink / raw)
  To: Luca Boccassi; +Cc: linux-fscrypt, Luca Boccassi

On Mon, Dec 21, 2020 at 11:24:28PM +0000, Luca Boccassi wrote:
> +### Building on Windows
> +
> +There is minimal support for building Windows executables using MinGW.
> +```bash
> +    make CC=x86_64-w64-mingw32-gcc-win32
> +```
> +
> +`fsverity.exe` will be built, and it supports the `digest` and `sign` commands.
> +
> +A Windows build of OpenSSL/libcrypto needs to be available.

For me "CC=x86_64-w64-mingw32-gcc-win32" doesn't work; I need
"x86_64-w64-mingw32-gcc" instead.  Is this difference intentional?

- Eric

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH v6 3/3] Allow to build and run sign/digest on Windows
  2020-12-21 23:53           ` Eric Biggers
@ 2020-12-21 23:57             ` Luca Boccassi
  2020-12-22  0:03               ` Eric Biggers
  0 siblings, 1 reply; 34+ messages in thread
From: Luca Boccassi @ 2020-12-21 23:57 UTC (permalink / raw)
  To: Eric Biggers; +Cc: linux-fscrypt, Luca Boccassi

On Mon, 21 Dec 2020 at 23:53, Eric Biggers <ebiggers@kernel.org> wrote:
>
> On Mon, Dec 21, 2020 at 11:24:28PM +0000, Luca Boccassi wrote:
> > +### Building on Windows
> > +
> > +There is minimal support for building Windows executables using MinGW.
> > +```bash
> > +    make CC=x86_64-w64-mingw32-gcc-win32
> > +```
> > +
> > +`fsverity.exe` will be built, and it supports the `digest` and `sign` commands.
> > +
> > +A Windows build of OpenSSL/libcrypto needs to be available.
>
> For me "CC=x86_64-w64-mingw32-gcc-win32" doesn't work; I need
> "x86_64-w64-mingw32-gcc" instead.  Is this difference intentional?
>
> - Eric

It's a distro setup difference I think, on Debian
x86_64-w64-mingw32-gcc is a symlink to x86_64-w64-mingw32-gcc-win32:

$ ls -l /usr/bin/x86_64-w64-mingw32-gcc-win32
-rwxr-xr-x 2 root root 1160320 Nov 27 05:57
/usr/bin/x86_64-w64-mingw32-gcc-win32
$ ls -l /usr/bin/x86_64-w64-mingw32-gcc
lrwxrwxrwx 1 root root 40 Sep 27 18:41 /usr/bin/x86_64-w64-mingw32-gcc
-> /etc/alternatives/x86_64-w64-mingw32-gcc
$ ls -l /etc/alternatives/x86_64-w64-mingw32-gcc
lrwxrwxrwx 1 root root 37 Sep 27 18:44
/etc/alternatives/x86_64-w64-mingw32-gcc ->
/usr/bin/x86_64-w64-mingw32-gcc-win32

Kind regards,
Luca Boccassi

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH v6 1/3] Move -D_GNU_SOURCE to CPPFLAGS
  2020-12-21 23:24       ` [PATCH v6 1/3] Move -D_GNU_SOURCE to CPPFLAGS Luca Boccassi
  2020-12-21 23:24         ` [PATCH v6 2/3] Wrap ./fsverity in TEST_WRAPPER_PROG too Luca Boccassi
  2020-12-21 23:24         ` [PATCH v6 3/3] Allow to build and run sign/digest on Windows Luca Boccassi
@ 2020-12-22  0:00         ` Eric Biggers
  2020-12-22  0:12           ` Luca Boccassi
  2020-12-22  0:10         ` [PATCH v7 " Luca Boccassi
  3 siblings, 1 reply; 34+ messages in thread
From: Eric Biggers @ 2020-12-22  0:00 UTC (permalink / raw)
  To: Luca Boccassi; +Cc: linux-fscrypt

On Mon, Dec 21, 2020 at 11:24:26PM +0000, Luca Boccassi wrote:
> Ensures it is actually defined before any include is preprocessed.

It was already at the beginning of the .c file, so this isn't a very good
explanation.  A better explanation would be "Use _GNU_SOURCE consistently in
every file rather than in just one file.  This is needed for the Windows build
in order to consistently get the MinGW version of printf.".

> diff --git a/Makefile b/Makefile
> index bfe83c4..f1ba956 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -47,7 +47,7 @@ override CFLAGS := -Wall -Wundef				\
>  	$(call cc-option,-Wvla)					\
>  	$(CFLAGS)
>  
> -override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 $(CPPFLAGS)
> +override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(CPPFLAGS)
>  
>  ifneq ($(V),1)
>  QUIET_CC        = @echo '  CC      ' $@;

Can you add -D_GNU_SOURCE to ./scripts/run-sparse.sh too?
Otherwise I get errors when running scripts/run-tests.sh:

[Mon Dec 21 03:52:15 PM PST 2020] Run sparse
./lib/utils.c:71:13: error: undefined identifier 'vasprintf'
./lib/utils.c:78:21: error: undefined identifier 'asprintf'

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH v6 3/3] Allow to build and run sign/digest on Windows
  2020-12-21 23:57             ` Luca Boccassi
@ 2020-12-22  0:03               ` Eric Biggers
  2020-12-22  0:11                 ` Luca Boccassi
  0 siblings, 1 reply; 34+ messages in thread
From: Eric Biggers @ 2020-12-22  0:03 UTC (permalink / raw)
  To: Luca Boccassi; +Cc: linux-fscrypt, Luca Boccassi

On Mon, Dec 21, 2020 at 11:57:41PM +0000, Luca Boccassi wrote:
> On Mon, 21 Dec 2020 at 23:53, Eric Biggers <ebiggers@kernel.org> wrote:
> >
> > On Mon, Dec 21, 2020 at 11:24:28PM +0000, Luca Boccassi wrote:
> > > +### Building on Windows
> > > +
> > > +There is minimal support for building Windows executables using MinGW.
> > > +```bash
> > > +    make CC=x86_64-w64-mingw32-gcc-win32
> > > +```
> > > +
> > > +`fsverity.exe` will be built, and it supports the `digest` and `sign` commands.
> > > +
> > > +A Windows build of OpenSSL/libcrypto needs to be available.
> >
> > For me "CC=x86_64-w64-mingw32-gcc-win32" doesn't work; I need
> > "x86_64-w64-mingw32-gcc" instead.  Is this difference intentional?
> >
> > - Eric
> 
> It's a distro setup difference I think, on Debian
> x86_64-w64-mingw32-gcc is a symlink to x86_64-w64-mingw32-gcc-win32:
> 
> $ ls -l /usr/bin/x86_64-w64-mingw32-gcc-win32
> -rwxr-xr-x 2 root root 1160320 Nov 27 05:57
> /usr/bin/x86_64-w64-mingw32-gcc-win32
> $ ls -l /usr/bin/x86_64-w64-mingw32-gcc
> lrwxrwxrwx 1 root root 40 Sep 27 18:41 /usr/bin/x86_64-w64-mingw32-gcc
> -> /etc/alternatives/x86_64-w64-mingw32-gcc
> $ ls -l /etc/alternatives/x86_64-w64-mingw32-gcc
> lrwxrwxrwx 1 root root 37 Sep 27 18:44
> /etc/alternatives/x86_64-w64-mingw32-gcc ->
> /usr/bin/x86_64-w64-mingw32-gcc-win32

Okay, it would be better to document the one that works on all distros.

- Eric

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [PATCH v7 1/3] Move -D_GNU_SOURCE to CPPFLAGS
  2020-12-21 23:24       ` [PATCH v6 1/3] Move -D_GNU_SOURCE to CPPFLAGS Luca Boccassi
                           ` (2 preceding siblings ...)
  2020-12-22  0:00         ` [PATCH v6 1/3] Move -D_GNU_SOURCE to CPPFLAGS Eric Biggers
@ 2020-12-22  0:10         ` Luca Boccassi
  2020-12-22  0:10           ` [PATCH v7 2/3] Wrap ./fsverity in TEST_WRAPPER_PROG too Luca Boccassi
                             ` (3 more replies)
  3 siblings, 4 replies; 34+ messages in thread
From: Luca Boccassi @ 2020-12-22  0:10 UTC (permalink / raw)
  To: linux-fscrypt; +Cc: ebiggers

Use _GNU_SOURCE consistently in every file rather than just one file.
This is needed for the Windows build in order to consistently get the MinGW
version of printf.

Signed-off-by: Luca Boccassi <bluca@debian.org>
---
v6: split from mingw patch

v7: adjust commit message and add CPPFLAG to run-sparse.sh as well

 Makefile              | 2 +-
 lib/utils.c           | 2 --
 scripts/run-sparse.sh | 2 +-
 3 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/Makefile b/Makefile
index bfe83c4..f1ba956 100644
--- a/Makefile
+++ b/Makefile
@@ -47,7 +47,7 @@ override CFLAGS := -Wall -Wundef				\
 	$(call cc-option,-Wvla)					\
 	$(CFLAGS)
 
-override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 $(CPPFLAGS)
+override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(CPPFLAGS)
 
 ifneq ($(V),1)
 QUIET_CC        = @echo '  CC      ' $@;
diff --git a/lib/utils.c b/lib/utils.c
index 8b5d6cb..13e3b35 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -9,8 +9,6 @@
  * https://opensource.org/licenses/MIT.
  */
 
-#define _GNU_SOURCE /* for asprintf() and strerror_r() */
-
 #include "lib_private.h"
 
 #include <stdio.h>
diff --git a/scripts/run-sparse.sh b/scripts/run-sparse.sh
index 30730b2..f75b837 100755
--- a/scripts/run-sparse.sh
+++ b/scripts/run-sparse.sh
@@ -10,5 +10,5 @@ set -e -u -o pipefail
 
 find . -name '*.c' | while read -r file; do
 	sparse "$file" -gcc-base-dir "$(gcc --print-file-name=)"	\
-		-Iinclude -D_FILE_OFFSET_BITS=64 -Wbitwise
+		-Iinclude -D_FILE_OFFSET_BITS=64 -Wbitwise -D_GNU_SOURCE
 done
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v7 2/3] Wrap ./fsverity in TEST_WRAPPER_PROG too
  2020-12-22  0:10         ` [PATCH v7 " Luca Boccassi
@ 2020-12-22  0:10           ` Luca Boccassi
  2020-12-22 18:41             ` Eric Biggers
  2020-12-22  0:10           ` [PATCH v7 3/3] Allow to build and run sign/digest on Windows Luca Boccassi
                             ` (2 subsequent siblings)
  3 siblings, 1 reply; 34+ messages in thread
From: Luca Boccassi @ 2020-12-22  0:10 UTC (permalink / raw)
  To: linux-fscrypt; +Cc: ebiggers

Allows make check to run fsverity under the desired tool

Signed-off-by: Luca Boccassi <bluca@debian.org>
---
v6: split from mingw patch

 Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index f1ba956..583db8e 100644
--- a/Makefile
+++ b/Makefile
@@ -87,9 +87,9 @@ CFLAGS          += $(shell "$(PKGCONF)" libcrypto --cflags 2>/dev/null || echo)
 # If we are dynamically linking, when running tests we need to override
 # LD_LIBRARY_PATH as no RPATH is set
 ifdef USE_SHARED_LIB
-RUN_FSVERITY    = LD_LIBRARY_PATH=./ ./fsverity
+RUN_FSVERITY    = LD_LIBRARY_PATH=./ $(TEST_WRAPPER_PROG) ./fsverity
 else
-RUN_FSVERITY    = ./fsverity
+RUN_FSVERITY    = $(TEST_WRAPPER_PROG) ./fsverity
 endif
 
 ##############################################################################
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v7 3/3] Allow to build and run sign/digest on Windows
  2020-12-22  0:10         ` [PATCH v7 " Luca Boccassi
  2020-12-22  0:10           ` [PATCH v7 2/3] Wrap ./fsverity in TEST_WRAPPER_PROG too Luca Boccassi
@ 2020-12-22  0:10           ` Luca Boccassi
  2020-12-22 18:40             ` Eric Biggers
  2020-12-22  8:21           ` [PATCH v7 1/3] Move -D_GNU_SOURCE to CPPFLAGS Eric Biggers
  2020-12-22 18:40           ` Eric Biggers
  3 siblings, 1 reply; 34+ messages in thread
From: Luca Boccassi @ 2020-12-22  0:10 UTC (permalink / raw)
  To: linux-fscrypt; +Cc: ebiggers

Add some minimal compat type defs, and omit the enable/measure
sources. Also add a way to handle the fact that mingw adds a
.exe extension automatically in the Makefile install rules.

Signed-off-by: Luca Boccassi <luca.boccassi@microsoft.com>
---
v2: rework the stubbing out to detect mingw in the Makefile and remove
    sources from compilation, instead of ifdefs.
    add a new common/win32_defs.h for the compat definitions.
    define strerror_r using strerror_s.

    To compile with mingw:
      make CC=x86_64-w64-mingw32-gcc-8.3-win32
    note that the openssl headers and a win32 libcrypto.dll need
    to be available in the default search paths, and otherwise have
    to be specified as expected via CPPFLAGS/LDFLAGS
v3: apply suggestion to remove -D_GNU_SOURCE from the header and define
    it as a CPPFLAGS, and to add  a definition of __printf for _WIN32
    to fix compiler warnings
    removed override of -lcrypto, not needed
v4: apply suggestion to remove overrides of %zu, as it now "just works".
    no more compilation warnings.
v5: change makefile fsverity target to use $(EXEEXT), and ensure make check
    can run under Windows (tested with wine, using TEST_WRAPPER_PROG=wine)
v6: split GNU_SOURCES and TEST_WRAPPER_PROG out in separate patches.
    add note about mingw to README.md.
    note in Makefile that we don't build libfsverity.dll yet.
    ensure the TEST_PROGS targets get the .exe suffix if needed.
v7: adjust path to mingw compiler in README.md


 Makefile               | 48 ++++++++++++++++++++++++-----------
 README.md              | 11 ++++++++
 common/common_defs.h   |  2 ++
 common/fsverity_uapi.h |  2 ++
 common/win32_defs.h    | 57 ++++++++++++++++++++++++++++++++++++++++++
 lib/utils.c            |  9 +++++++
 programs/fsverity.c    |  2 ++
 programs/utils.c       |  2 +-
 8 files changed, 117 insertions(+), 16 deletions(-)
 create mode 100644 common/win32_defs.h

diff --git a/Makefile b/Makefile
index 583db8e..0354f62 100644
--- a/Makefile
+++ b/Makefile
@@ -35,6 +35,12 @@
 cc-option = $(shell if $(CC) $(1) -c -x c /dev/null -o /dev/null > /dev/null 2>&1; \
 	      then echo $(1); fi)
 
+# Support building with MinGW for minimal Windows fsverity.exe, but not for
+# libfsverity. fsverity.exe will be statically linked.
+ifneq ($(findstring -mingw,$(shell $(CC) -dumpmachine 2>/dev/null)),)
+MINGW = 1
+endif
+
 CFLAGS ?= -O2
 
 override CFLAGS := -Wall -Wundef				\
@@ -62,7 +68,13 @@ BINDIR          ?= $(PREFIX)/bin
 INCDIR          ?= $(PREFIX)/include
 LIBDIR          ?= $(PREFIX)/lib
 DESTDIR         ?=
+ifneq ($(MINGW),1)
 PKGCONF         ?= pkg-config
+else
+PKGCONF         := false
+EXEEXT          := .exe
+endif
+FSVERITY        := fsverity$(EXEEXT)
 
 # Rebuild if a user-specified setting that affects the build changed.
 .build-config: FORCE
@@ -87,9 +99,9 @@ CFLAGS          += $(shell "$(PKGCONF)" libcrypto --cflags 2>/dev/null || echo)
 # If we are dynamically linking, when running tests we need to override
 # LD_LIBRARY_PATH as no RPATH is set
 ifdef USE_SHARED_LIB
-RUN_FSVERITY    = LD_LIBRARY_PATH=./ $(TEST_WRAPPER_PROG) ./fsverity
+RUN_FSVERITY    = LD_LIBRARY_PATH=./ $(TEST_WRAPPER_PROG) ./$(FSVERITY)
 else
-RUN_FSVERITY    = $(TEST_WRAPPER_PROG) ./fsverity
+RUN_FSVERITY    = $(TEST_WRAPPER_PROG) ./$(FSVERITY)
 endif
 
 ##############################################################################
@@ -99,6 +111,9 @@ endif
 SOVERSION       := 0
 LIB_CFLAGS      := $(CFLAGS) -fvisibility=hidden
 LIB_SRC         := $(wildcard lib/*.c)
+ifeq ($(MINGW),1)
+LIB_SRC         := $(filter-out lib/enable.c,${LIB_SRC})
+endif
 LIB_HEADERS     := $(wildcard lib/*.h) $(COMMON_HEADERS)
 STATIC_LIB_OBJ  := $(LIB_SRC:.c=.o)
 SHARED_LIB_OBJ  := $(LIB_SRC:.c=.shlib.o)
@@ -141,12 +156,15 @@ PROG_COMMON_SRC   := programs/utils.c
 PROG_COMMON_OBJ   := $(PROG_COMMON_SRC:.c=.o)
 FSVERITY_PROG_OBJ := $(PROG_COMMON_OBJ)		\
 		     programs/cmd_digest.o	\
-		     programs/cmd_enable.o	\
-		     programs/cmd_measure.o	\
 		     programs/cmd_sign.o	\
 		     programs/fsverity.o
+ifneq ($(MINGW),1)
+FSVERITY_PROG_OBJ += \
+		     programs/cmd_enable.o	\
+		     programs/cmd_measure.o
+endif
 TEST_PROG_SRC     := $(wildcard programs/test_*.c)
-TEST_PROGRAMS     := $(TEST_PROG_SRC:programs/%.c=%)
+TEST_PROGRAMS     := $(TEST_PROG_SRC:programs/%.c=%$(EXEEXT))
 
 # Compile program object files
 $(ALL_PROG_OBJ): %.o: %.c $(ALL_PROG_HEADERS) .build-config
@@ -154,18 +172,18 @@ $(ALL_PROG_OBJ): %.o: %.c $(ALL_PROG_HEADERS) .build-config
 
 # Link the fsverity program
 ifdef USE_SHARED_LIB
-fsverity: $(FSVERITY_PROG_OBJ) libfsverity.so
+$(FSVERITY): $(FSVERITY_PROG_OBJ) libfsverity.so
 	$(QUIET_CCLD) $(CC) -o $@ $(FSVERITY_PROG_OBJ) \
 		$(CFLAGS) $(LDFLAGS) -L. -lfsverity
 else
-fsverity: $(FSVERITY_PROG_OBJ) libfsverity.a
+$(FSVERITY): $(FSVERITY_PROG_OBJ) libfsverity.a
 	$(QUIET_CCLD) $(CC) -o $@ $+ $(CFLAGS) $(LDFLAGS) $(LDLIBS)
 endif
 
-DEFAULT_TARGETS += fsverity
+DEFAULT_TARGETS += $(FSVERITY)
 
 # Link the test programs
-$(TEST_PROGRAMS): %: programs/%.o $(PROG_COMMON_OBJ) libfsverity.a
+$(TEST_PROGRAMS): %$(EXEEXT): programs/%.o $(PROG_COMMON_OBJ) libfsverity.a
 	$(QUIET_CCLD) $(CC) -o $@ $+ $(CFLAGS) $(LDFLAGS) $(LDLIBS)
 
 ##############################################################################
@@ -184,25 +202,25 @@ test_programs:$(TEST_PROGRAMS)
 
 # This just runs some quick, portable tests.  Use scripts/run-tests.sh if you
 # want to run the full tests.
-check:fsverity test_programs
+check:$(FSVERITY) test_programs
 	for prog in $(TEST_PROGRAMS); do \
 		$(TEST_WRAPPER_PROG) ./$$prog || exit 1; \
 	done
 	$(RUN_FSVERITY) --help > /dev/null
 	$(RUN_FSVERITY) --version > /dev/null
-	$(RUN_FSVERITY) sign fsverity fsverity.sig \
+	$(RUN_FSVERITY) sign $(FSVERITY) fsverity.sig \
 		--key=testdata/key.pem --cert=testdata/cert.pem > /dev/null
-	$(RUN_FSVERITY) sign fsverity fsverity.sig --hash=sha512 \
+	$(RUN_FSVERITY) sign $(FSVERITY) fsverity.sig --hash=sha512 \
 		--block-size=512 --salt=12345678 \
 		--key=testdata/key.pem --cert=testdata/cert.pem > /dev/null
-	$(RUN_FSVERITY) digest fsverity --hash=sha512 \
+	$(RUN_FSVERITY) digest $(FSVERITY) --hash=sha512 \
 		--block-size=512 --salt=12345678 > /dev/null
 	rm -f fsverity.sig
 	@echo "All tests passed!"
 
 install:all
 	install -d $(DESTDIR)$(LIBDIR)/pkgconfig $(DESTDIR)$(INCDIR) $(DESTDIR)$(BINDIR)
-	install -m755 fsverity $(DESTDIR)$(BINDIR)
+	install -m755 $(FSVERITY) $(DESTDIR)$(BINDIR)
 	install -m644 libfsverity.a $(DESTDIR)$(LIBDIR)
 	install -m755 libfsverity.so.$(SOVERSION) $(DESTDIR)$(LIBDIR)
 	ln -sf libfsverity.so.$(SOVERSION) $(DESTDIR)$(LIBDIR)/libfsverity.so
@@ -215,7 +233,7 @@ install:all
 	chmod 644 $(DESTDIR)$(LIBDIR)/pkgconfig/libfsverity.pc
 
 uninstall:
-	rm -f $(DESTDIR)$(BINDIR)/fsverity
+	rm -f $(DESTDIR)$(BINDIR)/$(FSVERITY)
 	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.a
 	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.so.$(SOVERSION)
 	rm -f $(DESTDIR)$(LIBDIR)/libfsverity.so
diff --git a/README.md b/README.md
index 6045c75..2b63488 100644
--- a/README.md
+++ b/README.md
@@ -50,6 +50,17 @@ use `make USE_SHARED_LIB=1` to use dynamic linking instead.
 
 See the `Makefile` for other supported build and installation options.
 
+### Building on Windows
+
+There is minimal support for building Windows executables using MinGW.
+```bash
+    make CC=x86_64-w64-mingw32-gcc
+```
+
+`fsverity.exe` will be built, and it supports the `digest` and `sign` commands.
+
+A Windows build of OpenSSL/libcrypto needs to be available.
+
 ## Examples
 
 ### Basic use
diff --git a/common/common_defs.h b/common/common_defs.h
index 279385a..3ae5561 100644
--- a/common/common_defs.h
+++ b/common/common_defs.h
@@ -15,6 +15,8 @@
 #include <stddef.h>
 #include <stdint.h>
 
+#include "win32_defs.h"
+
 typedef uint8_t u8;
 typedef uint16_t u16;
 typedef uint32_t u32;
diff --git a/common/fsverity_uapi.h b/common/fsverity_uapi.h
index 33f4415..be1d3f6 100644
--- a/common/fsverity_uapi.h
+++ b/common/fsverity_uapi.h
@@ -10,8 +10,10 @@
 #ifndef _UAPI_LINUX_FSVERITY_H
 #define _UAPI_LINUX_FSVERITY_H
 
+#ifndef _WIN32
 #include <linux/ioctl.h>
 #include <linux/types.h>
+#endif /* _WIN32 */
 
 #define FS_VERITY_HASH_ALG_SHA256	1
 #define FS_VERITY_HASH_ALG_SHA512	2
diff --git a/common/win32_defs.h b/common/win32_defs.h
new file mode 100644
index 0000000..29ef9b2
--- /dev/null
+++ b/common/win32_defs.h
@@ -0,0 +1,57 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * WIN32 compat definitions for libfsverity and the 'fsverity' program
+ *
+ * Copyright 2020 Microsoft
+ *
+ * Use of this source code is governed by an MIT-style
+ * license that can be found in the LICENSE file or at
+ * https://opensource.org/licenses/MIT.
+ */
+#ifndef COMMON_WIN32_DEFS_H
+#define COMMON_WIN32_DEFS_H
+
+/* Some minimal definitions to allow the digest/sign commands to run under Windows */
+
+/* All file reads we do need this flag on _WIN32 */
+#ifndef O_BINARY
+#  define O_BINARY 0
+#endif
+
+#ifdef _WIN32
+
+#include <stdint.h>
+#include <inttypes.h>
+
+#ifndef ENOPKG
+#   define ENOPKG 65
+#endif
+
+#ifndef __cold
+#  define __cold
+#endif
+
+/* For %zu in printf() */
+#ifndef __printf
+#  define __printf(fmt_idx, vargs_idx) \
+       __attribute__((format(gnu_printf, fmt_idx, vargs_idx)))
+#endif
+
+typedef __signed__ char __s8;
+typedef unsigned char __u8;
+typedef __signed__ short __s16;
+typedef unsigned short __u16;
+typedef __signed__ int __s32;
+typedef unsigned int __u32;
+typedef __signed__ long long  __s64;
+typedef unsigned long long  __u64;
+typedef __u16 __le16;
+typedef __u16 __be16;
+typedef __u32 __le32;
+typedef __u32 __be32;
+typedef __u64 __le64;
+typedef __u64 __be64;
+
+#endif /* _WIN32 */
+
+#endif /* COMMON_WIN32_DEFS_H */
diff --git a/lib/utils.c b/lib/utils.c
index 13e3b35..036dd60 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -51,6 +51,15 @@ libfsverity_set_error_callback(void (*cb)(const char *msg))
 	libfsverity_error_cb = cb;
 }
 
+#ifdef _WIN32
+static char *strerror_r(int errnum, char *buf, size_t buflen)
+{
+	strerror_s(buf, buflen, errnum);
+
+	return buf;
+}
+#endif
+
 void libfsverity_do_error_msg(const char *format, va_list va, int err)
 {
 	int saved_errno = errno;
diff --git a/programs/fsverity.c b/programs/fsverity.c
index 5d5fbe2..f68e034 100644
--- a/programs/fsverity.c
+++ b/programs/fsverity.c
@@ -28,6 +28,7 @@ static const struct fsverity_command {
 "    fsverity digest FILE...\n"
 "               [--hash-alg=HASH_ALG] [--block-size=BLOCK_SIZE] [--salt=SALT]\n"
 "               [--compact] [--for-builtin-sig]\n"
+#ifndef _WIN32
 	}, {
 		.name = "enable",
 		.func = fsverity_cmd_enable,
@@ -43,6 +44,7 @@ static const struct fsverity_command {
 "Display the fs-verity digest of the given verity file(s)",
 		.usage_str =
 "    fsverity measure FILE...\n"
+#endif /* _WIN32 */
 	}, {
 		.name = "sign",
 		.func = fsverity_cmd_sign,
diff --git a/programs/utils.c b/programs/utils.c
index facccda..ce19b57 100644
--- a/programs/utils.c
+++ b/programs/utils.c
@@ -102,7 +102,7 @@ void install_libfsverity_error_handler(void)
 
 bool open_file(struct filedes *file, const char *filename, int flags, int mode)
 {
-	file->fd = open(filename, flags, mode);
+	file->fd = open(filename, flags | O_BINARY, mode);
 	if (file->fd < 0) {
 		error_msg_errno("can't open '%s' for %s", filename,
 				(flags & O_ACCMODE) == O_RDONLY ? "reading" :
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* Re: [PATCH v6 3/3] Allow to build and run sign/digest on Windows
  2020-12-22  0:03               ` Eric Biggers
@ 2020-12-22  0:11                 ` Luca Boccassi
  0 siblings, 0 replies; 34+ messages in thread
From: Luca Boccassi @ 2020-12-22  0:11 UTC (permalink / raw)
  To: Eric Biggers; +Cc: linux-fscrypt, Luca Boccassi

On Tue, 22 Dec 2020 at 00:03, Eric Biggers <ebiggers@kernel.org> wrote:
>
> On Mon, Dec 21, 2020 at 11:57:41PM +0000, Luca Boccassi wrote:
> > On Mon, 21 Dec 2020 at 23:53, Eric Biggers <ebiggers@kernel.org> wrote:
> > >
> > > On Mon, Dec 21, 2020 at 11:24:28PM +0000, Luca Boccassi wrote:
> > > > +### Building on Windows
> > > > +
> > > > +There is minimal support for building Windows executables using MinGW.
> > > > +```bash
> > > > +    make CC=x86_64-w64-mingw32-gcc-win32
> > > > +```
> > > > +
> > > > +`fsverity.exe` will be built, and it supports the `digest` and `sign` commands.
> > > > +
> > > > +A Windows build of OpenSSL/libcrypto needs to be available.
> > >
> > > For me "CC=x86_64-w64-mingw32-gcc-win32" doesn't work; I need
> > > "x86_64-w64-mingw32-gcc" instead.  Is this difference intentional?
> > >
> > > - Eric
> >
> > It's a distro setup difference I think, on Debian
> > x86_64-w64-mingw32-gcc is a symlink to x86_64-w64-mingw32-gcc-win32:
> >
> > $ ls -l /usr/bin/x86_64-w64-mingw32-gcc-win32
> > -rwxr-xr-x 2 root root 1160320 Nov 27 05:57
> > /usr/bin/x86_64-w64-mingw32-gcc-win32
> > $ ls -l /usr/bin/x86_64-w64-mingw32-gcc
> > lrwxrwxrwx 1 root root 40 Sep 27 18:41 /usr/bin/x86_64-w64-mingw32-gcc
> > -> /etc/alternatives/x86_64-w64-mingw32-gcc
> > $ ls -l /etc/alternatives/x86_64-w64-mingw32-gcc
> > lrwxrwxrwx 1 root root 37 Sep 27 18:44
> > /etc/alternatives/x86_64-w64-mingw32-gcc ->
> > /usr/bin/x86_64-w64-mingw32-gcc-win32
>
> Okay, it would be better to document the one that works on all distros.
>
> - Eric

Adjusted in v7.

Kind regards,
Luca Boccassi

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH v6 1/3] Move -D_GNU_SOURCE to CPPFLAGS
  2020-12-22  0:00         ` [PATCH v6 1/3] Move -D_GNU_SOURCE to CPPFLAGS Eric Biggers
@ 2020-12-22  0:12           ` Luca Boccassi
  0 siblings, 0 replies; 34+ messages in thread
From: Luca Boccassi @ 2020-12-22  0:12 UTC (permalink / raw)
  To: Eric Biggers; +Cc: linux-fscrypt

On Tue, 22 Dec 2020 at 00:00, Eric Biggers <ebiggers@kernel.org> wrote:
>
> On Mon, Dec 21, 2020 at 11:24:26PM +0000, Luca Boccassi wrote:
> > Ensures it is actually defined before any include is preprocessed.
>
> It was already at the beginning of the .c file, so this isn't a very good
> explanation.  A better explanation would be "Use _GNU_SOURCE consistently in
> every file rather than in just one file.  This is needed for the Windows build
> in order to consistently get the MinGW version of printf.".

Ok, copied verbatim in v7.

> > diff --git a/Makefile b/Makefile
> > index bfe83c4..f1ba956 100644
> > --- a/Makefile
> > +++ b/Makefile
> > @@ -47,7 +47,7 @@ override CFLAGS := -Wall -Wundef                            \
> >       $(call cc-option,-Wvla)                                 \
> >       $(CFLAGS)
> >
> > -override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 $(CPPFLAGS)
> > +override CPPFLAGS := -Iinclude -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(CPPFLAGS)
> >
> >  ifneq ($(V),1)
> >  QUIET_CC        = @echo '  CC      ' $@;
>
> Can you add -D_GNU_SOURCE to ./scripts/run-sparse.sh too?
> Otherwise I get errors when running scripts/run-tests.sh:
>
> [Mon Dec 21 03:52:15 PM PST 2020] Run sparse
> ./lib/utils.c:71:13: error: undefined identifier 'vasprintf'
> ./lib/utils.c:78:21: error: undefined identifier 'asprintf'

Sure, done in v7.

Kind regards,
Luca Boccassi

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH v7 1/3] Move -D_GNU_SOURCE to CPPFLAGS
  2020-12-22  0:10         ` [PATCH v7 " Luca Boccassi
  2020-12-22  0:10           ` [PATCH v7 2/3] Wrap ./fsverity in TEST_WRAPPER_PROG too Luca Boccassi
  2020-12-22  0:10           ` [PATCH v7 3/3] Allow to build and run sign/digest on Windows Luca Boccassi
@ 2020-12-22  8:21           ` Eric Biggers
  2020-12-22 18:40           ` Eric Biggers
  3 siblings, 0 replies; 34+ messages in thread
From: Eric Biggers @ 2020-12-22  8:21 UTC (permalink / raw)
  To: Luca Boccassi; +Cc: linux-fscrypt

On Tue, Dec 22, 2020 at 12:10:31AM +0000, Luca Boccassi wrote:
> Use _GNU_SOURCE consistently in every file rather than just one file.
> This is needed for the Windows build in order to consistently get the MinGW
> version of printf.
> 
> Signed-off-by: Luca Boccassi <bluca@debian.org>

Is this the email address you wanted to use in the Author and Signed-off-by?
v5 and earlier (and your other patches) had "luca.boccassi@microsoft.com".

- Eric

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH v7 1/3] Move -D_GNU_SOURCE to CPPFLAGS
  2020-12-22  0:10         ` [PATCH v7 " Luca Boccassi
                             ` (2 preceding siblings ...)
  2020-12-22  8:21           ` [PATCH v7 1/3] Move -D_GNU_SOURCE to CPPFLAGS Eric Biggers
@ 2020-12-22 18:40           ` Eric Biggers
  3 siblings, 0 replies; 34+ messages in thread
From: Eric Biggers @ 2020-12-22 18:40 UTC (permalink / raw)
  To: Luca Boccassi; +Cc: linux-fscrypt

On Tue, Dec 22, 2020 at 12:10:31AM +0000, Luca Boccassi wrote:
> Use _GNU_SOURCE consistently in every file rather than just one file.
> This is needed for the Windows build in order to consistently get the MinGW
> version of printf.
> 
> Signed-off-by: Luca Boccassi <bluca@debian.org>
> ---
> v6: split from mingw patch
> 
> v7: adjust commit message and add CPPFLAG to run-sparse.sh as well
> 
>  Makefile              | 2 +-
>  lib/utils.c           | 2 --
>  scripts/run-sparse.sh | 2 +-
>  3 files changed, 2 insertions(+), 4 deletions(-)
> 

Applied, thanks.

- Eric

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH v7 3/3] Allow to build and run sign/digest on Windows
  2020-12-22  0:10           ` [PATCH v7 3/3] Allow to build and run sign/digest on Windows Luca Boccassi
@ 2020-12-22 18:40             ` Eric Biggers
  0 siblings, 0 replies; 34+ messages in thread
From: Eric Biggers @ 2020-12-22 18:40 UTC (permalink / raw)
  To: Luca Boccassi; +Cc: linux-fscrypt

On Tue, Dec 22, 2020 at 12:10:33AM +0000, Luca Boccassi wrote:
> Add some minimal compat type defs, and omit the enable/measure
> sources. Also add a way to handle the fact that mingw adds a
> .exe extension automatically in the Makefile install rules.
> 
> Signed-off-by: Luca Boccassi <luca.boccassi@microsoft.com>

Applied, thanks.

- Eric

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH v7 2/3] Wrap ./fsverity in TEST_WRAPPER_PROG too
  2020-12-22  0:10           ` [PATCH v7 2/3] Wrap ./fsverity in TEST_WRAPPER_PROG too Luca Boccassi
@ 2020-12-22 18:41             ` Eric Biggers
  0 siblings, 0 replies; 34+ messages in thread
From: Eric Biggers @ 2020-12-22 18:41 UTC (permalink / raw)
  To: Luca Boccassi; +Cc: linux-fscrypt

On Tue, Dec 22, 2020 at 12:10:32AM +0000, Luca Boccassi wrote:
> Allows make check to run fsverity under the desired tool
> 
> Signed-off-by: Luca Boccassi <bluca@debian.org>
> ---
> v6: split from mingw patch
> 
>  Makefile | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)

Applied, thanks.

- Eric

^ permalink raw reply	[flat|nested] 34+ messages in thread

end of thread, other threads:[~2020-12-22 18:41 UTC | newest]

Thread overview: 34+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-17 14:47 [fsverity-utils PATCH v2 1/2] Remove unneeded includes luca.boccassi
2020-12-17 14:47 ` [fsverity-utils PATCH v2 2/2] Allow to build and run sign/digest on Windows luca.boccassi
2020-12-17 18:32   ` Eric Biggers
2020-12-17 18:44     ` Luca Boccassi
2020-12-17 19:05       ` Eric Biggers
2020-12-17 19:12         ` Luca Boccassi
2020-12-17 19:20           ` Eric Biggers
2020-12-17 19:26             ` Luca Boccassi
2020-12-17 19:16 ` [fsverity-utils PATCH v3 1/2] Remove unneeded includes luca.boccassi
2020-12-17 19:16   ` [fsverity-utils PATCH v3 2/2] Allow to build and run sign/digest on Windows luca.boccassi
2020-12-17 19:25 ` [fsverity-utils PATCH v4 1/2] Remove unneeded includes luca.boccassi
2020-12-17 19:25   ` [fsverity-utils PATCH v4 2/2] Allow to build and run sign/digest on Windows luca.boccassi
2020-12-21 21:40     ` Eric Biggers
2020-12-21 22:23       ` Luca Boccassi
2020-12-21 22:19     ` [PATCH v5] " Luca Boccassi
2020-12-21 23:03       ` Eric Biggers
2020-12-21 23:26         ` Luca Boccassi
2020-12-21 23:24       ` [PATCH v6 1/3] Move -D_GNU_SOURCE to CPPFLAGS Luca Boccassi
2020-12-21 23:24         ` [PATCH v6 2/3] Wrap ./fsverity in TEST_WRAPPER_PROG too Luca Boccassi
2020-12-21 23:24         ` [PATCH v6 3/3] Allow to build and run sign/digest on Windows Luca Boccassi
2020-12-21 23:53           ` Eric Biggers
2020-12-21 23:57             ` Luca Boccassi
2020-12-22  0:03               ` Eric Biggers
2020-12-22  0:11                 ` Luca Boccassi
2020-12-22  0:00         ` [PATCH v6 1/3] Move -D_GNU_SOURCE to CPPFLAGS Eric Biggers
2020-12-22  0:12           ` Luca Boccassi
2020-12-22  0:10         ` [PATCH v7 " Luca Boccassi
2020-12-22  0:10           ` [PATCH v7 2/3] Wrap ./fsverity in TEST_WRAPPER_PROG too Luca Boccassi
2020-12-22 18:41             ` Eric Biggers
2020-12-22  0:10           ` [PATCH v7 3/3] Allow to build and run sign/digest on Windows Luca Boccassi
2020-12-22 18:40             ` Eric Biggers
2020-12-22  8:21           ` [PATCH v7 1/3] Move -D_GNU_SOURCE to CPPFLAGS Eric Biggers
2020-12-22 18:40           ` Eric Biggers
2020-12-21 21:32   ` [fsverity-utils PATCH v4 1/2] Remove unneeded includes Eric Biggers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).