From ac0184d6085d9e4f7f59352e563055311f4d8792 Mon Sep 17 00:00:00 2001 From: Louai Al-Khanji Date: Mon, 21 Sep 2015 15:39:16 +0300 Subject: Add qt_safe_ftok wrapper for ftok The ftok function unfortunately can return duplicate keys even for different files if the same project id is used. This is discussed e.g. in Stevens' "Advanced Programming in the UNIX Environment". We want the key to be predictable so we cannot just pass a random number as the project id. To reduce the propability of key collisions we hash the file name with the project number as seed for a predictable value. This is the same approach taken e.g. by Apache, but the real fix is to move away from System V IPC completely once this is feasible on our supported platforms. Task-number: QTBUG-48375 Change-Id: If1a57f215f7ddd147aa38919907cfb83db07aea0 Reviewed-by: Thiago Macieira --- src/corelib/kernel/qcore_unix_p.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/corelib/kernel/qcore_unix_p.h') diff --git a/src/corelib/kernel/qcore_unix_p.h b/src/corelib/kernel/qcore_unix_p.h index c744873fce..f80dcb5a50 100644 --- a/src/corelib/kernel/qcore_unix_p.h +++ b/src/corelib/kernel/qcore_unix_p.h @@ -47,6 +47,7 @@ #include "qplatformdefs.h" #include "qatomic.h" +#include "qhash.h" #ifndef Q_OS_UNIX # error "qcore_unix_p.h included on a non-Unix system" @@ -322,6 +323,19 @@ union qt_semun { unsigned short *array; /* array for GETALL, SETALL */ }; +#ifndef QT_POSIX_IPC +#ifndef QT_NO_SHAREDMEMORY +#ifndef Q_OS_ANDROID +static inline key_t qt_safe_ftok(const QByteArray &filename, int proj_id) +{ + // Unfortunately ftok can return colliding keys even for different files. + // Try to add some more entropy via qHash. + return ::ftok(filename.constData(), qHash(filename, proj_id)); +} +#endif // !Q_OS_ANDROID +#endif // !QT_NO_SHAREDMEMORY +#endif // !QT_POSIX_IPC + QT_END_NAMESPACE #endif -- cgit v1.2.3