This source file includes following definitions.
- sys_ni_syscall
- proc_sel
- sys_setpriority
- sys_getpriority
- sys_profil
- sys_ftime
- sys_break
- sys_stty
- sys_gtty
- sys_prof
- sys_reboot
- ctrl_alt_del
- sys_setregid
- sys_setgid
- sys_acct
- sys_phys
- sys_lock
- sys_mpx
- sys_ulimit
- sys_old_syscall
- sys_setreuid
- sys_setuid
- sys_setfsuid
- sys_setfsgid
- sys_times
- sys_brk
- sys_setpgid
- sys_getpgid
- sys_getpgrp
- sys_getsid
- sys_setsid
- sys_getgroups
- sys_setgroups
- in_group_p
- sys_newuname
- sys_uname
- sys_olduname
- sys_sethostname
- sys_gethostname
- sys_setdomainname
- sys_getrlimit
- sys_setrlimit
- getrusage
- sys_getrusage
- sys_umask
1
2
3
4
5
6
7 #include <linux/errno.h>
8 #include <linux/sched.h>
9 #include <linux/kernel.h>
10 #include <linux/times.h>
11 #include <linux/utsname.h>
12 #include <linux/param.h>
13 #include <linux/resource.h>
14 #include <linux/signal.h>
15 #include <linux/string.h>
16 #include <linux/ptrace.h>
17 #include <linux/stat.h>
18 #include <linux/mman.h>
19 #include <linux/mm.h>
20
21 #include <asm/segment.h>
22 #include <asm/io.h>
23
24
25
26
27 static int C_A_D = 1;
28
29 extern void adjust_clock(void);
30
31 asmlinkage int sys_ni_syscall(void)
32 {
33 return -ENOSYS;
34 }
35
36 static int proc_sel(struct task_struct *p, int which, int who)
37 {
38 switch (which) {
39 case PRIO_PROCESS:
40 if (!who && p == current)
41 return 1;
42 return(p->pid == who);
43 case PRIO_PGRP:
44 if (!who)
45 who = current->pgrp;
46 return(p->pgrp == who);
47 case PRIO_USER:
48 if (!who)
49 who = current->uid;
50 return(p->uid == who);
51 }
52 return 0;
53 }
54
55 asmlinkage int sys_setpriority(int which, int who, int niceval)
56 {
57 struct task_struct *p;
58 int error = ESRCH;
59 unsigned int priority;
60
61 if (which > 2 || which < 0)
62 return -EINVAL;
63
64
65 priority = niceval;
66 if (niceval < 0)
67 priority = -niceval;
68 if (priority > 20)
69 priority = 20;
70 priority = (priority * DEF_PRIORITY + 10) / 20 + DEF_PRIORITY;
71
72 if (niceval >= 0) {
73 priority = 2*DEF_PRIORITY - priority;
74 if (!priority)
75 priority = 1;
76 }
77
78 for_each_task(p) {
79 if (!proc_sel(p, which, who))
80 continue;
81 if (p->uid != current->euid &&
82 p->uid != current->uid && !suser()) {
83 error = EPERM;
84 continue;
85 }
86 if (error == ESRCH)
87 error = 0;
88 if (priority > p->priority && !suser())
89 error = EACCES;
90 else
91 p->priority = priority;
92 }
93 return -error;
94 }
95
96
97
98
99
100
101 asmlinkage int sys_getpriority(int which, int who)
102 {
103 struct task_struct *p;
104 long max_prio = -ESRCH;
105
106 if (which > 2 || which < 0)
107 return -EINVAL;
108
109 for_each_task (p) {
110 if (!proc_sel(p, which, who))
111 continue;
112 if (p->priority > max_prio)
113 max_prio = p->priority;
114 }
115
116
117 if (max_prio > 0)
118 max_prio = (max_prio * 20 + DEF_PRIORITY/2) / DEF_PRIORITY;
119 return max_prio;
120 }
121
122 asmlinkage int sys_profil(void)
123 {
124 return -ENOSYS;
125 }
126
127 asmlinkage int sys_ftime(void)
128 {
129 return -ENOSYS;
130 }
131
132 asmlinkage int sys_break(void)
133 {
134 return -ENOSYS;
135 }
136
137 asmlinkage int sys_stty(void)
138 {
139 return -ENOSYS;
140 }
141
142 asmlinkage int sys_gtty(void)
143 {
144 return -ENOSYS;
145 }
146
147 asmlinkage int sys_prof(void)
148 {
149 return -ENOSYS;
150 }
151
152 extern void hard_reset_now(void);
153 extern asmlinkage sys_kill(int, int);
154
155
156
157
158
159
160
161
162
163 asmlinkage int sys_reboot(int magic, int magic_too, int flag)
164 {
165 if (!suser())
166 return -EPERM;
167 if (magic != 0xfee1dead || magic_too != 672274793)
168 return -EINVAL;
169 if (flag == 0x01234567)
170 hard_reset_now();
171 else if (flag == 0x89ABCDEF)
172 C_A_D = 1;
173 else if (!flag)
174 C_A_D = 0;
175 else if (flag == 0xCDEF0123) {
176 printk(KERN_EMERG "System halted\n");
177 sys_kill(-1, SIGKILL);
178 do_exit(0);
179 } else
180 return -EINVAL;
181 return (0);
182 }
183
184
185
186
187
188
189 void ctrl_alt_del(void)
190 {
191 if (C_A_D)
192 hard_reset_now();
193 else
194 kill_proc(1, SIGINT, 1);
195 }
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213 asmlinkage int sys_setregid(gid_t rgid, gid_t egid)
214 {
215 int old_rgid = current->gid;
216 int old_egid = current->egid;
217
218 if (rgid != (gid_t) -1) {
219 if ((old_rgid == rgid) ||
220 (current->egid==rgid) ||
221 suser())
222 current->gid = rgid;
223 else
224 return(-EPERM);
225 }
226 if (egid != (gid_t) -1) {
227 if ((old_rgid == egid) ||
228 (current->egid == egid) ||
229 (current->sgid == egid) ||
230 suser())
231 current->egid = egid;
232 else {
233 current->gid = old_rgid;
234 return(-EPERM);
235 }
236 }
237 if (rgid != (gid_t) -1 ||
238 (egid != (gid_t) -1 && egid != old_rgid))
239 current->sgid = current->egid;
240 current->fsgid = current->egid;
241 if (current->egid != old_egid)
242 current->dumpable = 0;
243 return 0;
244 }
245
246
247
248
249 asmlinkage int sys_setgid(gid_t gid)
250 {
251 int old_egid = current->egid;
252
253 if (suser())
254 current->gid = current->egid = current->sgid = current->fsgid = gid;
255 else if ((gid == current->gid) || (gid == current->sgid))
256 current->egid = current->fsgid = gid;
257 else
258 return -EPERM;
259 if (current->egid != old_egid)
260 current->dumpable = 0;
261 return 0;
262 }
263
264 asmlinkage int sys_acct(void)
265 {
266 return -ENOSYS;
267 }
268
269 asmlinkage int sys_phys(void)
270 {
271 return -ENOSYS;
272 }
273
274 asmlinkage int sys_lock(void)
275 {
276 return -ENOSYS;
277 }
278
279 asmlinkage int sys_mpx(void)
280 {
281 return -ENOSYS;
282 }
283
284 asmlinkage int sys_ulimit(void)
285 {
286 return -ENOSYS;
287 }
288
289 asmlinkage int sys_old_syscall(void)
290 {
291 return -ENOSYS;
292 }
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309 asmlinkage int sys_setreuid(uid_t ruid, uid_t euid)
310 {
311 int old_ruid = current->uid;
312 int old_euid = current->euid;
313
314 if (ruid != (uid_t) -1) {
315 if ((old_ruid == ruid) ||
316 (current->euid==ruid) ||
317 suser())
318 current->uid = ruid;
319 else
320 return(-EPERM);
321 }
322 if (euid != (uid_t) -1) {
323 if ((old_ruid == euid) ||
324 (current->euid == euid) ||
325 (current->suid == euid) ||
326 suser())
327 current->euid = euid;
328 else {
329 current->uid = old_ruid;
330 return(-EPERM);
331 }
332 }
333 if (ruid != (uid_t) -1 ||
334 (euid != (uid_t) -1 && euid != old_ruid))
335 current->suid = current->euid;
336 current->fsuid = current->euid;
337 if (current->euid != old_euid)
338 current->dumpable = 0;
339 return 0;
340 }
341
342
343
344
345
346
347
348
349
350
351
352
353 asmlinkage int sys_setuid(uid_t uid)
354 {
355 int old_euid = current->euid;
356
357 if (suser())
358 current->uid = current->euid = current->suid = current->fsuid = uid;
359 else if ((uid == current->uid) || (uid == current->suid))
360 current->fsuid = current->euid = uid;
361 else
362 return -EPERM;
363 if (current->euid != old_euid)
364 current->dumpable = 0;
365 return(0);
366 }
367
368
369
370
371
372
373
374 asmlinkage int sys_setfsuid(uid_t uid)
375 {
376 int old_fsuid = current->fsuid;
377
378 if (uid == current->uid || uid == current->euid ||
379 uid == current->suid || uid == current->fsuid || suser())
380 current->fsuid = uid;
381 if (current->fsuid != old_fsuid)
382 current->dumpable = 0;
383 return old_fsuid;
384 }
385
386
387
388
389 asmlinkage int sys_setfsgid(gid_t gid)
390 {
391 int old_fsgid = current->fsgid;
392
393 if (gid == current->gid || gid == current->egid ||
394 gid == current->sgid || gid == current->fsgid || suser())
395 current->fsgid = gid;
396 if (current->fsgid != old_fsgid)
397 current->dumpable = 0;
398 return old_fsgid;
399 }
400
401 asmlinkage int sys_times(struct tms * tbuf)
402 {
403 if (tbuf) {
404 int error = verify_area(VERIFY_WRITE,tbuf,sizeof *tbuf);
405 if (error)
406 return error;
407 put_user(current->utime,&tbuf->tms_utime);
408 put_user(current->stime,&tbuf->tms_stime);
409 put_user(current->cutime,&tbuf->tms_cutime);
410 put_user(current->cstime,&tbuf->tms_cstime);
411 }
412 return jiffies;
413 }
414
415 asmlinkage unsigned long sys_brk(unsigned long brk)
416 {
417 int freepages;
418 unsigned long rlim;
419 unsigned long newbrk, oldbrk;
420
421 if (brk < current->mm->end_code)
422 return current->mm->brk;
423 newbrk = PAGE_ALIGN(brk);
424 oldbrk = PAGE_ALIGN(current->mm->brk);
425 if (oldbrk == newbrk)
426 return current->mm->brk = brk;
427
428
429
430
431 if (brk <= current->mm->brk) {
432 current->mm->brk = brk;
433 do_munmap(newbrk, oldbrk-newbrk);
434 return brk;
435 }
436
437
438
439 rlim = current->rlim[RLIMIT_DATA].rlim_cur;
440 if (rlim >= RLIM_INFINITY)
441 rlim = ~0;
442 if (brk - current->mm->end_code > rlim)
443 return current->mm->brk;
444
445
446
447 if (find_vma_intersection(current, oldbrk, newbrk+PAGE_SIZE))
448 return current->mm->brk;
449
450
451
452
453
454 freepages = buffermem >> PAGE_SHIFT;
455 freepages += nr_free_pages;
456 freepages += nr_swap_pages;
457 freepages -= MAP_NR(high_memory) >> 4;
458 freepages -= (newbrk-oldbrk) >> PAGE_SHIFT;
459 if (freepages < 0)
460 return current->mm->brk;
461 #if 0
462 freepages += current->mm->rss;
463 freepages -= oldbrk >> 12;
464 if (freepages < 0)
465 return current->mm->brk;
466 #endif
467
468
469
470 current->mm->brk = brk;
471 do_mmap(NULL, oldbrk, newbrk-oldbrk,
472 PROT_READ|PROT_WRITE|PROT_EXEC,
473 MAP_FIXED|MAP_PRIVATE, 0);
474 return brk;
475 }
476
477
478
479
480
481
482
483
484
485
486
487
488
489 asmlinkage int sys_setpgid(pid_t pid, pid_t pgid)
490 {
491 struct task_struct * p;
492
493 if (!pid)
494 pid = current->pid;
495 if (!pgid)
496 pgid = pid;
497 if (pgid < 0)
498 return -EINVAL;
499 for_each_task(p) {
500 if (p->pid == pid)
501 goto found_task;
502 }
503 return -ESRCH;
504
505 found_task:
506 if (p->p_pptr == current || p->p_opptr == current) {
507 if (p->session != current->session)
508 return -EPERM;
509 if (p->did_exec)
510 return -EACCES;
511 } else if (p != current)
512 return -ESRCH;
513 if (p->leader)
514 return -EPERM;
515 if (pgid != pid) {
516 struct task_struct * tmp;
517 for_each_task (tmp) {
518 if (tmp->pgrp == pgid &&
519 tmp->session == current->session)
520 goto ok_pgid;
521 }
522 return -EPERM;
523 }
524
525 ok_pgid:
526 p->pgrp = pgid;
527 return 0;
528 }
529
530 asmlinkage int sys_getpgid(pid_t pid)
531 {
532 struct task_struct * p;
533
534 if (!pid)
535 return current->pgrp;
536 for_each_task(p) {
537 if (p->pid == pid)
538 return p->pgrp;
539 }
540 return -ESRCH;
541 }
542
543 asmlinkage int sys_getpgrp(void)
544 {
545 return current->pgrp;
546 }
547
548 asmlinkage int sys_getsid(pid_t pid)
549 {
550 struct task_struct * p;
551
552 if (!pid)
553 return current->session;
554 for_each_task(p) {
555 if (p->pid == pid)
556 return p->session;
557 }
558 return -ESRCH;
559 }
560
561 asmlinkage int sys_setsid(void)
562 {
563 if (current->leader)
564 return -EPERM;
565 current->leader = 1;
566 current->session = current->pgrp = current->pid;
567 current->tty = NULL;
568 current->tty_old_pgrp = 0;
569 return current->pgrp;
570 }
571
572
573
574
575 asmlinkage int sys_getgroups(int gidsetsize, gid_t *grouplist)
576 {
577 int i;
578 int * groups;
579
580 if (gidsetsize) {
581 i = verify_area(VERIFY_WRITE, grouplist, sizeof(gid_t) * gidsetsize);
582 if (i)
583 return i;
584 }
585 groups = current->groups;
586 for (i = 0 ; (i < NGROUPS) && (*groups != NOGROUP) ; i++, groups++) {
587 if (!gidsetsize)
588 continue;
589 if (i >= gidsetsize)
590 break;
591 put_user(*groups, grouplist);
592 grouplist++;
593 }
594 return(i);
595 }
596
597 asmlinkage int sys_setgroups(int gidsetsize, gid_t *grouplist)
598 {
599 int i;
600
601 if (!suser())
602 return -EPERM;
603 if (gidsetsize > NGROUPS)
604 return -EINVAL;
605 i = verify_area(VERIFY_READ, grouplist, sizeof(gid_t) * gidsetsize);
606 if (i)
607 return i;
608 for (i = 0; i < gidsetsize; i++, grouplist++) {
609 current->groups[i] = get_user(grouplist);
610 }
611 if (i < NGROUPS)
612 current->groups[i] = NOGROUP;
613 return 0;
614 }
615
616 int in_group_p(gid_t grp)
617 {
618 int i;
619
620 if (grp == current->fsgid)
621 return 1;
622
623 for (i = 0; i < NGROUPS; i++) {
624 if (current->groups[i] == NOGROUP)
625 break;
626 if (current->groups[i] == grp)
627 return 1;
628 }
629 return 0;
630 }
631
632 asmlinkage int sys_newuname(struct new_utsname * name)
633 {
634 int error;
635
636 if (!name)
637 return -EFAULT;
638 error = verify_area(VERIFY_WRITE, name, sizeof *name);
639 if (!error)
640 memcpy_tofs(name,&system_utsname,sizeof *name);
641 return error;
642 }
643
644 asmlinkage int sys_uname(struct old_utsname * name)
645 {
646 int error;
647 if (!name)
648 return -EFAULT;
649 error = verify_area(VERIFY_WRITE, name,sizeof *name);
650 if (error)
651 return error;
652 memcpy_tofs(&name->sysname,&system_utsname.sysname,
653 sizeof (system_utsname.sysname));
654 memcpy_tofs(&name->nodename,&system_utsname.nodename,
655 sizeof (system_utsname.nodename));
656 memcpy_tofs(&name->release,&system_utsname.release,
657 sizeof (system_utsname.release));
658 memcpy_tofs(&name->version,&system_utsname.version,
659 sizeof (system_utsname.version));
660 memcpy_tofs(&name->machine,&system_utsname.machine,
661 sizeof (system_utsname.machine));
662 return 0;
663 }
664
665 asmlinkage int sys_olduname(struct oldold_utsname * name)
666 {
667 int error;
668 if (!name)
669 return -EFAULT;
670 error = verify_area(VERIFY_WRITE, name,sizeof *name);
671 if (error)
672 return error;
673 memcpy_tofs(&name->sysname,&system_utsname.sysname,__OLD_UTS_LEN);
674 put_user(0,name->sysname+__OLD_UTS_LEN);
675 memcpy_tofs(&name->nodename,&system_utsname.nodename,__OLD_UTS_LEN);
676 put_user(0,name->nodename+__OLD_UTS_LEN);
677 memcpy_tofs(&name->release,&system_utsname.release,__OLD_UTS_LEN);
678 put_user(0,name->release+__OLD_UTS_LEN);
679 memcpy_tofs(&name->version,&system_utsname.version,__OLD_UTS_LEN);
680 put_user(0,name->version+__OLD_UTS_LEN);
681 memcpy_tofs(&name->machine,&system_utsname.machine,__OLD_UTS_LEN);
682 put_user(0,name->machine+__OLD_UTS_LEN);
683 return 0;
684 }
685
686 asmlinkage int sys_sethostname(char *name, int len)
687 {
688 int error;
689
690 if (!suser())
691 return -EPERM;
692 if (len < 0 || len > __NEW_UTS_LEN)
693 return -EINVAL;
694 error = verify_area(VERIFY_READ, name, len);
695 if (error)
696 return error;
697 memcpy_fromfs(system_utsname.nodename, name, len);
698 system_utsname.nodename[len] = 0;
699 return 0;
700 }
701
702 asmlinkage int sys_gethostname(char *name, int len)
703 {
704 int i;
705
706 if (len < 0)
707 return -EINVAL;
708 i = verify_area(VERIFY_WRITE, name, len);
709 if (i)
710 return i;
711 i = 1+strlen(system_utsname.nodename);
712 if (i > len)
713 i = len;
714 memcpy_tofs(name, system_utsname.nodename, i);
715 return 0;
716 }
717
718
719
720
721
722 asmlinkage int sys_setdomainname(char *name, int len)
723 {
724 int error;
725
726 if (!suser())
727 return -EPERM;
728 if (len < 0 || len > __NEW_UTS_LEN)
729 return -EINVAL;
730 error = verify_area(VERIFY_READ, name, len);
731 if (error)
732 return error;
733 memcpy_fromfs(system_utsname.domainname, name, len);
734 system_utsname.domainname[len] = 0;
735 return 0;
736 }
737
738 asmlinkage int sys_getrlimit(unsigned int resource, struct rlimit *rlim)
739 {
740 int error;
741
742 if (resource >= RLIM_NLIMITS)
743 return -EINVAL;
744 error = verify_area(VERIFY_WRITE,rlim,sizeof *rlim);
745 if (error)
746 return error;
747 memcpy_tofs(rlim, current->rlim + resource, sizeof(*rlim));
748 return 0;
749 }
750
751 asmlinkage int sys_setrlimit(unsigned int resource, struct rlimit *rlim)
752 {
753 struct rlimit new_rlim, *old_rlim;
754 int err;
755
756 if (resource >= RLIM_NLIMITS)
757 return -EINVAL;
758 err = verify_area(VERIFY_READ, rlim, sizeof(*rlim));
759 if (err)
760 return err;
761 memcpy_fromfs(&new_rlim, rlim, sizeof(*rlim));
762 old_rlim = current->rlim + resource;
763 if (((new_rlim.rlim_cur > old_rlim->rlim_max) ||
764 (new_rlim.rlim_max > old_rlim->rlim_max)) &&
765 !suser())
766 return -EPERM;
767 if (resource == RLIMIT_NOFILE) {
768 if (new_rlim.rlim_cur > NR_OPEN || new_rlim.rlim_max > NR_OPEN)
769 return -EPERM;
770 }
771 *old_rlim = new_rlim;
772 return 0;
773 }
774
775
776
777
778
779
780
781
782
783 int getrusage(struct task_struct *p, int who, struct rusage *ru)
784 {
785 int error;
786 struct rusage r;
787
788 error = verify_area(VERIFY_WRITE, ru, sizeof *ru);
789 if (error)
790 return error;
791 memset((char *) &r, 0, sizeof(r));
792 switch (who) {
793 case RUSAGE_SELF:
794 r.ru_utime.tv_sec = CT_TO_SECS(p->utime);
795 r.ru_utime.tv_usec = CT_TO_USECS(p->utime);
796 r.ru_stime.tv_sec = CT_TO_SECS(p->stime);
797 r.ru_stime.tv_usec = CT_TO_USECS(p->stime);
798 r.ru_minflt = p->min_flt;
799 r.ru_majflt = p->maj_flt;
800 r.ru_nswap = p->nswap;
801 break;
802 case RUSAGE_CHILDREN:
803 r.ru_utime.tv_sec = CT_TO_SECS(p->cutime);
804 r.ru_utime.tv_usec = CT_TO_USECS(p->cutime);
805 r.ru_stime.tv_sec = CT_TO_SECS(p->cstime);
806 r.ru_stime.tv_usec = CT_TO_USECS(p->cstime);
807 r.ru_minflt = p->cmin_flt;
808 r.ru_majflt = p->cmaj_flt;
809 r.ru_nswap = p->cnswap;
810 break;
811 default:
812 r.ru_utime.tv_sec = CT_TO_SECS(p->utime + p->cutime);
813 r.ru_utime.tv_usec = CT_TO_USECS(p->utime + p->cutime);
814 r.ru_stime.tv_sec = CT_TO_SECS(p->stime + p->cstime);
815 r.ru_stime.tv_usec = CT_TO_USECS(p->stime + p->cstime);
816 r.ru_minflt = p->min_flt + p->cmin_flt;
817 r.ru_majflt = p->maj_flt + p->cmaj_flt;
818 r.ru_nswap = p->nswap + p->cnswap;
819 break;
820 }
821 memcpy_tofs(ru, &r, sizeof(r));
822 return 0;
823 }
824
825 asmlinkage int sys_getrusage(int who, struct rusage *ru)
826 {
827 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
828 return -EINVAL;
829 return getrusage(current, who, ru);
830 }
831
832 asmlinkage int sys_umask(int mask)
833 {
834 int old = current->fs->umask;
835
836 current->fs->umask = mask & S_IRWXUGO;
837 return (old);
838 }