Skip to content

Commit 0f52649

Browse files
authored
[clang] Reject character devices in #embed for now (#135370)
See #126629 . Right now they are not supported properly and support requires modifying several layers of LLVM. For now simply reject them while proper support is being developed to avoid potential security problems.
1 parent 17b4cac commit 0f52649

File tree

7 files changed

+42
-0
lines changed

7 files changed

+42
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,9 @@ Bug Fixes in This Version
398398
399399
#if 1 ? 1 : 999999999999999999999
400400
#endif
401+
- ``#embed`` directive now diagnoses use of a non-character file (device file)
402+
such as ``/dev/urandom`` as an error. This restriction may be relaxed in the
403+
future. See (#GH126629).
401404

402405
Bug Fixes to Compiler Builtins
403406
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/include/clang/Basic/DiagnosticLexKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,8 @@ def warn_compat_pp_embed_directive : Warning<
458458
InGroup<CPre23Compat>, DefaultIgnore;
459459
def err_pp_embed_dup_params : Error<
460460
"cannot specify parameter '%0' twice in the same '#embed' directive">;
461+
def err_pp_embed_device_file : Error<
462+
"device files are not yet supported by '#embed' directive">;
461463

462464
def ext_pp_extra_tokens_at_eol : ExtWarn<
463465
"extra tokens at end of #%0 directive">, InGroup<ExtraTokens>;

clang/include/clang/Basic/FileEntry.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class FileEntryRef {
8282
inline const llvm::sys::fs::UniqueID &getUniqueID() const;
8383
inline time_t getModificationTime() const;
8484
inline bool isNamedPipe() const;
85+
inline bool isDeviceFile() const;
8586
inline void closeFile() const;
8687

8788
/// Check if the underlying FileEntry is the same, intentially ignoring
@@ -316,6 +317,7 @@ class FileEntry {
316317
llvm::sys::fs::UniqueID UniqueID;
317318
unsigned UID = 0; // A unique (small) ID for the file.
318319
bool IsNamedPipe = false;
320+
bool IsDeviceFile = false;
319321

320322
/// The open file, if it is owned by the \p FileEntry.
321323
mutable std::unique_ptr<llvm::vfs::File> File;
@@ -340,6 +342,7 @@ class FileEntry {
340342
/// Check whether the file is a named pipe (and thus can't be opened by
341343
/// the native FileManager methods).
342344
bool isNamedPipe() const { return IsNamedPipe; }
345+
bool isDeviceFile() const { return IsDeviceFile; }
343346

344347
void closeFile() const;
345348
};
@@ -357,6 +360,9 @@ time_t FileEntryRef::getModificationTime() const {
357360
}
358361

359362
bool FileEntryRef::isNamedPipe() const { return getFileEntry().isNamedPipe(); }
363+
bool FileEntryRef::isDeviceFile() const {
364+
return getFileEntry().isDeviceFile();
365+
}
360366

361367
void FileEntryRef::closeFile() const { getFileEntry().closeFile(); }
362368

clang/lib/Basic/FileManager.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ llvm::Expected<FileEntryRef> FileManager::getFileRef(StringRef Filename,
330330
UFE->UID = NextFileUID++;
331331
UFE->UniqueID = Status.getUniqueID();
332332
UFE->IsNamedPipe = Status.getType() == llvm::sys::fs::file_type::fifo_file;
333+
UFE->IsDeviceFile =
334+
Status.getType() == llvm::sys::fs::file_type::character_file;
333335
UFE->File = std::move(F);
334336

335337
if (UFE->File) {

clang/lib/Lex/PPDirectives.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4012,6 +4012,12 @@ void Preprocessor::HandleEmbedDirective(SourceLocation HashLoc, Token &EmbedTok,
40124012
Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
40134013
return;
40144014
}
4015+
4016+
if (MaybeFileRef->isDeviceFile()) {
4017+
Diag(FilenameTok, diag::err_pp_embed_device_file) << Filename;
4018+
return;
4019+
}
4020+
40154021
std::optional<llvm::MemoryBufferRef> MaybeFile =
40164022
getSourceManager().getMemoryBufferForFileOrNone(*MaybeFileRef);
40174023
if (!MaybeFile) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %clang_cc1 -std=c23 %s -fsyntax-only -verify
2+
// REQUIRES: system-linux
3+
4+
5+
int urandom[] = {
6+
#embed "/dev/urandom" //expected-error {{device files are not yet supported by '#embed' directive}}
7+
};
8+
int random[] = {
9+
#embed "/dev/random" //expected-error {{device files are not yet supported by '#embed' directive}}
10+
};
11+
int zero[] = {
12+
#embed "/dev/zero" //expected-error {{device files are not yet supported by '#embed' directive}}
13+
};
14+
int null[] = {
15+
#embed "/dev/null" //expected-error {{device files are not yet supported by '#embed' directive}}
16+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %clang_cc1 -std=c23 %s -fsyntax-only -verify
2+
// REQUIRES: system-windows
3+
4+
5+
int null[] = {
6+
#embed "NUL" limit(1) //expected-error {{device files are not yet supported by '#embed' directive}}
7+
};

0 commit comments

Comments
 (0)