Skip to content

Fixed ringtone saving - #197

Merged
nijel merged 16 commits into
masterfrom
alert-autofix-25
Feb 5, 2026
Merged

Fixed ringtone saving#197
nijel merged 16 commits into
masterfrom
alert-autofix-25

Conversation

@nijel

@nijel nijel commented Feb 5, 2026

Copy link
Copy Markdown
Member

Potential fix for https://github.com/gammu/python-gammu/security/code-scanning/25

In general, to avoid creating world-writable files you should not rely on the process umask. Instead, when creating new files, use APIs that accept an explicit mode (such as open with O_CREAT) and specify restrictive permissions, typically S_IRUSR | S_IWUSR (read/write only for the owner). If you need a FILE * stream, you can then wrap the file descriptor with fdopen.

For this specific case in gammu/src/gammu.c, the problematic path is when value is a Unicode string and the code calls f = fopen(name, "wb");. Replace this with a sequence that:

  1. Opens the file descriptor with open(name, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);.
  2. Wraps it in a FILE * using fdopen(fd, "wb");.
  3. Handles errors by closing the descriptor on failure and setting the same Python exception as today.

We also need to ensure the required headers are present. The file currently includes standard C and project headers, but not the POSIX ones for open and permission constants. Add #include <fcntl.h> and #include <sys/stat.h> near the other system includes at the top of the file. This preserves existing functionality: the file is still opened for binary write and truncated/created as before, but with explicitly owner-only read/write permissions.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request attempts to fix a code scanning alert about file creation without restricting permissions by replacing fopen() with POSIX open() + fdopen() to explicitly set file permissions to owner-only read/write.

Changes:

  • Added includes for <fcntl.h> and <sys/stat.h> for POSIX file creation APIs
  • Modified file opening logic in gammu_SaveRingtone() to use open() with explicit permissions (S_IRUSR | S_IWUSR) instead of fopen()
  • Added proper error handling for the file descriptor and cleanup on failure

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread gammu/src/gammu.c
Comment on lines +33 to +35
/* File creation and permissions */
#include <fcntl.h>
#include <sys/stat.h>

Copilot AI Feb 5, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The headers <fcntl.h> and <sys/stat.h> are POSIX headers that are not available on Windows. Since this project supports Windows builds (as evidenced by the CI workflows), these includes should be wrapped in platform-specific guards or replaced with Windows-compatible alternatives.

For Windows compatibility, consider using <io.h> and <sys/stat.h> (Windows has a limited version) with appropriate #ifdef _WIN32 guards.

Suggested change
/* File creation and permissions */
#include <fcntl.h>
#include <sys/stat.h>
/* File creation and permissions */
#ifdef _WIN32
#include <io.h>
#include <sys/stat.h>
#else
#include <fcntl.h>
#include <sys/stat.h>
#endif

Copilot uses AI. Check for mistakes.
Comment thread gammu/src/gammu.c
Comment on lines +6100 to +6108
ofd = open(name, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
Py_DECREF(str);
if (ofd == -1) {
PyErr_SetString(PyExc_IOError, "Can not open file for writing!");
return NULL;
}
f = fdopen(ofd, "wb");
if (f == NULL) {
close(ofd);

Copilot AI Feb 5, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The POSIX open() function and the permission constants S_IRUSR and S_IWUSR are not directly available on Windows. This code will fail to compile on Windows, which is a supported platform according to the CI configuration in .github/workflows/test.yml and .github/workflows/setup.yml.

On Windows, you should either:

  1. Use _open() with _S_IREAD | _S_IWRITE and include <io.h>, or
  2. Add platform-specific compilation guards with #ifdef _WIN32 to use different approaches for Windows vs POSIX systems, or
  3. Use a cross-platform abstraction.

The same issue applies to the close() function at line 6108, which should be _close() on Windows.

Copilot uses AI. Check for mistakes.
Comment thread gammu/src/gammu.c Outdated
Comment on lines 6077 to 6088
@@ -6082,15 +6086,26 @@ gammu_SaveRingtone(PyObject *self, PyObject *args, PyObject *kwds)
if (f == NULL) return NULL;
closefile = TRUE;
} else if (PyUnicode_Check(value)) {

Copilot AI Feb 5, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function parameter parsing uses &file to receive the first argument "Filename" (line 6070), but then the code checks PyUnicode_Check(value) and PyObject_AsFileDescriptor(value) instead of checking file. The variable value contains the second parameter "Ringtone" (a dictionary), not the filename.

This means the file opening logic at line 6100 will never be reached for string filenames, because value is always a dictionary (enforced by the PyDict_Type check in line 6070). The code should be checking and using the file variable instead of value for file descriptor and Unicode string checks.

Copilot uses AI. Check for mistakes.
@nijel

nijel commented Feb 5, 2026

Copy link
Copy Markdown
Member Author

@copilot Please address review comments and fix compilation on Windows.

Copilot AI commented Feb 5, 2026

Copy link
Copy Markdown
Contributor

@nijel I've opened a new pull request, #198, to work on those changes. Once the pull request is ready, I'll request review from you.

Copilot AI and others added 13 commits February 5, 2026 09:36
Co-authored-by: nijel <212189+nijel@users.noreply.github.com>
Co-authored-by: nijel <212189+nijel@users.noreply.github.com>
…ssue

Co-authored-by: nijel <212189+nijel@users.noreply.github.com>
Co-authored-by: nijel <212189+nijel@users.noreply.github.com>
…le creation

Co-authored-by: nijel <212189+nijel@users.noreply.github.com>
Co-authored-by: nijel <212189+nijel@users.noreply.github.com>
Co-authored-by: nijel <212189+nijel@users.noreply.github.com>
Co-authored-by: nijel <212189+nijel@users.noreply.github.com>
Fix parameter validation and Windows compilation in SaveRingtone security patch
@nijel nijel changed the title Potential fix for code scanning alert no. 25: File created without restricting permissions Fixed ringtone saving Feb 5, 2026
@nijel
nijel marked this pull request as ready for review February 5, 2026 12:55
@nijel
nijel enabled auto-merge (squash) February 5, 2026 12:56
@nijel
nijel merged commit d5366c8 into master Feb 5, 2026
41 checks passed
@nijel
nijel deleted the alert-autofix-25 branch February 5, 2026 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants