| 1 | // Copyright (C) 2015 Konstantin Ritt <ritt.ks@gmail.com> |
| 2 | // Copyright (C) 2016 The Qt Company Ltd. |
| 3 | // Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Tobias Koenig <tobias.koenig@kdab.com> |
| 4 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 5 | // Qt-Security score:significant reason:default |
| 6 | |
| 7 | #include "qsharedmemory.h" |
| 8 | #include "qsharedmemory_p.h" |
| 9 | #include "qtipccommon_p.h" |
| 10 | #include <qfile.h> |
| 11 | |
| 12 | #include <errno.h> |
| 13 | |
| 14 | #if QT_CONFIG(sharedmemory) |
| 15 | #if QT_CONFIG(posix_shm) |
| 16 | #include <sys/types.h> |
| 17 | #include <sys/mman.h> |
| 18 | #include <sys/stat.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <unistd.h> |
| 21 | |
| 22 | #include "private/qcore_unix_p.h" |
| 23 | |
| 24 | #ifndef O_CLOEXEC |
| 25 | # define O_CLOEXEC 0 |
| 26 | #endif |
| 27 | |
| 28 | QT_BEGIN_NAMESPACE |
| 29 | |
| 30 | using namespace Qt::StringLiterals; |
| 31 | using namespace QtIpcCommon; |
| 32 | |
| 33 | bool QSharedMemoryPosix::runtimeSupportCheck() |
| 34 | { |
| 35 | static const bool result = []() { |
| 36 | (void)shm_open(name: "" , oflag: 0, mode: 0); // this WILL fail |
| 37 | return errno != ENOSYS; |
| 38 | }(); |
| 39 | return result; |
| 40 | } |
| 41 | |
| 42 | bool QSharedMemoryPosix::handle(QSharedMemoryPrivate *self) |
| 43 | { |
| 44 | // don't allow making handles on empty keys |
| 45 | if (self->nativeKey.isEmpty()) { |
| 46 | self->setError(e: QSharedMemory::KeyError, |
| 47 | message: QSharedMemory::tr(s: "%1: key is empty" ).arg(a: "QSharedMemory::handle"_L1 )); |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | bool QSharedMemoryPosix::cleanHandle(QSharedMemoryPrivate *) |
| 55 | { |
| 56 | if (hand != -1) |
| 57 | qt_safe_close(fd: hand); |
| 58 | hand = -1; |
| 59 | |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | bool QSharedMemoryPosix::create(QSharedMemoryPrivate *self, qsizetype size) |
| 64 | { |
| 65 | if (!handle(self)) |
| 66 | return false; |
| 67 | |
| 68 | const QByteArray shmName = QFile::encodeName(fileName: self->nativeKey.nativeKey()); |
| 69 | |
| 70 | int fd; |
| 71 | QT_EINTR_LOOP(fd, ::shm_open(shmName.constData(), O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0600)); |
| 72 | if (fd == -1) { |
| 73 | const int errorNumber = errno; |
| 74 | const auto function = "QSharedMemory::attach (shm_open)"_L1 ; |
| 75 | switch (errorNumber) { |
| 76 | case EINVAL: |
| 77 | self->setError(e: QSharedMemory::KeyError, |
| 78 | message: QSharedMemory::tr(s: "%1: bad name" ).arg(a: function)); |
| 79 | break; |
| 80 | default: |
| 81 | self->setUnixErrorString(function); |
| 82 | } |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | // the size may only be set once |
| 87 | int ret; |
| 88 | QT_EINTR_LOOP(ret, QT_FTRUNCATE(fd, size)); |
| 89 | if (ret == -1) { |
| 90 | self->setUnixErrorString("QSharedMemory::create (ftruncate)"_L1 ); |
| 91 | qt_safe_close(fd); |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | qt_safe_close(fd); |
| 96 | |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | bool QSharedMemoryPosix::attach(QSharedMemoryPrivate *self, QSharedMemory::AccessMode mode) |
| 101 | { |
| 102 | const QByteArray shmName = QFile::encodeName(fileName: self->nativeKey.nativeKey()); |
| 103 | |
| 104 | const int oflag = (mode == QSharedMemory::ReadOnly ? O_RDONLY : O_RDWR); |
| 105 | const mode_t omode = (mode == QSharedMemory::ReadOnly ? 0400 : 0600); |
| 106 | |
| 107 | QT_EINTR_LOOP(hand, ::shm_open(shmName.constData(), oflag | O_CLOEXEC, omode)); |
| 108 | if (hand == -1) { |
| 109 | const int errorNumber = errno; |
| 110 | const auto function = "QSharedMemory::attach (shm_open)"_L1 ; |
| 111 | switch (errorNumber) { |
| 112 | case EINVAL: |
| 113 | self->setError(e: QSharedMemory::KeyError, |
| 114 | message: QSharedMemory::tr(s: "%1: bad name" ).arg(a: function)); |
| 115 | break; |
| 116 | default: |
| 117 | self->setUnixErrorString(function); |
| 118 | } |
| 119 | hand = -1; |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | // grab the size |
| 124 | QT_STATBUF st; |
| 125 | if (QT_FSTAT(fd: hand, buf: &st) == -1) { |
| 126 | self->setUnixErrorString("QSharedMemory::attach (fstat)"_L1 ); |
| 127 | cleanHandle(self); |
| 128 | return false; |
| 129 | } |
| 130 | self->size = qsizetype(st.st_size); |
| 131 | |
| 132 | // grab the memory |
| 133 | const int mprot = (mode == QSharedMemory::ReadOnly ? PROT_READ : PROT_READ | PROT_WRITE); |
| 134 | self->memory = QT_MMAP(addr: 0, len: size_t(self->size), prot: mprot, MAP_SHARED, fd: hand, offset: 0); |
| 135 | if (self->memory == MAP_FAILED || !self->memory) { |
| 136 | self->setUnixErrorString("QSharedMemory::attach (mmap)"_L1 ); |
| 137 | cleanHandle(self); |
| 138 | self->memory = 0; |
| 139 | self->size = 0; |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | #ifdef F_ADD_SEALS |
| 144 | // Make sure the shared memory region will not shrink |
| 145 | // otherwise someone could cause SIGBUS on us. |
| 146 | // (see http://lwn.net/Articles/594919/) |
| 147 | fcntl(fd: hand, F_ADD_SEALS, F_SEAL_SHRINK); |
| 148 | #endif |
| 149 | |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | bool QSharedMemoryPosix::detach(QSharedMemoryPrivate *self) |
| 154 | { |
| 155 | // detach from the memory segment |
| 156 | if (::munmap(addr: self->memory, len: size_t(self->size)) == -1) { |
| 157 | self->setUnixErrorString("QSharedMemory::detach (munmap)"_L1 ); |
| 158 | return false; |
| 159 | } |
| 160 | self->memory = 0; |
| 161 | self->size = 0; |
| 162 | |
| 163 | #ifdef Q_OS_QNX |
| 164 | // On QNX the st_nlink field of struct stat contains the number of |
| 165 | // active shm_open() connections to the shared memory file, so we |
| 166 | // can use it to automatically clean up the file once the last |
| 167 | // user has detached from it. |
| 168 | |
| 169 | // get the number of current attachments |
| 170 | int shm_nattch = 0; |
| 171 | QT_STATBUF st; |
| 172 | if (QT_FSTAT(hand, &st) == 0) { |
| 173 | // subtract 2 from linkcount: one for our own open and one for the dir entry |
| 174 | shm_nattch = st.st_nlink - 2; |
| 175 | } |
| 176 | |
| 177 | cleanHandle(self); |
| 178 | |
| 179 | // if there are no attachments then unlink the shared memory |
| 180 | if (shm_nattch == 0) { |
| 181 | const QByteArray shmName = QFile::encodeName(self->nativeKey.nativeKey()); |
| 182 | if (::shm_unlink(shmName.constData()) == -1 && errno != ENOENT) |
| 183 | self->setUnixErrorString("QSharedMemory::detach (shm_unlink)"_L1 ); |
| 184 | } |
| 185 | #else |
| 186 | // On non-QNX systems (tested Linux and Haiku), the st_nlink field is always 1, |
| 187 | // so we'll simply leak the shared memory files. |
| 188 | cleanHandle(self); |
| 189 | #endif |
| 190 | |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | QT_END_NAMESPACE |
| 195 | |
| 196 | #endif // QT_CONFIG(posix_shm) |
| 197 | #endif // QT_CONFIG(sharedmemory) |
| 198 | |